1.txt文件格式
DOTA数据集的txt文件格式如下:
其中,每一行的前8个数字代表目标物体四个顶点的坐标。
2.xml文件格式
使用labimg手动标注得到的xml文件:
从txt转换到xml有两种选择:
1.将原有的8坐标转换为4坐标的最小外界矩形,即hbb形式 xmin,ymin,xmax,ymax
2.保留原有坐标,即obb形式x0,y0,x1,y1,x2,y2,x3,y3
3.一般的txt到xml的转换思路
sourcetxtfile=fopen('p0000.txt')
all_img_name=os.listdir('./') #找到图片的全部文件名写入一个列表
img_num=len(all_img) #图片数量
#以下是遍历txt的写法×××××××××××××××××××××××××××××××××××××××××××××××××××××××
for i in range(len(img_num)):
current_img_name=all_img_name[i] #当前图片名字
current_img_name_xml=str(current_img_name)+'.xml' #转为str型并改为xml全名
f=fopen(current_img_name_xml) #新建一个上面生成的xml文件
#读取txt坐标
sourcettxtfile.readlines()
然后按照一定规律读就行,写成列表,比如过有八个框,就写成一个32维度的矩阵
#读取完写入一个location[32个数字]的列表
fwrite('<annotation>'+'\n') #第一行加换行符
fwrite('工程的文件夹目录 +换行符)
fwrite(current_img_name=all_img_name[i] )
`````
`````
fwrite(<segmented>0</segmented>)#第一步结束
#循环len(location)/4次,也就是8次,写xml文件
fwrite('<object>'+'\n)
fwrite(<name>你的标签</name>+\n)
`````
`````
fwrite(xmin+\n)
fwrite(ymin+\n)
fwrite(xmax+\n)
fwrite(ymax+\n)
`````
`````
fwrite('<object>'+'\n)
#循环完就基本就结束,最后一部分对应上面的
fwrite('<annotation>'+'\n')
f.close()关闭所有打开的文件
#××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
####这样p0000.txt就转为了第一张图片对应的xml文件
#如果txt文件也遍历就用××××××××××××之内的办法
4.最终使用的txt到xml转换的脚本
import os
import cv2
from xml.dom.minidom import Document
category_set = ['ship']
def custombasename(fullname):
return os.path.basename(os.path.splitext(fullname)[0]) #只返回文件名
def limit_value(a, b):
if a < 1:
a = 1
if a >= b:
a = b - 1
return a
def readlabeltxt(txtpath, height, width, hbb=True):
print(txtpath)
with open(txtpath, 'r') as f_in: # 打开txt文件
lines = f_in.readlines()
splitlines = [x.strip().split(' ') for x in lines] # 根据空格分割
boxes = []
for i, splitline in enumerate(splitlines): #splitline按照行分隔,返回一个包含各行作为元素的列表
if i in [0, 1]: # DOTA数据集前两行对于我们来说是无用的
continue
label = splitline[8]
if label not in category_set: # 只书写制定的类别 #最后可print出不含ship的
continue
x1