【发布时间】:2019-01-23 12:18:03
【问题描述】:
我有一个 CSV,如下所示。我正在尝试将其转换为 CSV,然后从 CSV 转换为 tfrecord,以便在 TensorFlow 中训练模型。
<annotation>
<folder>imgs</folder>
<filename>steve_jobs.jpg</filename>
<path>C:/Users/kulkaa/PythonProjects/tensor_android/imgs/steve_jobs.jpg</path>
<source>
<database>Unknown</database>
</source>
<size>
<width>183</width>
<height>276</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>steve_jobs</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>8</xmin>
<ymin>13</ymin>
<xmax>178</xmax>
<ymax>227</ymax>
</bndbox>
</object>
</annotation>
我编写了一个python 程序将XML 转换为CSV。我什至创建了一个名为data 的空目录。
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(), 'imgs/{}'.format(directory))
if image_path is None:
print('Error!')
xml_df = xml_to_csv(image_path)
#Storing the csv file into the data directory.
xml_df.to_csv('data/{}.csv'.format(directory), index=None)
print('Successfully converted xml to csv.')
main()
【问题讨论】:
-
这里有谁能回答这个问题吗?
-
有时你的图像路径是不同的。确保你在图像中有训练和测试文件夹。研究/object_detection/training/images-train & test 文件夹
标签: xml python-3.x csv