当前位置: 首页 > article >正文

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

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)

结果如下:

<?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>

http://www.kler.cn/a/465326.html

相关文章:

  • 电脑里msvcr120.dll文件丢失怎样修复?
  • Elasticsearch与数据库数据一致性:最佳实践与解决方案
  • 基于物联网疫苗冷链物流监测系统设计
  • 基于springboot的课程作业管理系统(源码+数据库+文档)
  • Tableau数据可视化与仪表盘搭建-安装教程
  • SQL把字符串按逗号分割成记录
  • [python SQLAlchemy数据库操作入门]-21.SQLAlchemy Session生命周期管理:保持数据持久化
  • 网络安全 | 防护层次:从物理到应用的多重保障
  • 【2025年最新】OpenWrt 更换国内源的指南(图形界面版)
  • C# OpenCV机器视觉:非刚性物体跟踪
  • 算法基础五:归并排序
  • 边沿检测电路漏检原因分析
  • Ubuntu--安装搜狗输入法
  • pip下载包出现SSLError
  • 面试提问:Redis为什么快?
  • 使用MediaPipe Face Mesh 面部动作检测
  • ElasticSearch备考 -- 整体脉络梳理
  • 【SQL】进阶知识 -- SQL创建表的几种方法
  • 影刀进阶指令 | Kimi (对标ChatGPT)
  • 通过爬虫方式实现视频号助手发布视频
  • GICv2与GICv3中断架构对比与LPI中断机制分析
  • 对45家“AI+安全”产品/方案的分析
  • Linux之ARM(MX6U)裸机篇----5.仿stm32的LED驱动实验
  • 国产数据库OceanBase从入门到放弃教程
  • Web3对跨境支付系统的潜在影响与发展前景
  • Elasticsearch向量检索需要的数据集以及768维向量生成