【问题标题】:Extract part of XML files in a folder提取文件夹中的部分 XML 文件
【发布时间】:2022-11-21 20:02:13
【问题描述】:

我有一个文件夹,其中包含许多图像的 Pascal Voc XML 注释。注释看起来像下面的那个

<annotation>
    <folder>images</folder>
    <filename>Norway_000000.jpg</filename>
    <size>
        <width>3650</width>
        <height>2044</height>
        <depth/>
    </size>
    <segmented>0</segmented>
    <object>
        <name>D00</name>
        <truncated>0</truncated>
        <occluded>0</occluded>
        <difficult>0</difficult>
        <bndbox>
            <xmin>1138.46</xmin>
            <ymin>1281.93</ymin>
            <xmax>1169.35</xmax>
            <ymax>1336.85</ymax>
        </bndbox>
        <attributes>
            <attribute>
                <name>rotation</name>
                <value>0.0</value>
            </attribute>
        </attributes>
    </object>
    <object>
        <name>D20</name>
        <truncated>0</truncated>
        <occluded>0</occluded>
        <difficult>0</difficult>
        <bndbox>
            <xmin>1537.53</xmin>
            <ymin>1131.79</ymin>
            <xmax>1629.06</xmax>
            <ymax>1247.64</ymax>
        </bndbox>
        <attributes>
            <attribute>
                <name>rotation</name>
                <value>0.0</value>
            </attribute>
        </attributes>
    </object>
    <object>
        <name>D00</name>
        <truncated>0</truncated>
        <occluded>0</occluded>
        <difficult>0</difficult>
        <bndbox>
            <xmin>1773.45</xmin>
            <ymin>1825.97</ymin>
            <xmax>1862.69</xmax>
            <ymax>2038.78</ymax>
        </bndbox>
        <attributes>
            <attribute>
                <name>rotation</name>
                <value>0.0</value>
            </attribute>
        </attributes>
    </object>
    <object>
        <name>D00</name>
        <truncated>0</truncated>
        <occluded>0</occluded>
        <difficult>0</difficult>
        <bndbox>
            <xmin>1589.02</xmin>
            <ymin>1296.26</ymin>
            <xmax>1624.77</xmax>
            <ymax>1343.46</ymax>
        </bndbox>
        <attributes>
            <attribute>
                <name>rotation</name>
                <value>0.0</value>
            </attribute>
            </attributes>
    </object>
    <object>
        <name>D00</name>
        <truncated>0</truncated>
        <occluded>0</occluded>
        <difficult>0</difficult>
        <bndbox>
            <xmin>1507.53</xmin>
            <ymin>1216.53</ymin>
            <xmax>1527.49</xmax>
            <ymax>1254.27</ymax>
        </bndbox>
        <attributes>
            <attribute>
                <name>rotation</name>
                <value>0.0</value>
            </attribute>
        </attributes>
    </object>
</annotation>

我只想提取以下部分并保存新的 xml 文件。

<object>
    <name>D00</name>
    <truncated>0</truncated>
    <occluded>0</occluded>
    <difficult>0</difficult>
    <bndbox>
        <xmin>1138.46</xmin>
        <ymin>1281.93</ymin>
        <xmax>1169.35</xmax>
        <ymax>1336.85</ymax>
    </bndbox>
    <attributes>
        <attribute>
            <name>rotation</name>
            <value>0.0</value>
        </attribute>
    </attributes>
</object>

除了手动删除不需要的部分外,我没有找到任何特定的资源或指南来解决这个问题。如何读取文件夹中的所有文件,仅提取所需的注释,然后保存新的 xml 文件?我需要张量流中自定义对象检测的图像。

【问题讨论】:

  • 从原始 XML 中提取特定 object 元素的标准是什么?

标签: python xml tensorflow annotations object-detection


【解决方案1】:

这是一种提取每个objects的方法。 您稍后可以在其中简单地使用 iterate 并搜索特定的 name

这是我尝试查找所有 object 元素的部分:

import xml.etree.ElementTree as ET

xml_file = ET.parse('YourXml.xml')
xml_root = xml_file.getroot()
xml_objects = list()

for i in xml_root:
    if i.tag == 'object':
        xml_objects.append(i)

在这部分中,我将 Elements 转换为 ElementTree,这让我可以将它们转换为 write.xml文件。

for iter_i, i in enumerate(xml_objects):
   ET.ElementTree(i).write(f'D:\object_{iter_i}.xml')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-14
    • 2023-01-25
    • 1970-01-01
    • 2022-10-23
    相关资源
    最近更新 更多