【发布时间】:2019-10-08 05:45:05
【问题描述】:
我正在使用这个 python 脚本将 xml 转换为 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 上运行它时:
<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: child index out of range' 错误
谁能帮我找出问题所在?我不确定为什么会不断弹出错误。
【问题讨论】: