“int(member[5][0].text), IndexError: child index out of range“错误解决办法

在用Tensorflow做目标检测时,需要使用 xml_to_csv.py文件将labelimg生成的.xml格式的文件转换成.csv格式的文件。

在转换的过程中经常会出现以下错误:

Traceback (most recent call last):
  File "xml_to_csv.py", line 37, in <module>
    xml_to_csv(sys.argv[1], sys.argv[2])
  File "xml_to_csv.py", line 23, in xml_to_csv
    int(member[4][0].text),
IndexError: child index out of range

错误出现在member[4][0].text上,其中member[4][0].text到member[4][3].text应该是指的bbox的4个坐标参数。

查看xml文件发现,其代码如下:

<annotation>
  <folder>egohands</folder>
  <filename>CARDS_COURTYARD_B_T_0011.jpg</filename>
  <size>
    <width>1280</width>
    <height>720</height>
    <depth>3</depth>
  </size>
  <segmented>0</segmented>
  <object>
    <name>hand</name>
    <difficult>0</difficult>
    <bndbox>
      <xmin>648</xmin>
      <ymin>454</ymin>
      <xmax>825</xmax>
      <ymax>551</ymax>
    </bndbox>
  </object>
  <object>
    <name>hand</name>
    <difficult>0</difficult>
    <bndbox>
      <xmin>516</xmin>
      <ymin>431</ymin>
      <xmax>623</xmax>
      <ymax>543</ymax>
    </bndbox>
  </object>
</annotation>

member[4][0].text是bndbox的参数,bndbox在xml文件中是object的第3个参数,从0算起的话对应数字应该是2,而转换代码里写的member[4][0].text,所以应该把member[4][0].text到member[4][3].text中的4全部换成2,即:

        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[2][0].text),
                     int(member[2][1].text),
                     int(member[2][2].text),
                     int(member[2][3].text)
                     )

错误解决!

猜你喜欢

转载自blog.csdn.net/gaoqing_dream163/article/details/114335901