Skip to content

Instantly share code, notes, and snippets.

@zakirangwala
Created December 1, 2020 05:54
Show Gist options
  • Save zakirangwala/7de0903e2fdfcc4d579584637b82a74b to your computer and use it in GitHub Desktop.
Save zakirangwala/7de0903e2fdfcc4d579584637b82a74b to your computer and use it in GitHub Desktop.
Convert file format
# XML TO CSV
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+".jpg",
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 convert():
for directory in ['train', 'test']:
image_path = f'Tensorflow/workspace/images/{directory}'
xml_df = xml_to_csv(image_path)
xml_df.to_csv(
'Tensorflow/workspace/annotations/{}labels.csv'.format(directory), index=None)
print('Successfully converted xml to csv.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment