xml文件转成yolo中的txt文件
xml文件转成yolo中的txt文件
# coding=utf-8import os
import xml.dom.minidom
import cv2 as cvdef xml_to_txt(indir, outdir):os.chdir(indir)xmls = os.listdir('.')for i, file in enumerate(xmls):file_save = file.split('.')[0] + '.txt'file_txt = os.path.join(outdir, file_save)f_w = open(file_txt, 'w')# actual parsingDOMTree = xml.dom.minidom.parse(file)annotation = DOMTree.documentElementfilename = annotation.getElementsByTagName("path")[0]imgname = filename.childNodes[0].dataimg_temp = imgname.split('\\')[-1]img_temp = os.path.join(image_dir, img_temp)image = cv.imread(imgname)
# cv.imwrite(img_temp, image)objects = annotation.getElementsByTagName("object")print(file)for object in objects:bbox = object.getElementsByTagName("robndbox")[0]cx = bbox.getElementsByTagName("cx")[0]x = float(cx.childNodes[0].data)print(x)cy = bbox.getElementsByTagName("cy")[0]y = float(cy.childNodes[0].data)print(y)cw = bbox.getElementsByTagName("w")[0]w = float(cw.childNodes[0].data)print(w)ch = bbox.getElementsByTagName("h")[0]h = float(ch.childNodes[0].data)print(h)cangel = bbox.getElementsByTagName("angle")[0]angle = float(cangel.childNodes[0].data)print(angle)cname = object.getElementsByTagName("name")[0]name = cname.childNodes[0].dataprint(name)x1 = x - w / 2.y1 = y - h / 2.x2 = x + w / 2.y2 = y - h / 2.x3 = x + w / 2.y3 = y + h / 2.x4 = x - w / 2.y4 = y + h / 2.temp = str(x1) + ' ' + str(y1) + ' ' + str(x2) + ' ' + str(y2) + ' ' + str(x3) + ' ' + str(y3) + ' ' + str(x4) + ' ' + str(y4) + ' ' + name + '\n'f_w.write(temp)f_w.close()image_dir = 'D:/img' #img目录
indir = 'D:/xml' # xml目录
outdir = 'D:/vtal' # txt目录
xml_to_txt(indir, outdir)