labelimg标注软件中的txt文件转换成xml文件。
txt文件。

xml文件。

代码
from xml.dom.minidom import Document
import os
import cv2
#转载https://zhuanlan.zhihu.com/p/58392978
def makexml(txtPath, xmlPath, picPath): # 读取txt路径,xml保存路径,数据集图片所在路径
dict = {'0': "B2", # 字典对类型进行转换
'1': "B3",
'2': "B1",
'3': "3",
'4': "2",
'5': "C2",
'6': "D2",
'7': "C3",
'8': "h",
'9': "D3",
}
files = os.listdir(txtPath)
for i, name in enumerate(files):
xmlBuilder = Document()
# 创建annotation
annotation = xmlBuilder.createElement("annotation") # 创建annotation标签
xmlBuilder.appendChild(annotation)
txtFile = open(txtPath + name)
txtList = txtFile.readlines()
# 创建folder
folder = xmlBuilder.createElement('folder')
folder_text = xmlBuilder.createTextNode('ee')
folder.appendChild(folder_text)
annotation.appendChild(folder)
# 创建
filename = xmlBuilder.createElement("filename") # filename标签
filenameContent = xmlBuilder.createTextNode(name[0:-4] + ".jpg")
filename.appendChild(filenameContent)
annotation.appendChild(filename)
# 创建
path = xmlBuilder.createElement('path')
path_text = xmlBuilder.createTextNode('path is null')
path.appendChild(path_text)
annotation.appendChild(path)
# 创建
source = xmlBuilder.createElement('source')
databass = xmlBuilder.createElement('databass')
databass_text = xmlBuilder.createTextNode('Unknown')
source.appendChild(databass)
databass.appendChild(databass_text)
annotation.appendChild(source)
# 创建
size = xmlBuilder.createElement('size')
width = xmlBuilder.createElement('width')
width_text = xmlBuilder.createTextNode('875')
height = xmlBuilder.createElement('height')
# 创建
height_text = xmlBuilder.createTextNode('656')
depth = xmlBuilder.createElement('depth')
depth_text = xmlBuilder.createTextNode('1')
size.appendChild(width)
width.appendChild(width_text)
size.appendChild(height)
height.appendChild(height_text)
size.appendChild(depth)
depth.appendChild(depth_text)
annotation.appendChild(size)
# 创建
segmented = xmlBuilder.createElement('segmented')
segmented_text = xmlBuilder.createTextNode('0')
segmented.appendChild(segmented_text)
annotation.appendChild(segmented)
# 创建
img = cv2.imread(picPath + name[0:-4] + ".jpg")
Pheight, Pwidth, Pdepth = img.shape
for i in txtList:
oneline = i.strip().split(" ")
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)
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")
#坐标计算过程
print(oneline[4])
a=float(oneline[4])*Pheight #y_max-y_min
b=float(oneline[2])*Pheight*2+1 #(y_min+y_max)
y_max=float((a+b)/2) #y_max
c=float(oneline[3]) *Pwidth #x_max-x_min
d=float(oneline[1])*Pwidth*2+1 #x_min+x_max
x_max=(c+d)/2
y_min=y_max-float(oneline[4])*Pheight
x_min=x_max-float(oneline[3])*Pwidth
xmin = xmlBuilder.createElement("xmin")
mathData = int(x_min)
#xmin
xminContent = xmlBuilder.createTextNode(str(mathData))
xmin.appendChild(xminContent)
bndbox.appendChild(xmin)
#ymin
ymin = xmlBuilder.createElement("ymin")
mathData = int(y_min)
yminContent = xmlBuilder.createTextNode(str(mathData))
ymin.appendChild(yminContent)
bndbox.appendChild(ymin)
#xmax
xmax = xmlBuilder.createElement("xmax")
mathData = int(x_max)
xmaxContent = xmlBuilder.createTextNode(str(mathData))
xmax.appendChild(xmaxContent)
bndbox.appendChild(xmax)
#ymax
ymax = xmlBuilder.createElement("ymax")
mathData = int(y_max)
ymaxContent = xmlBuilder.createTextNode(str(mathData))
ymax.appendChild(ymaxContent)
bndbox.appendChild(ymax)
object.appendChild(bndbox)
annotation.appendChild(object)
'''
#另一种坐标转换计算方式 输出结果有一像素的差异
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)
'''
f = open(xmlPath + name[0:-4] + ".xml", 'w')
xmlBuilder.writexml(f, addindent='\t', newl='\n',encoding='utf-8')
f.close()
makexml("E:/test/txt/", "E:/test/xml/", "E:/test/img/")
本文详细介绍了如何使用Python在LabelImg标注软件中将txt文件转换为xml文件,包括读取txt路径、构建xml结构和坐标计算方法。
文件转(.xml)文件,批量转换&spm=1001.2101.3001.5002&articleId=134704164&d=1&t=3&u=eb4bc072e4954b0587f6534fd42cfab0)
3万+

被折叠的 条评论
为什么被折叠?



