Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中将注释xml转换为文本_Xml_Python 3.x_Xslt - Fatal编程技术网

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

在Python中将注释xml转换为文本,xml,python-3.x,xslt,Xml,Python 3.x,Xslt,我有一个文件夹,其中包含大量包含图像注释数据的xml文件。我想将xml文件转换为文本文件,以便用于YOLO模型 我已经通过标记图像生成了xml文件 <annotation> <folder>train</folder> <filename>img_1.jpg</filename> <path>/home/avnika/images_used_for _project/train/img_1.jpg&l

我有一个文件夹,其中包含大量包含图像注释数据的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>
我想以文本格式获取这些值及其属性。但代码给出了如下错误

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'
回溯(最近一次呼叫最后一次):
文件“C:/Users/128938/PycharmProjects/augmentation_code/test_File.py”,第31行,在
读取xml(文件、操作路径)
文件“C:/Users/128938/PycharmProjects/augmentation_code/test_File.py”,第17行,以只读xml格式
doc=[xcontent.find(“train”).text,xcontent.find(“文件名”).text,xcontent.find(“路径”).text,xcontent.find(“宽度”).text,
AttributeError:“非类型”对象没有属性“文本”
在您的代码上

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文件夹中的是标记

<annotation>
    <folder>train</folder>
    <filename>img_1.jpg</filename>
    <path>/home/avnika/images_used_for _project/train/img_1.jpg</path>
    <source>
参考ElementTree XML API以获取根元素, 属性数据、标记文本等

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