在Python中将注释xml转换为文本

问题描述 投票:0回答:3

我有一个文件夹,其中包含大量带有图像注释数据的 xml 文件。我想将xml文件转换为文本文件,以便它们可以用于YOLO模型

我通过标记图像生成了 xml 文件

<annotation>
    <folder>train</folder>
    <filename>img_1.jpg</filename>
    <path>/home/avnika/images_used_for _project/train/img_1.jpg</path>
    <source>
        <database>Unknown</database>
    </source>
    <size>
        <width>310</width>
        <height>163</height>
        <depth>3</depth>
    </size>
    <segmented>0</segmented>
    <object>
        <name>person</name>
        <pose>Unspecified</pose>
        <truncated>1</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>193</xmin>
            <ymin>40</ymin>
            <xmax>237</xmax>
            <ymax>163</ymax>
        </bndbox>
    </object>
</annotation>

下面是我到目前为止的代码

from xml.etree.ElementTree import ElementTree
import sys
import os
import glob
from glob import glob

def read_xml(f,op):

    if not os.path.exists(op):
        os.makedirs(op,exist_ok=True)

    file_n = glob(f)
    for i in range(len(file_n)):
        xcontent = ElementTree()
        xcontent.parse(file_n[i])

        doc = [xcontent.find("train").text,xcontent.find("filename").text,xcontent.find("path").text,xcontent.find("width").text,
            xcontent.find("height").text,xcontent.find("depth").text,xcontent.find("name").text,xcontent.find("xmin").text,
            xcontent.find("ymin").text,xcontent.find("xmax").text,xcontent.find("ymax").text]

        out = open(file_n[i]+".txt","w")
        out.write(op)



if __name__ == '__main__':

    files=("C:\\multi_cat_3\\models\\research\\object_detection\\images\\train_xmls\\*")
    op_path=("C:\\multi_cat_3\\models\\research\\object_detection\\images\\train_xmls_op")

    read_xml(files,op_path)

我想以文本格式获取这些值及其属性。但代码给了我这个错误,如下所示

Traceback (most recent call last):
  File "C:/Users/128938/PycharmProjects/augmentation_code/test_file.py", line 31, in <module>
    read_xml(files,op_path)
  File "C:/Users/128938/PycharmProjects/augmentation_code/test_file.py", line 17, in read_xml
    doc = [xcontent.find("train").text,xcontent.find("filename").text,xcontent.find("path").text,xcontent.find("width").text,
AttributeError: 'NoneType' object has no attribute 'text'
xml python-3.x xslt
3个回答
1
投票

在您的代码上。,

doc = [xcontent.find("train").text,xcontent.find("filename").text,xcontent.find("path").text,xcontent.find("width").text,
            xcontent.find("height").text,xcontent.find("depth").text,xcontent.find("name").text,xcontent.find("xmin").text,
            xcontent.find("ymin").text,xcontent.find("xmax").text,xcontent.find("ymax").text]

您尝试查找 train 标签,但在您的 XML folder 中是标签

<annotation>
    <folder>train</folder>
    <filename>img_1.jpg</filename>
    <path>/home/avnika/images_used_for _project/train/img_1.jpg</path>
    <source>

替换此代码部分,find方法尝试查找数据。如果没有获取数据,则返回 NoneType。

doc = [xcontent.find("folder").text,xcontent.find("filename").text,xcontent.find("path").text,xcontent.find("width").text,
                xcontent.find("height").text,xcontent.find("depth").text,xcontent.find("name").text,xcontent.find("xmin").text,
                xcontent.find("ymin").text,xcontent.find("xmax").text,xcontent.find("ymax").text]

请参阅 ElementTree XML API https://docs.python.org/3/library/xml.etree.elementtree.html#module-xml.etree.ElementTree 获取根元素, 属性数据、标签文本等,


0
投票
import os 
xml_label = [x for x in os.walk('../Drone3/label/')]
xml_label = xml_label[0][2]
for xml in xml_label:
    xml_sp = xml.split(".")
    tree = ET.parse("../Drone3/label/"+xml)
    root = tree.getroot()

    xmin = root.find("./object/bndbox/xmin").text
    ymin = root.find("./object/bndbox/ymin").text
    xmax = root.find("./object/bndbox/xmax").text
    ymax = root.find("./object/bndbox/ymax").text

    data = "0" + " " + xmin + " " + ymin+ " " + xmax + " " + ymax

    txt = open('../Drone3/label_txt/'+xml_sp[0]+".txt","w+")
    txt.write(data)

0
投票

希望你不介意几年后回复。

我遇到了同样的问题,并找到了一个可以进行此转换的在线 github 存储库。 数据注释

© www.soinside.com 2019 - 2024. All rights reserved.