python 将YOLO(txt)格式的标注数据批量转换为PascalVOC(XML)格式的标注数据
python 將YOLO(txt)格式的標(biāo)注數(shù)據(jù)批量轉(zhuǎn)換為PascalVOC(XML)格式的標(biāo)注數(shù)據(jù)
- 準(zhǔn)備工作
- 修改代碼路徑
- 運(yùn)行代碼
- 程序bug(沒(méi)時(shí)間看所以還沒(méi)解決):
準(zhǔn)備工作
需在目標(biāo)文件夾中創(chuàng)建三個(gè)子文件夾,如:
20190822_Artificial_Flower_YOLO-TXT_convert_to_PascalVOC-XML├─20190822_Artificial_Flower├─20190822_Artificial_Flower_txt└─20190822_Artificial_Flower_xml其中20190822_Artificial_Flower存放我們的圖片,20190822_Artificial_Flower_txt存放我們用(yolo)已經(jīng)生成的.txt文件,20190822_Artificial_Flower_xml(空文件夾)存放我們將要生成的.xml文件。然后還有一個(gè)obj.names文件,存放我們的標(biāo)注類名。
修改代碼路徑
import os import xml.etree.ElementTree as ET from PIL import Image import numpy as np# img_path = 'C:/Users/jsb/Desktop/TFRecord/JPEGImages/' #原圖.jpg文件的路徑 img_path = 'D:/Yolov3_Tensorflow/LabelImg/YOLO-TXT_convert_to_PascalVOC-XML/20190822_Artificial_Flower_YOLO-TXT_convert_to_PascalVOC-XML/20190822_Artificial_Flower/' #原圖.jpg文件的路徑# labels_path = 'C:/Users/jsb/Desktop/TFRecord/labels/' #labels中.txt文件的路徑 labels_path = 'D:/Yolov3_Tensorflow/LabelImg/YOLO-TXT_convert_to_PascalVOC-XML/20190822_Artificial_Flower_YOLO-TXT_convert_to_PascalVOC-XML/20190822_Artificial_Flower_txt/' #labels中.txt文件的路徑# annotations_path = 'C:/Users/jsb/Desktop/TFRecord/Annotations/' #生成的xml文件需要保存的路徑 annotations_path = 'D:/Yolov3_Tensorflow\LabelImg/YOLO-TXT_convert_to_PascalVOC-XML/20190822_Artificial_Flower_YOLO-TXT_convert_to_PascalVOC-XML/20190822_Artificial_Flower_xml/' #生成的xml文件需要保存的路徑labels = os.listdir(labels_path) clsnames_path = 'D:/Yolov3_Tensorflow/LabelImg/YOLO-TXT_convert_to_PascalVOC-XML/20190822_Artificial_Flower_YOLO-TXT_convert_to_PascalVOC-XML/obj.names' #names文件的路徑 with open(clsnames_path,'r') as f:classes = f.readlines()classes = [cls.strip('\n') for cls in classes] def write_xml(imgname,filepath,labeldicts): #參數(shù)imagename是圖片名(無(wú)后綴)root = ET.Element('Annotation') #創(chuàng)建Annotation根節(jié)點(diǎn)ET.SubElement(root, 'filename').text = str(imgname) #創(chuàng)建filename子節(jié)點(diǎn)(無(wú)后綴)sizes = ET.SubElement(root,'size') #創(chuàng)建size子節(jié)點(diǎn) ET.SubElement(sizes, 'width').text = '1280' #沒(méi)帶腦子直接寫(xiě)了原圖片的尺寸......ET.SubElement(sizes, 'height').text = '720'ET.SubElement(sizes, 'depth').text = '3' #圖片的通道數(shù):img.shape[2]for labeldict in labeldicts:objects = ET.SubElement(root, 'object') #創(chuàng)建object子節(jié)點(diǎn)ET.SubElement(objects, 'name').text = labeldict['name'] #BDD100K_10.names文件中 #的類別名ET.SubElement(objects, 'pose').text = 'Unspecified'ET.SubElement(objects, 'truncated').text = '0'ET.SubElement(objects, 'difficult').text = '0'bndbox = ET.SubElement(objects,'bndbox')ET.SubElement(bndbox, 'xmin').text = str(int(labeldict['xmin']))ET.SubElement(bndbox, 'ymin').text = str(int(labeldict['ymin']))ET.SubElement(bndbox, 'xmax').text = str(int(labeldict['xmax']))ET.SubElement(bndbox, 'ymax').text = str(int(labeldict['ymax']))tree = ET.ElementTree(root)tree.write(filepath, encoding='utf-8')for label in labels: #批量讀.txt文件with open(labels_path + label, 'r') as f:img_id = os.path.splitext(label)[0]contents = f.readlines()labeldicts = []for content in contents:img = np.array(Image.open(img_path+label.strip('.txt') + '.jpg'))sh,sw = img.shape[0],img.shape[1] #img.shape[0]是圖片的高度720#img.shape[1]是圖片的寬度720content = content.strip('\n').split()x=float(content[1])*swy=float(content[2])*shw=float(content[3])*swh=float(content[4])*shnew_dict = {'name': classes[int(content[0])],'difficult': '0','xmin': x+1-w/2, #坐標(biāo)轉(zhuǎn)換公式看另一篇文章....'ymin': y+1-h/2,'xmax': x+1+w/2,'ymax': y+1+h/2}labeldicts.append(new_dict)write_xml(img_id, annotations_path + label.strip('.txt') + '.xml', labeldicts)需修改代碼中四個(gè)路徑:
img_path = 'D:/Yolov3_Tensorflow/LabelImg/YOLO-TXT_convert_to_PascalVOC-XML/20190822_Artificial_Flower_YOLO-TXT_convert_to_PascalVOC-XML/20190822_Artificial_Flower/'labels_path = 'D:/Yolov3_Tensorflow/LabelImg/YOLO-TXT_convert_to_PascalVOC-XML/20190822_Artificial_Flower_YOLO-TXT_convert_to_PascalVOC-XML/20190822_Artificial_Flower_txt/'annotations_path = 'D:/Yolov3_Tensorflow\LabelImg/YOLO-TXT_convert_to_PascalVOC-XML/20190822_Artificial_Flower_YOLO-TXT_convert_to_PascalVOC-XML/20190822_Artificial_Flower_xml/' clsnames_path = 'D:/Yolov3_Tensorflow/LabelImg/YOLO-TXT_convert_to_PascalVOC-XML/20190822_Artificial_Flower_YOLO-TXT_convert_to_PascalVOC-XML/obj.names'修改成與自己的路徑相對(duì)應(yīng)的即可,需注意的是路徑中要用“/”分隔符而不要用“\”分隔符,因?yàn)樗锌赡軙?huì)被識(shí)別成轉(zhuǎn)義字符。
運(yùn)行代碼
然后運(yùn)行代碼,它就會(huì)在20190822_Artificial_Flower_xml嘩啦嘩啦地生成對(duì)應(yīng)的xml文件了:
參考文章:Python將VOC數(shù)據(jù)集歸一化后的labels(.txt)文件批量轉(zhuǎn)成xml文件
程序bug(沒(méi)時(shí)間看所以還沒(méi)解決):
貌似代碼中有點(diǎn)bug,計(jì)算出來(lái)的坐標(biāo)數(shù)值比用LabelImg轉(zhuǎn)換出來(lái)的值大1。如圖框選部分實(shí)際應(yīng)為[563, 495, 696, 618]
這是程序生成的:
這是用LabelImg轉(zhuǎn)換得到的:
總結(jié)
以上是生活随笔為你收集整理的python 将YOLO(txt)格式的标注数据批量转换为PascalVOC(XML)格式的标注数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python 将PascalVOC(XM
- 下一篇: YunYang1994/tensorfl