Max*_*Max 1 python xml csv elementtree tensorflow
作为 Python 的初学者,我正在尝试使用本教程说明(https://www.youtube.com/watch?v=kq2Gjv_pPe8&list=PLiIy2ThQvgewp67FDKV2H1h-154bJK9RS&index=2&t=477s)将我的 XML 文件转换为 CSV 。最后,我需要 tfrecord 格式的图像和注释,以便在我的自定义 EfficientDet 模型中使用它们。我遵循了这两个帖子的解决方案(IndexError: child index out of range以及为什么我不断让 child out of range 错误?)并尝试了一堆这句话中不同节点号(1-9)的“int(member[3][0].text)”却不断收到“IndexError: child index out of range”错误!
我正在尝试使用以下格式转换我的 XML 文件:
<annotation>
<folder>images</folder>
<filename>Czech_000010.jpg</filename>
<size>
<depth>3</depth>
<width>600</width>
<height>600</height>
</size>
<object>
<name>D40</name>
<bndbox>
<xmin>213</xmin>
<ymin>409</ymin>
<xmax>274</xmax>
<ymax>441</ymax>
</bndbox>
</object>
<object>
<name>D10</name>
<bndbox>
<xmin>228</xmin>
<ymin>473</ymin>
<xmax>327</xmax>
<ymax>495</ymax>
</bndbox>
</object>
</annotation>
Run Code Online (Sandbox Code Playgroud)
使用以下脚本转换为 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():
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()
Run Code Online (Sandbox Code Playgroud)
请尝试:
for member in root.findall('object'):
value = (root.find('filename').text,
int(root.find('size').find('width').text),
int(root.find('size').find('height').text),
member[0].text,
int(member.find("bndbox").find('xmin').text),
int(member.find("bndbox").find('ymin').text),
int(member.find("bndbox").find('xmax').text),
int(member.find("bndbox").find('ymax').text)
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1259 次 |
| 最近记录: |