第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > yolo图像检测数据集格式转换:xml 与 txt格式相互转换

yolo图像检测数据集格式转换:xml 与 txt格式相互转换

时间:2019-11-27 17:57:30

相关推荐

yolo图像检测数据集格式转换:xml 与 txt格式相互转换

格式介绍

一图流介绍的比较详细,一般图像检测数据集格式为txt或者xml格式,在使用labelimg进行标注的时候,可以设置获得不同格式的数据集,以满足不同算法训练格式要求:

一般建议使用pascalVoc:即PASCAL VOC数据集格式,关于该数据集的参见:PASCAL VOC

因为这样的数据方便在标注软件中看到对应的框;

xml转txt

对于xml格式数据集,如果要用yolo对其进行使用时候,先将其转化为txt格式,再进行训练,转换代码:

#!/usr/bin/env python3# -*- coding: utf-8 -*-import copyfrom lxml.etree import Element, SubElement, tostring, ElementTreeimport xml.etree.ElementTree as ETimport pickleimport osfrom os import listdir, getcwdfrom os.path import joinclasses = ["0", "1", "2", "3"] # 类别CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))def convert(size, box):dw = 1. / size[0]dh = 1. / size[1]x = (box[0] + box[1]) / 2.0y = (box[2] + box[3]) / 2.0w = box[1] - box[0]h = box[3] - box[2]x = x * dww = w * dwy = y * dhh = h * dhreturn (x, y, w, h)def convert_annotation(image_id):in_file = open('./label_xml\%s.xml' % (image_id), encoding='UTF-8')out_file = open('./label_txt\%s.txt' % (image_id), 'w') # 生成txt格式文件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'):cls = obj.find('name').text# print(cls)if cls not in classes:continuecls_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)out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')xml_path = os.path.join(CURRENT_DIR, './label_xml/')# xml listimg_xmls = os.listdir(xml_path)for img_xml in img_xmls:label_name = img_xml.split('.')[0]print(label_name)convert_annotation(label_name)

txt转xml

txt格式的数据不方便进行一些数据增强操作,可以先将其转换成xml格式后再进行相关标注工作或者

增强操作;

#!/user/bin/env python3# _*_ coding:utf-8 -*_# using: 将yolo txt label 转换成xml标签from xml.dom.minidom import Documentimport osimport cv2def makexml(txtPath, xmlPath, picPath): # 读取txt路径,xml保存路径,数据集图片所在路径dict = {'0': "0", # 字典对类型进行转换'1': "1",'2': "2",'3': "3"}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]+".jpg")Pheight, Pwidth, Pdepth=img.shapeflag = 0for i in txtList:flag += 1oneline = i.strip().split(" ")folder = xmlBuilder.createElement("folder") # folder标签folderContent = xmlBuilder.createTextNode("VOC")folder.appendChild(folderContent)annotation.appendChild(folder)if flag == 1:filename = xmlBuilder.createElement("filename") # filename标签filenameContent = xmlBuilder.createTextNode(name[0:-4]+".jpg")filename.appendChild(filenameContent)annotation.appendChild(filename)size = xmlBuilder.createElement("size") # size标签width = xmlBuilder.createElement("width") # size子标签widthwidthContent = xmlBuilder.createTextNode(str(Pwidth))width.appendChild(widthContent)size.appendChild(width)height = xmlBuilder.createElement("height") # size子标签heightheightContent = xmlBuilder.createTextNode(str(Pheight))height.appendChild(heightContent)size.appendChild(height)depth = xmlBuilder.createElement("depth") # size子标签depthdepthContent = xmlBuilder.createTextNode(str(Pdepth))depth.appendChild(depthContent)size.appendChild(depth)annotation.appendChild(size)object = xmlBuilder.createElement("object")picname = xmlBuilder.createElement("name")nameContent = xmlBuilder.createTextNode(dict[oneline[0]])picname.appendChild(nameContent)object.appendChild(picname)pose = xmlBuilder.createElement("pose")poseContent = xmlBuilder.createTextNode("Unspecified")pose.appendChild(poseContent)object.appendChild(pose)truncated = xmlBuilder.createElement("truncated")truncatedContent = xmlBuilder.createTextNode("0")truncated.appendChild(truncatedContent)object.appendChild(truncated)difficult = xmlBuilder.createElement("difficult")difficultContent = xmlBuilder.createTextNode("0")difficult.appendChild(difficultContent)object.appendChild(difficult)bndbox = xmlBuilder.createElement("bndbox")xmin = xmlBuilder.createElement("xmin")mathData=int(((float(oneline[1]))*Pwidth+1)-(float(oneline[3]))*0.5*Pwidth)xminContent = xmlBuilder.createTextNode(str(mathData))xmin.appendChild(xminContent)bndbox.appendChild(xmin)ymin = xmlBuilder.createElement("ymin")mathData = int(((float(oneline[2]))*Pheight+1)-(float(oneline[4]))*0.5*Pheight)yminContent = xmlBuilder.createTextNode(str(mathData))ymin.appendChild(yminContent)bndbox.appendChild(ymin)xmax = xmlBuilder.createElement("xmax")mathData = int(((float(oneline[1]))*Pwidth+1)+(float(oneline[3]))*0.5*Pwidth)xmaxContent = xmlBuilder.createTextNode(str(mathData))xmax.appendChild(xmaxContent)bndbox.appendChild(xmax)ymax = xmlBuilder.createElement("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)object.appendChild(bndbox)annotation.appendChild(object)f = open(xmlPath+name[0:-4]+".xml", 'w')xmlBuilder.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='utf-8')f.close()makexml("./label_txt/","./label_xml/","./image/")

其中:

label_txt: 存放txt格式的文件label_xml: 存放xml格式的文件image: 存放本地图片

参考

https://arleyzhang.github.io/articles/1dc20586//p/58392978

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。