数据篇---用python创建想要的xml

python 复制代码
import os
import xml.etree.ElementTree as ET


def create_xml(xml_filename):
    # 创建根节点
    annotation = ET.Element("annotation")

    # 添加子节点
    folder = ET.SubElement(annotation, "folder")
    folder.text = "img"

    filename = ET.SubElement(annotation, "filename")
    filename.text = xml_filename

    path = ET.SubElement(annotation, "path")
    path.text = "/workspace/img/" + xml_filename

    source = ET.SubElement(annotation, "source")
    database = ET.SubElement(source, "database")
    database.text = "Unknown"

    size = ET.SubElement(annotation, "size")
    width = ET.SubElement(size, "width")
    width.text = "1920"
    height = ET.SubElement(size, "height")
    height.text = "1080"
    depth = ET.SubElement(size, "depth")
    depth.text = "3"

    segmented = ET.SubElement(annotation, "segmented")
    segmented.text = "0"

    obj = ET.SubElement(annotation, "object")
    name = ET.SubElement(obj, "name")
    name.text = "cover_plate"
    pose = ET.SubElement(obj, "pose")
    pose.text = "Unspecified"
    truncated = ET.SubElement(obj, "truncated")
    truncated.text = "0"
    difficult = ET.SubElement(obj, "difficult")
    difficult.text = "0"
    bndbox = ET.SubElement(obj, "bndbox")
    xmin = ET.SubElement(bndbox, "xmin")
    xmin.text = "0"
    ymin = ET.SubElement(bndbox, "ymin")
    ymin.text = "0"
    xmax = ET.SubElement(bndbox, "xmax")
    xmax.text = "1920"
    ymax = ET.SubElement(bndbox, "ymax")
    ymax.text = "1080"

    # 创建树结构并写入文件
    tree = ET.ElementTree(annotation)
    output_name = os.path.join("/app/xml", xml_filename[:-4] + ".xml")
    tree.write(output_name, encoding='utf-8', xml_declaration=True)


if __name__ == '__main__':
    img_path = "/app/img"
    img_path_all = os.listdir(img_path)
    print(img_path_all)

    for i in img_path_all:
        create_xml(i)

结果如下:

python 复制代码
<?xml version='1.0' encoding='utf-8'?>
<annotation>
    <folder>img</folder>
    <filename>192.168.60.110_20241230053729_[R][0@0][0].jpg</filename>
    <path>/workspace/img/192.168.60.110_20241230053729_[R][0@0][0].jpg</path>
    <source>
        <database>Unknown</database>
    </source>
    <size>
        <width>1920</width>
        <height>1080</height>
        <depth>3</depth>
    </size>
    <segmented>0</segmented>
    <object>
        <name>cover_plate</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>0</xmin>
            <ymin>0</ymin>
            <xmax>1920</xmax>
            <ymax>1080</ymax>
        </bndbox>
    </object>
</annotation>
相关推荐
Sunsets_Red1 分钟前
差分操作正确性证明
java·c语言·c++·python·算法·c#
APIshop17 分钟前
代码实例:Python 爬虫抓取与解析 JSON 数据
爬虫·python·json
伐尘21 分钟前
【Qt】QTableWidget 自定义排序功能实现
开发语言·qt
程序员爱钓鱼29 分钟前
Python编程实战 · 基础入门篇 | Python的版本与安装
后端·python
hmbbcsm40 分钟前
练习python题目小记
python
多多*1 小时前
上传文件相关业务,采用策略模式+模版方法模式进行动态解耦
java·开发语言
ZhengEnCi1 小时前
CMD 与 Python 完全区别指南-小白必看的编程入门解惑宝典
windows·python
Hs_QY_FX1 小时前
逻辑回归实战:泰坦尼克号生存预测
python·逻辑回归
赴前尘1 小时前
Go 通道非阻塞发送:优雅地处理“通道已满”的场景
开发语言·后端·golang
weixin_456904272 小时前
以太网与工业以太网通信C#开发
开发语言·c#