Python版(.txt)文件转(.xml)文件,批量转换

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/")

  • 23
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以使用readFileToXML模块中的readFileToXML类来将txt文件转换xml文件。首先,你需要在Linux上运行脚本,所以你需要修改调用方式。在代码中,你可以看到通过sys.argv获取命令行输入的文件路径,如果没有输入参数,则提示用户输入文件路径。然后,你可以实例化readFileToXML类,并传入文件路径作为参数。最后,调用makeXML方法将txt文件转换xml文件。\[1\] 如果你想调试代码,你可以使用调试代码部分中的代码。你可以实例化readFileToXML类,并传入文件路径作为参数。然后,你可以调用makeXML方法将txt文件转换xml文件。你还可以打印JSON格式和XML格式的数据,以及路径和文件名。\[2\] 对于转换的具体格式,你可以参考引用\[3\]中给出的示例。每个txt文件中的数据表示一个类别,包括xmin、ymin、w和h。你需要将这些数据转换为对应的xml格式。 #### 引用[.reference_title] - *1* *2* [使用PythonTXT文本内容读取后生成指定XML格式的文件](https://blog.csdn.net/weixin_34104341/article/details/91746431)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [python 批量txtxml](https://blog.csdn.net/m0_46529214/article/details/124495369)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值