YOLO格式txt数据与COCO格式xml数据互转
【代码】YOLO格式txt数据与COCO格式xml数据互转。
·
txt转xml
from xml.dom.minidom import Document
import os
import cv2
def makexml(txtPath, xmlPath, picPath): # txt所在文件夹路径,xml文件保存路径,图片所在文件夹路径
"""此函数用于将yolo格式txt标注文件转换为voc格式xml标注文件
chirography
color dot
hole
drawnwork
在自己的标注图片文件夹下建三个子文件夹,分别命名为picture、txt、xml
"""
# 本例中的标签有5个类,按照自己的类别进行更改
dic = {'0': "chirography", # 创建字典用来对类型进行转换
'1': "color dot", # 此处的字典要与自己的classes.txt文件中的类对应,且顺序要一致
'2': "hole",
'3': "drawnwork"
}
files = os.listdir(txtPath)
for i, name in enumerate(files):
xmlBuilder = Document()
annotation = xmlBuilder.createElement("annotation") # 创建annotation标签
xmlBuilder.appendChild(annotation)
txtFile = open(txtPath + name)
txtList = txtFile.readlines()
img = cv2.imread(picPath + name[0:-4] + ".bmp")
Pheight, Pwidth, Pdepth = img.shape
folder = xmlBuilder.createElement("folder") # folder标签
foldercontent = xmlBuilder.createTextNode("my_person")
folder.appendChild(foldercontent)
annotation.appendChild(folder) # folder标签结束
filename = xmlBuilder.createElement("filename") # filename标签
filenamecontent = xmlBuilder.createTextNode(name[0:-4] + ".bmp")
filename.appendChild(filenamecontent)
annotation.appendChild(filename) # filename标签结束
path = xmlBuilder.createElement("path") # path标签
pathcontent = xmlBuilder.createTextNode(r"E:\My_project\projectCode\Python\new_things\datasets\datasets_13\\"+name[0:-4] + ".bmp")
path.appendChild(pathcontent)
annotation.appendChild(path) # path标签结束
source = xmlBuilder.createElement("source") # source标签
database = xmlBuilder.createElement("database") # source子标签database
databasecontent = xmlBuilder.createTextNode("Unknown")
database.appendChild(databasecontent)
source.appendChild(database) # source子标签database结束
annotation.appendChild(source) # source标签结束
size = xmlBuilder.createElement("size") # size标签
width = xmlBuilder.createElement("width") # size子标签width
widthcontent = xmlBuilder.createTextNode(str(Pwidth))
width.appendChild(widthcontent)
size.appendChild(width) # size子标签width结束
height = xmlBuilder.createElement("height") # size子标签height
heightcontent = xmlBuilder.createTextNode(str(Pheight))
height.appendChild(heightcontent)
size.appendChild(height) # size子标签height结束
depth = xmlBuilder.createElement("depth") # size子标签depth
depthcontent = xmlBuilder.createTextNode(str(Pdepth))
depth.appendChild(depthcontent)
size.appendChild(depth) # size子标签depth结束
annotation.appendChild(size) # size标签结束
for j in txtList:
oneline = j.strip().split(" ")
object = xmlBuilder.createElement("object") # object 标签
picname = xmlBuilder.createElement("name") # name标签
namecontent = xmlBuilder.createTextNode(dic[oneline[0]])
picname.appendChild(namecontent)
object.appendChild(picname) # name标签结束
pose = xmlBuilder.createElement("pose") # pose标签
posecontent = xmlBuilder.createTextNode("Unspecified")
pose.appendChild(posecontent)
object.appendChild(pose) # pose标签结束
truncated = xmlBuilder.createElement("truncated") # truncated标签
truncatedContent = xmlBuilder.createTextNode("0")
truncated.appendChild(truncatedContent)
object.appendChild(truncated) # truncated标签结束
difficult = xmlBuilder.createElement("difficult") # difficult标签
difficultcontent = xmlBuilder.createTextNode("0")
difficult.appendChild(difficultcontent)
object.appendChild(difficult) # difficult标签结束
bndbox = xmlBuilder.createElement("bndbox") # bndbox标签
xmin = xmlBuilder.createElement("xmin") # xmin标签
mathData = int(((float(oneline[1])) * Pwidth ) - (float(oneline[3])) * 0.5 * Pwidth)
xminContent = xmlBuilder.createTextNode(str(mathData))
xmin.appendChild(xminContent)
bndbox.appendChild(xmin) # xmin标签结束
ymin = xmlBuilder.createElement("ymin") # ymin标签
mathData = int(((float(oneline[2])) * Pheight) - (float(oneline[4])) * 0.5 * Pheight)
yminContent = xmlBuilder.createTextNode(str(mathData))
ymin.appendChild(yminContent)
bndbox.appendChild(ymin) # ymin标签结束
xmax = xmlBuilder.createElement("xmax") # xmax标签
mathData = int(((float(oneline[1])) * Pwidth ) + (float(oneline[3])) * 0.5 * Pwidth)
xmaxContent = xmlBuilder.createTextNode(str(mathData))
xmax.appendChild(xmaxContent)
bndbox.appendChild(xmax) # xmax标签结束
ymax = xmlBuilder.createElement("ymax") # ymax标签
mathData = int(((float(oneline[2])) * Pheight + 1) + (float(oneline[4])) * 0.5 * Pheight)
ymaxContent = xmlBuilder.createTextNode(str(mathData))
ymax.appendChild(ymaxContent)
bndbox.appendChild(ymax) # ymax标签结束
object.appendChild(bndbox) # bndbox标签结束
annotation.appendChild(object) # object标签结束
f = open(xmlPath + name[0:-4] + ".xml", 'w')
xmlBuilder.writexml(f, indent='', newl='\n', addindent='\t', encoding='utf-8')
f.close()
# 注意下面的路径要换成自己文件夹的路径,否则会报错
makexml(r"E:\My_project\projectCode\Python\new_things\datasets\datasets_13\label/",
r"E:\My_project\projectCode\Python\new_things\datasets\datasets_13\label_xml/",
r"E:\My_project\projectCode\Python\new_things\datasets\datasets_13\img/")
# yolo标签的txt格式所在文件夹路径,voc标签的xml格式的文件保存路径,图片所在文件夹路径 !!!注意39行处还需要修改成自己文件夹的路径
xml转txt
import xml.etree.ElementTree as ET
import os
from glob import glob
# VOC格式的xml转YOLO格式的txt
def convert(size, box):
'''
size = (w, h)
box = (xmin, xmax, ymin, ymax)
'''
dw = 1. / size[0]
dh = 1. / size[1]
x = (box[0] + box[1]) / 2.0
y = (box[2] + box[3]) / 2.0
w = box[1] - box[0]
h = box[3] - box[2]
x = x * dw
w = w * dw
y = y * dh
h = h * dh
return x, y, w, h
# 单个标注文件的转换
def convert_annotation(image_id):
res = ""
in_file = open('{}/{}.xml'.format(xml_path, image_id))
tree = ET.parse(in_file)
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
for obj in root.iter('object'):
difficult = obj.find('difficult').text
cls = obj.find('name').text
if cls not in classes or int(difficult) == 1:
continue
cls_id = classes.index(cls)
xmlbox = obj.find('bndbox')
b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
float(xmlbox.find('ymax').text))
bb = convert((w, h), b)
res += "{} {} {} {} {}\n".format(cls_id, bb[0], bb[1], bb[2], bb[3])
return res
# 写文件
def write_file(fname, content):
with open(fname, 'w') as f:
f.write(content)
# 批量转换
def generate_label_file():
# 获取所有xml文件
xmls = glob("{}/*.xml".format(xml_path))
xml_ids = [v.split(os.sep)[-1].split('.xml')[0] for v in xmls]
for xml_id in xml_ids:
content = convert_annotation(xml_id)
if content != "":
lable_file = "{}/{}.txt".format(txt_path, xml_id)
write_file(lable_file, content)
if __name__ == '__main__':
# 填写xml文件路径、txt文件保存路径
xml_path = r"xml"
txt_path = r"txt"
# 填写类别列表
classes = ["chirography","color dot","hole","drawnwork"]
# 执行转换
generate_label_file()
更多推荐
所有评论(0)