最近打比赛用到的SAR舰船目标检测集,赛方给出的是dota格式的标签文件,如图:
上图中前8个数据代表真实框四个点的坐标(以左上角坐标顺时针旋转),ship是DOTA数据集的分类,最后的0表示识别难易程度是简单,为1表示难。
要用yolo检测必须先把dota标签文件转化为yolo标签文件。转化代码如下,这里使用的是参考程序中的YOLO_Transform.py这个脚本,同时需注意,需要在dota_utils.py中修改类别名称wordname_18。
YOLO_Transform.py
import dota_utils as util
import os
import numpy as np
from PIL import Image
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
Image.MAX_IMAGE_PIXELS = None
## trans dota format to format YOLO(darknet) required
def dota2darknet(imgpath, txtpath, dstpath, extractclassname):
"""
:param imgpath: the path of images
:param txtpath: the path of txt in dota format
:param dstpath: the path of txt in YOLO format
:param extractclassname: the category you selected
:return:
"""
filelist = util.GetFileFromThisRootDir(txtpath)
for fullname in filelist:
objects = util.parse_dota_poly(fullname)
name = os.path.splitext(os.path.basename(fullname))[0]
img_fullname = os.path.join(imgpath, name + '.png')
img = Image.open(img_fullname)
img_w, img_h = img.size
# print img_w,img_h
with open(os.path.join(dstpath, name + '.txt'), 'w') as f_out:
for obj in objects:
poly = obj['poly']
bbox = np.array(util.dots4ToRecC(poly, img_w, img_h))
if (sum(bbox <= 0) + sum(bbox >= 1)) >= 1:
continue
if (obj['name'] in extractclassname):
id = extractclassname.index(obj['name'])
else:
continue
outline = str(id) + ' ' + ' '.join(list(map(str, bbox)))
f_out.write(outline + '\n')
if __name__ == '__main__':
dota2darknet('C:/Users/xy/Desktop/Work/upload/DOTA/train/images',
'C:/Users/xy/Desktop/Work/upload/DOTA/train/labels1',
'C:/Users/xy/Desktop/Work/upload/DOTA/train/labels',
util.wordname_18)
dota2darknet('C:/Users/xy/Desktop/Work/upload/DOTA/val/images',
'C:/Users/xy/Desktop/Work/upload/DOTA/val/labels1',
'C:/Users/xy/Desktop/Work/upload/DOTA/val/labels',
util.wordname_18)
dota_utils.py
import sys
import codecs
import numpy as np
import shapely.geometry as shgeo
import os
import re
import math
"""
some basic functions which are useful for process DOTA data
"""
wordname_18 = [
'airport',
'small-vehicle',
'large-vehicle',
'plane',
'storage-tank',
'ship',
'harbor',
'ground-track-field',
'soccer-ball-field',
'tennis-court',
'swimming-pool',
'baseball-diamond',
'roundabout',
'basketball-court',
'bridge',
'helicopter',
'container-crane',
'helipad']
def custombasename(fullname):
return os.path.basename(os.path.splitext(fullname)[0])
def GetFileFromThisRootDir(dir,ext = None):
allfiles = []
needExtFilter = (ext != None)
for root,dirs,files in os.walk(dir):
for filespath in files:
filepath = os.path.join(root, filespath)
extension = os.path.splitext(filepath)[1][1:]
if needExtFilter and extension in ext:
allfiles.append(filepath)
elif not needExtFilter:
allfiles.append(filepath)
return allfiles
def TuplePoly2Poly(poly):
outpoly = [poly[0][0], poly[0][1],
poly[1][0], poly[1][1],
poly[2][0], poly[2][1],
poly[3][0], poly[3][1]
]
return outpoly
def parse_dota_poly(filename):
"""
parse the dota ground truth in the format:
[(x1, y1), (x2, y2), (x3, y3), (x4, y4)]
"""
objects = []
#print('filename:', filename)
f = []
if (sys.version_info >= (3, 5)):
fd = open(filename, 'r')
f = fd
elif (sys.version_info >= 2.7):
fd = codecs.open(filename, 'r')
f = fd
# count = 0
while True:
line = f.readline()
# count = count + 1
# if count < 2:
# continue
if line:
splitlines = line.strip().split(' ')
object_struct = {}
### clear the wrong name after check all the data
#if (len(splitlines) >= 9) and (splitlines[8] in classname):
if (len(splitlines) < 9):
continue
if (len(splitlines) >= 9):
object_struct['name'] = splitlines[8]
if (len(splitlines) == 9):
object_struct['difficult'] = '0'
elif (len(splitlines) >= 10):
# if splitlines[9] == '1':
# if (splitlines[9] == 'tr'):
# object_struct['difficult'] = '1'
# else:
object_struct['difficult'] = splitlines[9]
# else:
# object_struct['difficult'] = 0
object_struct['poly'] = [(float(splitlines[0]), float(splitlines[1])),
(float(splitlines[2]), float(splitlines[3])),
(float(splitlines[4]), float(splitlines[5])),
(float(splitlines[6]), float(splitlines[7]))
]
gtpoly = shgeo.Polygon(object_struct['poly'])
object_struct['area'] = gtpoly.area
# poly = list(map(lambda x:np.array(x), object_struct['poly']))
# object_struct['long-axis'] = max(distance(poly[0], poly[1]), distance(poly[1], poly[2]))
# object_struct['short-axis'] = min(distance(poly[0], poly[1]), distance(poly[1], poly[2]))
# if (object_struct['long-axis'] < 15):
# object_struct['difficult'] = '1'
# global small_count
# small_count = small_count + 1
objects.append(object_struct)
else:
break
return objects
def dots4ToRecC(poly, img_w, img_h):
xmin, ymin, xmax, ymax = dots4ToRec4(poly)
x = (xmin + xmax)/2
y = (ymin + ymax)/2
w = xmax - xmin
h = ymax - ymin
return x/img_w, y/img_h, w/img_w, h/img_h
def parse_dota_poly2(filename):
"""
parse the dota ground truth in the format:
[x1, y1, x2, y2, x3, y3, x4, y4]
"""
objects = parse_dota_poly(filename)
for obj in objects:
obj['poly'] = TuplePoly2Poly(obj['poly'])
obj['poly'] = list(map(int, obj['poly']))
return objects
def parse_dota_rec(filename):
"""
parse the dota ground truth in the bounding box format:
"xmin, ymin, xmax, ymax"
"""
objects = parse_dota_poly(filename)
for obj in objects:
poly = obj['poly']
bbox = dots4ToRec4(poly)
obj['bndbox'] = bbox
return objects
## bounding box transfer for varies format
def dots4ToRec4(poly):
xmin, xmax, ymin, ymax = min(poly[0][0], min(poly[1][0], min(poly[2][0], poly[3][0]))), \
max(poly[0][0], max(poly[1][0], max(poly[2][0], poly[3][0]))), \
min(poly[0][1], min(poly[1][1], min(poly[2][1], poly[3][1]))), \
max(poly[0][1], max(poly[1][1], max(poly[2][1], poly[3][1])))
return xmin, ymin, xmax, ymax
def dots4ToRec8(poly):
xmin, ymin, xmax, ymax = dots4ToRec4(poly)
return xmin, ymin, xmax, ymin, xmax, ymax, xmin, ymax
#return dots2ToRec8(dots4ToRec4(poly))
def dots2ToRec8(rec):
xmin, ymin, xmax, ymax = rec[0], rec[1], rec[2], rec[3]
return xmin, ymin, xmax, ymin, xmax, ymax, xmin, ymax
def groundtruth2Task1(srcpath, dstpath):
filelist = GetFileFromThisRootDir(srcpath)
# names = [custombasename(x.strip())for x in filelist]
filedict = {}
for cls in wordname_15:
fd = open(os.path.join(dstpath, 'Task1_') + cls + r'.txt', 'w')
filedict[cls] = fd
for filepath in filelist:
objects = parse_dota_poly2(filepath)
subname = custombasename(filepath)
pattern2 = re.compile(r'__([\d+\.]+)__\d+___')
rate = re.findall(pattern2, subname)[0]
for obj in objects:
category = obj['name']
difficult = obj['difficult']
poly = obj['poly']
if difficult == '2':
continue
if rate == '0.5':
outline = custombasename(filepath) + ' ' + '1' + ' ' + ' '.join(map(str, poly))
elif rate == '1':
outline = custombasename(filepath) + ' ' + '0.8' + ' ' + ' '.join(map(str, poly))
elif rate == '2':
outline = custombasename(filepath) + ' ' + '0.6' + ' ' + ' '.join(map(str, poly))
filedict[category].write(outline + '\n')
def Task2groundtruth_poly(srcpath, dstpath):
thresh = 0.1
filedict = {}
Tasklist = GetFileFromThisRootDir(srcpath, '.txt')
for Taskfile in Tasklist:
idname = custombasename(Taskfile).split('_')[-1]
# idname = datamap_inverse[idname]
f = open(Taskfile, 'r')
lines = f.readlines()
for line in lines:
if len(line) == 0:
continue
# print('line:', line)
splitline = line.strip().split(' ')
filename = splitline[0]
confidence = splitline[1]
bbox = splitline[2:]
if float(confidence) > thresh:
if filename not in filedict:
# filedict[filename] = codecs.open(os.path.join(dstpath, filename + '.txt'), 'w', 'utf_16')
filedict[filename] = codecs.open(os.path.join(dstpath, filename + '.txt'), 'w')
# poly = util.dots2ToRec8(bbox)
poly = bbox
# filedict[filename].write(' '.join(poly) + ' ' + idname + '_' + str(round(float(confidence), 2)) + '\n')
# print('idname:', idname)
# filedict[filename].write(' '.join(poly) + ' ' + idname + '_' + str(round(float(confidence), 2)) + '\n')
filedict[filename].write(' '.join(poly) + ' ' + idname + '\n')
def polygonToRotRectangle(bbox):
"""
:param bbox: The polygon stored in format [x1, y1, x2, y2, x3, y3, x4, y4]
:return: Rotated Rectangle in format [cx, cy, w, h, theta]
"""
bbox = np.array(bbox,dtype=np.float32)
bbox = np.reshape(bbox,newshape=(2,4),order='F')
angle = math.atan2(-(bbox[0,1]-bbox[0,0]),bbox[1,1]-bbox[1,0])
center = [[0],[0]]
for i in range(4):
center[0] += bbox[0,i]
center[1] += bbox[1,i]
center = np.array(center,dtype=np.float32)/4.0
R = np.array([[math.cos(angle), -math.sin(angle)], [math.sin(angle), math.cos(angle)]], dtype=np.float32)
normalized = np.matmul(R.transpose(),bbox-center)
xmin = np.min(normalized[0,:])
xmax = np.max(normalized[0,:])
ymin = np.min(normalized[1,:])
ymax = np.max(normalized[1,:])
w = xmax - xmin + 1
h = ymax - ymin + 1
return [float(center[0]),float(center[1]),w,h,angle]
转化成功
文章来源:https://www.toymoban.com/news/detail-607745.html
参考博客:【目标检测】YOLO+DOTA:小样本检测策略文章来源地址https://www.toymoban.com/news/detail-607745.html
到了这里,关于深度学习|dota格式的txt文件转化为yolo格式的txt文件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!