简体   繁体   中英

How to append multiple xml files into one

The first file that I want to append, is an image annotation from LabelImg . Is there a way I can append two XML files? I need to append these files to convert them into a CSV file that will be used in cross-validation for my object detection.

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

Second File

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

Output File

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

Is there a software or code I can automate the process? I have around 1405 files.

See below (1.xml and 2.xml are the examples taken from the question).

The idea is to load the 2 files and nest them under a common root.

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)

output

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM