IndexError:子索引超出范围 [英] IndexError: child index out of range

查看:67
本文介绍了IndexError:子索引超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 tensorFlow 对象检测 api 训练模型.当我尝试将 xml 转换为 csv 时,出现以下错误.我有 6300 个训练数据和 700 个测试数据.有人可以指出我为什么会收到此错误吗?谢谢//Tensorflow 对象检测

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

错误

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

代码

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

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

推荐答案

找到了答案.希望它对某些人有用.由于在代码中它的 findall ('object) ,正如您在 xml 中看到的那样,xmin,ymin,xmax,ymax 被列为第 4 组.所以它应该是 int(member[3][0].text)所有 4 应为 3

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

这篇关于IndexError:子索引超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆