如何将多个xml文件附加到一个xml文件中

2024-10-03 21:28:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我要附加的第一个文件是来自LabelImg的图像注释。有没有办法附加两个XML文件?我需要附加这些文件,将它们转换为CSV文件,用于交叉验证以进行对象检测

<annotation>
    <folder>business center</folder>
    <filename>bcenter (1).PNG</filename>
    <path>D:\RealTimeObjectDetection\Tensorflow\workspace\images\business center\bcenter (1).PNG</path>
    <source>
        <database>Unknown</database>
    </source>
    <size>
        <width>1815</width>
        <height>861</height>
        <depth>3</depth>
    </size>
    <segmented>0</segmented>
    <object>
        <name>business_center</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>488</xmin>
            <ymin>148</ymin>
            <xmax>980</xmax>
            <ymax>533</ymax>
        </bndbox>
    </object>
</annotation>

第二档

<annotation>
    <folder>business center</folder>
    <filename>bcenter (2).PNG</filename>
    <path>D:\RealTimeObjectDetection\Tensorflow\workspace\images\business center\bcenter (2).PNG</path>
    <source>
        <database>Unknown</database>
    </source>
    <size>
        <width>1851</width>
        <height>887</height>
        <depth>3</depth>
    </size>
    <segmented>0</segmented>
    <object>
        <name>business_center</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>805</xmin>
            <ymin>126</ymin>
            <xmax>1173</xmax>
            <ymax>513</ymax>
        </bndbox>
    </object>
</annotation>

输出文件

<annotation>
    <folder>business center</folder>
    <filename>bcenter (1).PNG</filename>
    <path>D:\RealTimeObjectDetection\Tensorflow\workspace\images\business center\bcenter (1).PNG</path>
    <source>
        <database>Unknown</database>
    </source>
    <size>
        <width>1815</width>
        <height>861</height>
        <depth>3</depth>
    </size>
    <segmented>0</segmented>
    <object>
        <name>business_center</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>488</xmin>
            <ymin>148</ymin>
            <xmax>980</xmax>
            <ymax>533</ymax>
        </bndbox>
    </object>
</annotation>
<annotation>
    <folder>business center</folder>
    <filename>bcenter (2).PNG</filename>
    <path>D:\RealTimeObjectDetection\Tensorflow\workspace\images\business center\bcenter (2).PNG</path>
    <source>
        <database>Unknown</database>
    </source>
    <size>
        <width>1851</width>
        <height>887</height>
        <depth>3</depth>
    </size>
    <segmented>0</segmented>
    <object>
        <name>business_center</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>805</xmin>
            <ymin>126</ymin>
            <xmax>1173</xmax>
            <ymax>513</ymax>
        </bndbox>
    </object>
</annotation>

是否有一个软件或代码可以使流程自动化?我有大约1405个文件


Tags: pathsourcesizepngannotationbusinessfolderfilename
1条回答
网友
1楼 · 发布于 2024-10-03 21:28:48

见下文(1.xml和2.xml是从问题中选取的示例)

想法是加载这两个文件并将它们嵌套在一个公共根目录下

import xml.etree.ElementTree as ET

root1 = ET.parse('1.xml').getroot()
root2 = ET.parse('2.xml').getroot()

root = ET.Element('annotations')
root.append(root1)
root.append(root2)
ET.dump(root)

输出

<?xml version="1.0" encoding="UTF-8"?>
<annotations>
   <annotation>
      <folder>business center</folder>
      <filename>bcenter (1).PNG</filename>
      <path>D:\RealTimeObjectDetection\Tensorflow\workspace\images\business center\bcenter (1).PNG</path>
      <source>
         <database>Unknown</database>
      </source>
      <size>
         <width>1815</width>
         <height>861</height>
         <depth>3</depth>
      </size>
      <segmented>0</segmented>
      <object>
         <name>business_center</name>
         <pose>Unspecified</pose>
         <truncated>0</truncated>
         <difficult>0</difficult>
         <bndbox>
            <xmin>488</xmin>
            <ymin>148</ymin>
            <xmax>980</xmax>
            <ymax>533</ymax>
         </bndbox>
      </object>
   </annotation>
   <annotation>
      <folder>business center</folder>
      <filename>bcenter (2).PNG</filename>
      <path>D:\RealTimeObjectDetection\Tensorflow\workspace\images\business center\bcenter (2).PNG</path>
      <source>
         <database>Unknown</database>
      </source>
      <size>
         <width>1851</width>
         <height>887</height>
         <depth>3</depth>
      </size>
      <segmented>0</segmented>
      <object>
         <name>business_center</name>
         <pose>Unspecified</pose>
         <truncated>0</truncated>
         <difficult>0</difficult>
         <bndbox>
            <xmin>805</xmin>
            <ymin>126</ymin>
            <xmax>1173</xmax>
            <ymax>513</ymax>
         </bndbox>
      </object>
   </annotation>
</annotations>

相关问题 更多 >