ETH行人检测数据集,annotation转换xml

import glob
import os
from shutil import copy
import xml.dom.minidom as minidom

with open("F:/pedestrian/seq03-img-left.idl") as file_obj:
    for line in file_obj:
        line_break = line.strip(";\n").split(":")
        img_name = line_break[0].strip("\"").strip("left/")
        if len(line_break) == 2:

            doc = minidom.Document()##在内存中创建一个空的xml 文档
            root = doc.createElement("annotation")
            doc.appendChild(root)

    #                 filename
            filename_node = doc.createElement("filename")
            filename_value = doc.createTextNode(img_name)
            filename_node.appendChild(filename_value)
            root.appendChild(filename_node)
    #                       size
            size_node = doc.createElement("size")
            width_node = doc.createElement("width")
            width_value = doc.createTextNode("640")
            width_node.appendChild(width_value)
            height_node = doc.createElement("height")
            height_value = doc.createTextNode("480")
            height_node.appendChild(height_value)
            depth_node = doc.createElement("depth")
            depth_value = doc.createTextNode("3")
            depth_node.appendChild(depth_value)
            size_node.appendChild(width_node)
            size_node.appendChild(height_node)
            size_node.appendChild(depth_node)
            root.appendChild(size_node)

            people_position = line_break[1].replace(" ", '')
            people_position = people_position.split(")")
            del people_position[-1]
            for single in people_position:
                single = single.strip(",(")
                single = single.split(',')
                print(single)
     #                        object
                object_node = doc.createElement("object")
                name_node = doc.createElement("name")
                name_value = doc.createTextNode("person")
                name_node.appendChild(name_value)
                object_node.appendChild(name_node)

                bndbox_node = doc.createElement("bndbox")
                xmin_node = doc.createElement("xmin")
                xmin_value = doc.createTextNode(single[0])
                xmin_node.appendChild(xmin_value)
                xmax_node = doc.createElement("xmax")
                xmax_value = doc.createTextNode(single[2])
                xmax_node.appendChild(xmax_value)
                ymin_node = doc.createElement("ymin")
                ymin_value = doc.createTextNode(single[1])
                ymin_node.appendChild(ymin_value)
                ymax_node = doc.createElement("ymax")
                ymax_value = doc.createTextNode(single[3])
                ymax_node.appendChild(ymax_value)
                bndbox_node.appendChild(xmin_node)
                bndbox_node.appendChild(xmax_node)
                bndbox_node.appendChild(ymin_node)
                bndbox_node.appendChild(ymax_node)
                object_node.appendChild(bndbox_node)

                root.appendChild(object_node)

                img_name = img_name.strip(".png")
                path = os.path.join("F:/pedestrian/seq03-img-left",img_name) + ".xml"

                with open(path, 'w') as f:
                    # 缩进 - 换行 - 编码
                    doc.writexml(f, addindent='    ', newl='\n')

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值