我正在运行在github上找到的一些非常酷的代码-> https://github.com/datitran/raccoon_dataset。该代码应将一些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)
)
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():
image_path = os.path.join(os.getcwd(), 'annotations')
xml_df = xml_to_csv(image_path)
xml_df.to_csv('raccoon_labels.csv', index=None)
print('Successfully converted xml to csv.')
main()
我正在装有XML文件的文件夹中运行它。它显示消息“成功将xml转换为csv”。并执行到最后-但CSV文件输出为空(标题除外)。
代码对我来说似乎是正确的,我找不到任何问题。我不明白为什么它不起作用。我以为可能是因为我在XML旁边有JPG文件,但是...没用。
有什么想法吗?
谢谢