DOTA数据集的格式为:
前6个数字分别为gt的4个点坐标(x、y)
转换为VOC标注的代码为:
import os
import cv2
from xml.dom.minidom import Document
'''
#windows下无需
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
'''
def custombasename(fullname):
return os.path.basename(os.path.splitext(fullname)[0])
def readlabeltxt(txtpath):
print(txtpath)
with open(txtpath, 'r') as f_in: #打开txt文件
lines = f_in.readlines()
splitlines = [x.strip().split(' ') for x in lines] #根据空格分割
boxes = []
for splitline in splitlines:
x1 = float(splitline[0])
y1 = float(splitline[1])
x2 = float(splitline[2])
y2 = float(splitline[3])
x3 = float(splitline[4])
y3 = float(splitline[5])
x4 = float(splitline[6])
y4 = float(splitline[7])
label = splitline[8]
xx1 = min(x1,x2,x3,x4)
xx2 = max(x1,x2,x3,x4)
yy1 = min(y1,y2,y3,y4)
yy2 = max(y1,y2,y3,y4)
box = [xx1,yy1,xx2,yy2,label]
boxes.append(box)
return boxes
def writeXml(tmp, imgname, w, h, d, bboxes):
doc = Document()
#owner
annotation = doc.createElement('annotation')
doc.appendChild(annotation)
#owner
folder = doc.createElement('folder')
annotation.appendChild(folder)
folder_txt = doc.createTextNode("VOC2007")
folder.appendChild(folder_txt)
filename = doc.createElement('filename')
annotation.appendChild(filename)
filename_txt = doc.createTextNode(imgname)
filename.appendChild(filename_txt)
#ones#
source = doc.createElement('source')
annotation.appendChild(source)
database = doc.createElement('database')
source.appendChild(database)
database_txt = doc.createTextNode("My Database")
database.appendChild(database_txt)
annotation_new = doc.createElement('annotation')
source.appendChild(annotation_new)
annotation_new_txt = doc.createTextNode("VOC2007")
annotation_new.appendChild(annotation_new_txt)
image = doc.createElement('image')
source.appendChild(image)
image_txt = doc.createTextNode("flickr")
image.appendChild(image_txt)
#owner
owner = doc.createElement('owner')
annotation.appendChild(owner)
flickrid = doc.createElement('flickrid')
owner.appendChild(flickrid)
flickrid_txt = doc.createTextNode("NULL")
flickrid.appendChild(flickrid_txt)
ow_name = doc.createElement('name')
owner.appendChild(ow_name)
ow_name_txt = doc.createTextNode("idannel")
ow_name.appendChild(ow_name_txt)
#onee#
#twos#
size = doc.createElement('size')
annotation.appendChild(size)
width = doc.createElement('width')
size.appendChild(width)
width_txt = doc.createTextNode(str(w))
width.appendChild(width_txt)
height = doc.createElement('height')
size.appendChild(height)
height_txt = doc.createTextNode(str(h))
height.appendChild(height_txt)
depth = doc.createElement('depth')
size.appendChild(depth)
depth_txt = doc.createTextNode(str(d))
depth.appendChild(depth_txt)
#twoe#
segmented = doc.createElement('segmented')
annotation.appendChild(segmented)
segmented_txt = doc.createTextNode("0")
segmented.appendChild(segmented_txt)
for bbox in bboxes:
#threes#
object_new = doc.createElement("object")
annotation.appendChild(object_new)
name = doc.createElement('name')
object_new.appendChild(name)
name_txt = doc.createTextNode(str(bbox[4]))
name.appendChild(name_txt)
pose = doc.createElement('pose')
object_new.appendChild(pose)
pose_txt = doc.createTextNode("Unspecified")
pose.appendChild(pose_txt)
truncated = doc.createElement('truncated')
object_new.appendChild(truncated)
truncated_txt = doc.createTextNode("0")
truncated.appendChild(truncated_txt)
difficult = doc.createElement('difficult')
object_new.appendChild(difficult)
difficult_txt = doc.createTextNode("0")
difficult.appendChild(difficult_txt)
#threes-1#
bndbox = doc.createElement('bndbox')
object_new.appendChild(bndbox)
xmin = doc.createElement('xmin')
bndbox.appendChild(xmin)
xmin_txt = doc.createTextNode(str(bbox[0]))
xmin.appendChild(xmin_txt)
ymin = doc.createElement('ymin')
bndbox.appendChild(ymin)
ymin_txt = doc.createTextNode(str(bbox[1]))
ymin.appendChild(ymin_txt)
xmax = doc.createElement('xmax')
bndbox.appendChild(xmax)
xmax_txt = doc.createTextNode(str(bbox[2]))
xmax.appendChild(xmax_txt)
ymax = doc.createElement('ymax')
bndbox.appendChild(ymax)
ymax_txt = doc.createTextNode(str(bbox[3]))
ymax.appendChild(ymax_txt)
xmlname = os.path.splitext(imgname)[0]
tempfile = os.path.join(tmp ,xmlname+'.xml')
with open(tempfile, 'wb') as f:
f.write(doc.toprettyxml(indent='\t', encoding='utf-8'))
return
if __name__ == '__main__':
data_path = 'F:\\hang\\aug'
images_path = os.path.join(data_path, 'new_JPEGImages') #样本图片路径
labeltxt_path = os.path.join(data_path, 'new_labelTxt') #DOTA标签的所在路径
anno_new_path = os.path.join(data_path, 'hbbxml') #新的voc格式存储位置(hbb形式)
ext = '.jpg' #样本图片的后缀
filenames=os.listdir(labeltxt_path) #获取每一个txt的名称
for filename in filenames:
filepath=labeltxt_path + '/'+filename #每一个DOTA标签的具体路径
boxes = readlabeltxt(filepath) #得到gt
#读取对应的样本图片,得到H,W,D用于书写xml
picname = os.path.splitext(filename)[0] + ext
pic_path = os.path.join(images_path, picname)
im= cv2.imread(pic_path)
(H,W,D) = im.shape
#书写xml
writeXml(anno_new_path, picname, W, H, D, boxes)
print('正在处理%s'%filename)