数据篇---用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>
相关推荐
执笔论英雄17 分钟前
Slime异步原理(单例设计模式)4
开发语言·python·设计模式
e***74952 小时前
Modbus报文详解
服务器·开发语言·php
lly2024062 小时前
ASP 发送电子邮件详解
开发语言
小徐敲java2 小时前
python使用s7协议与plc进行数据通讯(HslCommunication模拟)
开发语言·python
likuolei2 小时前
XSL-FO 软件
java·开发语言·前端·数据库
6***37942 小时前
PHP在电商中的BigCommerce
开发语言·php
猫头虎2 小时前
如何解决 pip install 编译报错 fatal error: hdf5.h: No such file or directory(h5py)问题
人工智能·python·pycharm·开源·beautifulsoup·ai编程·pip
Dev7z2 小时前
基于Matlab的多制式条形码识别与图形界面(GUI)系统设计与实现
开发语言·matlab
合作小小程序员小小店2 小时前
桌面开发,在线%信息管理%系统,基于vs2022,c#,winform,sql server数据。
开发语言·数据库·sql·microsoft·c#
FL16238631292 小时前
ONNX RuntimeC++ 静态库下载安装和使用教程
开发语言·c++