数据篇---用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>
相关推荐
CharlesC++6 分钟前
JAVA类和对象练习
java·开发语言
编码浪子8 分钟前
进军AI大模型-Langchain程序部署
linux·python·langchain
huluang9 分钟前
构建JS全栈开发的CMS系统——从零开始搭建前后端
开发语言·javascript·ecmascript
北极糊的狐12 分钟前
vue2框架配置路由设计打印单
开发语言·前端·javascript
远方2.019 分钟前
使用cursor完成飞机大战
python·pygame
半夏知半秋35 分钟前
Python中的OS库
服务器·开发语言·笔记·后端·python·学习
大数据魔法师37 分钟前
Python爬虫 - 豆瓣图书数据爬取、处理与存储
爬虫·python
糯诺诺米团43 分钟前
Qt|QWidget窗口支持旋转
开发语言·qt
sonnet001 小时前
偏振测量——典型光学元件的穆勒矩阵
python
一只小萌新.1 小时前
【Python学习(六)——While、for、循环控制、指数爆炸】
开发语言·python·学习