1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > TXT文件转Pascal voc数据集XML格式标注文件

TXT文件转Pascal voc数据集XML格式标注文件

时间:2023-10-10 20:05:55

相关推荐

TXT文件转Pascal voc数据集XML格式标注文件

TXT的文件格式为 wmin,hmin,wmax,hmax

最终转换出来的效果为

代码:

from xml.dom import minidomimport cv2txt_dirtory=r'F:\9.txt'jpg_dirtory=r'F:\9.jpg'img_name=jpg_dirtory.split('\\')[-1]floder=jpg_dirtory.split('\\')[-2]im = cv2.imread(jpg_dirtory)w = im.shape[1]h = im.shape[0]d = im.shape[2]# print w,h,ddoc = minidom.Document() #创建DOM树对象annotation = doc.createElement('annotation') #创建子节点doc.appendChild(annotation) #annotation作为doc树的子节点folder = doc.createElement('folder')folder.appendChild(doc.createTextNode(floder)) #文本节点作为floder的子节点annotation.appendChild(folder) #folder作为annotation的子节点filename = doc.createElement('filename')filename.appendChild(doc.createTextNode(img_name))annotation.appendChild(filename)filename = doc.createElement('path')filename.appendChild(doc.createTextNode(jpg_dirtory))annotation.appendChild(filename)source = doc.createElement('source')database = doc.createElement('database')database.appendChild(doc.createTextNode("Unknown"))source.appendChild(database)# annotation2 = doc.createElement('annotation')# annotation2.appendChild(doc.createTextNode("ICDAR POD"))# source.appendChild(annotation2)# image = doc.createElement('image')# image.appendChild(doc.createTextNode("image"))# source.appendChild(image)# flickrid = doc.createElement('flickrid')# flickrid.appendChild(doc.createTextNode("NULL"))# source.appendChild(flickrid)annotation.appendChild(source)# owner = doc.createElement('owner')# flickrid = doc.createElement('flickrid')# flickrid.appendChild(doc.createTextNode("NULL"))# owner.appendChild(flickrid)# na = doc.createElement('name')# na.appendChild(doc.createTextNode("cxm"))# owner.appendChild(na)# annotation.appendChild(owner)size = doc.createElement('size')width = doc.createElement('width')width.appendChild(doc.createTextNode("%d" % w))size.appendChild(width)height = doc.createElement('height')height.appendChild(doc.createTextNode("%d" % h))size.appendChild(height)depth = doc.createElement('depth')depth.appendChild(doc.createTextNode("%d" % d))size.appendChild(depth)annotation.appendChild(size)segmented = doc.createElement('segmented')segmented.appendChild(doc.createTextNode("0"))annotation.appendChild(segmented)txtLabel = open(txt_dirtory, 'r')boxes = [txtLabel.readline()]for box in boxes:# print(box)box = box.split(' ')# print(box)object = doc.createElement('object')nm = doc.createElement('name')nm.appendChild(doc.createTextNode('0'))object.appendChild(nm)pose = doc.createElement('pose')pose.appendChild(doc.createTextNode("Unspecified"))object.appendChild(pose)truncated = doc.createElement('truncated')truncated.appendChild(doc.createTextNode("1"))object.appendChild(truncated)difficult = doc.createElement('difficult')difficult.appendChild(doc.createTextNode("0"))object.appendChild(difficult)bndbox = doc.createElement('bndbox')xmin = doc.createElement('xmin')xmin.appendChild(doc.createTextNode(box[0]))bndbox.appendChild(xmin)ymin = doc.createElement('ymin')ymin.appendChild(doc.createTextNode(box[1]))bndbox.appendChild(ymin)xmax = doc.createElement('xmax')xmax.appendChild(doc.createTextNode(box[2]))bndbox.appendChild(xmax)ymax = doc.createElement('ymax')ymax.appendChild(doc.createTextNode(box[3]))bndbox.appendChild(ymax)object.appendChild(bndbox)annotation.appendChild(object)savefile = open(r'F:\9.xml', 'w')savefile.write(doc.toprettyxml())savefile.close()

参考内容:

txt版本的标签转换成PASCAL VOC 格式的XML标签

Python minidom模块(DOM写入和解析XML)

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