赞
踩
1、xml转txt
怎么运行和得到结果(小白式教学):
在桌面新建一个xml2txt.py文件,输入以下代码,把classes和输入和输出路径改了,右键->运行python->在终端中运行python文件(我用的是VScode)即可。
(如果报错:['VOCdevkit\\VOC2007/JPEGImages\\0.bmp', 'company', '640', '640', 'black stain', '258', '150', '287', '185', 'broken', '175', '243', '206', '282', 'broken', '319', '234', '341', '275', 'broken', '228', '236', '251', '284', 'broken', '362', '232', '384', '272', 'black stain', '287', '348', '308', '364']
Traceback (most recent call last):
File "c:/Users/DELL/Desktop/xml2txt/xml2txt.py", line 52, in <module>
dw=1/int(width)
ValueError: invalid literal for int() with base 10: 'company'
解决方法:搜索所有文件,将以下代码替换为空即可
- <owner>
- <flickrid>NULL</flickrid>
- <name>company</name>
- </owner>
)
- # 以东北大学钢铁数据集标签为例
- import xml.etree.ElementTree as ET#xml 是python自带的package
- import os
-
-
-
- classes=['crazing','inclusion','patches','pitted_surface','rolled-in_scale','scratches']#写自己的分类名
- pre_dir=r'C:\Users\Bin\Desktop\Deeplearning\NEU surface defect database\labels'#xml文件所在文件夹
- target_dir=r'C:\Users\Bin\Desktop\Deeplearning\NEU surface defect database\labeltxt'#想要存储txt文件的文件夹
- path=os.listdir(pre_dir)
-
- for path1 in path:
- # path1=r'C:\Users\loadlicb\Desktop\chrome_RTJOXXsYHM.xml'#xml文件路径
- tree=ET.parse(os.path.join(pre_dir,path1))
- root=tree.getroot()#这两个步骤将xml文件拆出来了
- oo=[]
- for child in root:
- if child.tag == 'filename':#tag对应的是《》中的内容,text对应的是两个《》中间的部分内容
- oo.append(child.text)#获得xml文件的名
- # print(child.text)
- for i in child:
-
- if i.tag == 'width':#获得图片的w
- oo.append(i.text)
- # print(i.text)
- if i.tag == 'height':#获得图片的h
- oo.append(i.text)
- # print(i.text)
- if i.tag == 'name':#获得当前框的class
- oo.append(i.text)
- # print(i.text)
-
- for j in i:
- if j.tag == 'xmin':#获得当前框的两个对角线上的点的两组坐标
- oo.append(j.text)
- # print(j.text)
- if j.tag == 'ymin':
- oo.append(j.text)
- # print(j.text)
- if j.tag == 'xmax':
- oo.append(j.text)
- # print(j.text)
- if j.tag == 'ymax':
- oo.append(j.text)
- # print(j.text)
- print(oo)
- filename=oo[0]#读取图片的名和宽高
- filename=os.path.split(filename)
- # print(filename)
- name,extension=os.path.splitext(filename[1])#获取xml名和后缀
- width=oo[1]
- dw=1/int(width)
- height=oo[2]
- dh=1/int(height)
- oo.pop(0)
- oo.pop(0)
- oo.pop(0)#删除三次oolist的0号元素
-
-
- back=[]
- # print((len(oo))%5)
- for i in range(len(oo)//5):
- for p in range(len(classes)):#划定class的序号
- if classes[p] == oo[5*i]:#str == str
- cl=p
- back.append(cl)
- x=(int(oo[5*i+1])+int(oo[5*i+3]))/2#oo里的所有元素都是str,数字也是
- y = (int(oo[5 * i + 2]) + int(oo[5 * i + 4])) / 2#计算标注框的中心点的xy坐标
- w=int(oo[5*i+3])-int(oo[5*i+1])
- h=int(oo[5*i+4])-int(oo[5*i+2])#计算标注框的宽高
- back.append('{:.4f}'.format(x*dw))
- back.append('{:.4f}'.format(y * dh))
- back.append('{:.4f}'.format(w * dw))
- back.append('{:.4f}'.format(h * dh))
- # back.append(y*dh)
- # back.append(w*dw)
- # back.append(h*dh)#转换到0-1区间
- print(back)
- # dir=r'C:\Users\loadlicb\Desktop'#label文件夹名
- file=open(os.path.join(target_dir,name+'.txt'),'w')
- for i in range(len(back)):
- l=' '
- if (i+1)%5==0:
- l='\n'
- file.writelines(str(back[i])+l)#完成了,现在进行批量操作修改
- #完成
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
2、txt转xml
怎么运行和得到结果(小白式教学):
在桌面建立一个文件夹名字为txt2xml, 里面又三个文件夹和一个.py文件,它们分别是:picture、txt、xml、txt2xml.py
txt2xml.py文件中输入以下代码:
- # 将txt格式转换成xml格式数据集
- from xml.dom.minidom import Document
- import os
- import cv2
-
-
- def makexml(picPath, txtPath, xmlPath): # txt所在文件夹路径,xml文件保存路径,图片所在文件夹路径
- """此函数用于将yolo格式txt标注文件转换为voc格式xml标注文件
- 在自己的标注图片文件夹下建三个子文件夹,分别命名为picture、txt、xml
- """
- #创建字典用来对类型进行转换,要与classes.txt文件中的类对应,且顺序要一致
- dic = {'0': "missing pin", '1': "broken", '2': "scratch", '3': "cataclasm",
- '4': "cross fracture", '5': "missing lead", '6': "black stain", '7': "foreign matter"}
-
- 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") # 注意这里的图片后缀,.jpg/.png
- Pheight, Pwidth, Pdepth = img.shape
-
- folder = xmlBuilder.createElement("folder") # folder标签
- foldercontent = xmlBuilder.createTextNode("datasetRGB")
- folder.appendChild(foldercontent)
- annotation.appendChild(folder)
-
- filename = xmlBuilder.createElement("filename") # filename标签
- filenamecontent = xmlBuilder.createTextNode(name[0:-4] + ".bmp")
- filename.appendChild(filenamecontent)
- annotation.appendChild(filename)
-
- size = xmlBuilder.createElement("size") # size标签
- width = xmlBuilder.createElement("width") # size子标签width
- widthcontent = xmlBuilder.createTextNode(str(Pwidth))
- width.appendChild(widthcontent)
- size.appendChild(width)
-
- height = xmlBuilder.createElement("height") # size子标签height
- heightcontent = xmlBuilder.createTextNode(str(Pheight))
- height.appendChild(heightcontent)
- size.appendChild(height)
-
- depth = xmlBuilder.createElement("depth") # size子标签depth
- depthcontent = xmlBuilder.createTextNode(str(Pdepth))
- depth.appendChild(depthcontent)
- size.appendChild(depth)
-
- annotation.appendChild(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)
-
- pose = xmlBuilder.createElement("pose") # pose标签
- posecontent = xmlBuilder.createTextNode("Unspecified")
- pose.appendChild(posecontent)
- object.appendChild(pose)
-
- truncated = xmlBuilder.createElement("truncated") # truncated标签
- truncatedContent = xmlBuilder.createTextNode("0")
- truncated.appendChild(truncatedContent)
- object.appendChild(truncated)
-
- difficult = xmlBuilder.createElement("difficult") # difficult标签
- difficultcontent = xmlBuilder.createTextNode("0")
- difficult.appendChild(difficultcontent)
- object.appendChild(difficult)
-
- bndbox = xmlBuilder.createElement("bndbox") # bndbox标签
- xmin = xmlBuilder.createElement("xmin") # 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") # 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") # 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") # 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) # 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()
-
-
- if __name__ == "__main__":
- picPath = "C:\\Users\\Bin\\Desktop\\txt2xml\\picture\\" # 图片所在文件夹路径,后面的\\一定要带上
- txtPath = "C:\\Users\\Bin\\Desktop\\txt2xml\\txt\\" # txt所在文件夹路径,后面的\\一定要带上
- xmlPath = "C:\\Users\\Bin\\Desktop\\txt2xml\\xml\\" # xml文件保存路径,后面的\\一定要带上
- makexml(picPath, txtPath, xmlPath)
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
.py文件做好了,文件夹也做好之后,重新打开VScode->打开文件夹->选择txt2xml文件夹->点击txt2xml.py->右键运行python->在终端中运行python文件(我用的是VScode)即可。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。