簡體   English   中英

如何將 append 多個 xml 文件合二為一

[英]How to append multiple xml files into one

我想要 append 的第一個文件是來自LabelImg的圖像注釋。 有沒有辦法可以 append 兩個 XML 文件? 我需要 append 這些文件將它們轉換為 CSV 文件,該文件將用於我的 object 檢測的交叉驗證。

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

Output 文件

<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 個文件。

請參見下文(1.xml 和 2.xml 是取自問題的示例)。

這個想法是加載 2 個文件並將它們嵌套在一個公共根目錄下。

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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM