IndexError: child index out of range

喜欢而已 提交于 2021-01-29 04:03:10

问题


I am trying to train a model for the tensorFlow object detection api. And when I try to convert the xml to csv I get the following error. I have 6300 train data and 700 test data. Can someone please point me out why Im getting this error? Thank you //Tensorflow object detection

Error

Traceback (most recent call last):
File "xml_to_csv.py", line 35, in <module>
main()
File "xml_to_csv.py", line 31, in main
xml_df = xml_to_csv(image_path)
File "xml_to_csv.py", line 17, in xml_to_csv
int(member[4][0].text),
IndexError: child index out of range

Code

import os
import glob
import pandas as pd
import xml.etree.ElementTree as ET


def xml_to_csv(path):
xml_list = []
for xml_file in glob.glob(path + '/*.xml'):
    tree = ET.parse(xml_file)
    root = tree.getroot()
    for member in root.findall('object'):
        value = (root.find('filename').text,
                 int(root.find('size')[0].text),
                 int(root.find('size')[1].text),
                 member[0].text,
                 int(member[4][0].text),
                 int(member[4][1].text),
                 int(member[4][2].text),
                 int(member[4][3].text)
                 )
        xml_list.append(value)
        column_name = ['filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax']
        xml_df = pd.DataFrame(xml_list, columns=column_name)
        return xml_df


def main():
    for directory in ['train','test']:
    image_path = os.path.join(os.getcwd(), 'images/{}'.format(directory))
    xml_df = xml_to_csv(image_path)
    xml_df.to_csv('data/{}_labels.csv'.format(directory), index=None)
    print('Successfully converted xml to csv.')

main()

XML file

<annotation>
<folder>JPEGImages</folder>
<filename>000001.jpg</filename>
<path>VOC2007/JPEGImages/000001.jpg</path>
<source>
    <database>Unknown</database>
</source>
<size>
    <width>1920</width>
    <height>1080</height>
    <depth>3</depth>
</size>
<segmented>0</segmented>
<object>
    <name>ore carrier</name>
    <pose>Unspecified</pose>
    <truncated>0</truncated>
    <bndbox>
        <xmin>633</xmin>
        <ymin>467</ymin>
        <xmax>944</xmax>
        <ymax>510</ymax>
    </bndbox>
</object>


回答1:


Found the answer. Hope it will be useful for some people. Since in the code its findall ('object) , as you can see in the xml, the xmin,ymin,xmax,ymax are listed as the 4th set. So it should be as int(member[3][0].text) All the 4 should be as 3



来源:https://stackoverflow.com/questions/57056675/indexerror-child-index-out-of-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!