为什么我总是让孩子超出范围错误? [英] why do I keep getting child out of range error?

查看:37
本文介绍了为什么我总是让孩子超出范围错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此python脚本将xml转换为csv:

I am using this python script to convert xml's to csv:

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),
                     int(member[4][4].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(), 'papers/{}'.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上运行它时,如下所示:

when I run it on xml's that look like this:

<annotation>
    <folder>images</folder>
    <filename>BROWN, M M-1.jpg</filename>
    <size>
        <width>2200</width>
        <height>3217</height>
        <depth>3</depth>
    </size>
    <segmented>0</segmented>
    <object>
        <name>DOB</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <occluded>0</occluded>
        <difficult>0</difficult>
        <bndbox>
            <xmin>121</xmin>
            <ymin>296</ymin>
            <xmax>625</xmax>
            <ymax>346</ymax>
        </bndbox>
    </object>
</annotation>

我不断得到这个

int(member [4] [0] .text),IndexError:子索引超出范围'

int(member[4][0].text), IndexError: child index out of range' error

谁能帮助我找出问题所在?我不确定为什么错误不断弹出.

Can anyone help me figure out what's the problem? I'm not sure why the error keeps popping up.

推荐答案

根据示例XML, bundbox 元素位于节点5,而不是4. pandas 用于csv迁移.考虑 csv (Python 3的内置模块).下面还显示了可以对节点使用编号索引, [##] find(): xmin ymin xmin xmax .

According to sample XML, the bundbox element is located at node 5, not 4. By the way, there's no need to use pandas for csv migration. Consider the csv (built-in module as of Python 3). Also below shows you can use numbered indexing, [##], or find() for the nodes: xmin, ymin, xmin, and xmax.

# ALL STANDARD LIBRARY MODULES
import os, glob, csv
import xml.etree.ElementTree as ET

def xml_to_csv(path):

    with open("Output.csv", "w") as f:
        cw = csv.writer(f, lineterminator="\n")
        cw.writerow(['filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax'])

        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[5][0].text),
                         int(member[5][1].text),
                         int(member[5].find('ymin').text),
                         int(member[5].find('ymax').text)
                        )
                cw.writerow(value)

这篇关于为什么我总是让孩子超出范围错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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