资源简介

压缩文件中对的代码UA-DETRAC数据集中的xml进行了转换,转换成标准的voc中xml格式。代码运行无误

资源截图

代码片段和文件信息

import xml.etree.ElementTree as ET
from xml.dom.minidom import Document
import os
import cv2
import time


def ConvertVOCxml(file_path=““ file_name=““):
    tree = ET.parse(file_name)
    root = tree.getroot()
    # print(root.tag)

    num = 0  # 计数
    # 读xml操作

    frame_lists = []
    output_file_name = ““
    for child in root:

        if (child.tag == “frame“):
            # 创建dom文档
            doc = Document()
            # 创建根节点
            annotation = doc.createElement(‘annotation‘)
            # 根节点插入dom树
            doc.appendChild(annotation)

            # print(child.tag child.attrib[“num“])
            pic_id = child.attrib[“num“].zfill(5)
            # print(pic_id)
            output_file_name = root.attrib[“name“] + “__img“ + pic_id + “.xml“
            #  print(output_file_name)

            folder = doc.createElement(“folder“)
            folder.appendChild(doc.createTextNode(“VOC2007“))
            annotation.appendChild(folder)

            filename = doc.createElement(“filename“)
            pic_name = “img“ + pic_id + “.jpg“
            filename.appendChild(doc.createTextNode(pic_name))
            annotation.appendChild(filename)

            sizeimage = doc.createElement(“size“)
            imagewidth = doc.createElement(“width“)
            imageheight = doc.createElement(“height“)
            imagedepth = doc.createElement(“depth“)

            imagewidth.appendChild(doc.createTextNode(“960“))
            imageheight.appendChild(doc.createTextNode(“540“))
            imagedepth.appendChild(doc.createTextNode(“3“))

            sizeimage.appendChild(imagedepth)
            sizeimage.appendChild(imagewidth)
            sizeimage.appendChild(imageheight)
            annotation.appendChild(sizeimage)

            target_list = child.getchildren()[0]  # 获取target_list
            # print(target_list.tag)
            object = None
            for target in target_list:
                if (target.tag == “target“):
                    # print(target.tag)
                    object = doc.createElement(‘object‘)
                    bndbox = doc.createElement(“bndbox“)

                    for target_child in target:
                        if (target_child.tag == “box“):
                            xmin = doc.createElement(“xmin“)
                            ymin = doc.createElement(“ymin“)
                            xmax = doc.createElement(“xmax“)
                            ymax = doc.createElement(“ymax“)
                            xmin_value = int(float(target_child.attrib[“left“]))
                            ymin_value = int(float(target_child.attrib[“top“]))
                            box_width_value = int(float(target_child.attrib[“width“]))
                            box_height_value = int(float(target_child.attrib[“height“]))
                            xmin.appendChild(doc.createTextNode(str(xmin_value)))
                            ym

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件         930  2019-11-08 10:05  voc_data_migrate.py
     文件        8732  2019-11-09 11:51  covert.py

评论

共有 条评论