数据篇---用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>
相关推荐
山沐与山1 分钟前
【Python】深入理解Python Web框架:从Flask到FastAPI的并发之路
python·flask·fastapi
嫂子的姐夫2 分钟前
011-AES:深证信
爬虫·python·逆向
海奥华22 分钟前
Golang Channel 原理深度解析
服务器·开发语言·网络·数据结构·算法·golang
代码游侠4 分钟前
学习笔记——MQTT协议
开发语言·笔记·php
love530love4 分钟前
Flash Attention 2.8.3 在 Windows + RTX 3090 上成功编译与运行复盘笔记(2026年1月版)
人工智能·windows·笔记·python·flash_attn·flash attention·z-image
odoo中国5 分钟前
如何在 Odoo 中从 XML 文件调用函数
xml·odoo·odoo开发·调用函数
渡我白衣10 分钟前
计算机组成原理(13):多路选择器与三态门
开发语言·javascript·ecmascript·数字电路·计算机组成原理·三态门·多路选择器
@zulnger10 分钟前
python 学习笔记(异常对象)
笔记·python·学习
HUST11 分钟前
C语言 第十讲:操作符详解
c语言·开发语言
No0d1es15 分钟前
2025年12月 GESP CCF编程能力等级认证Python七级真题
python·青少年编程·gesp·ccf