Skip to content

Instantly share code, notes, and snippets.

@iKhushPatel
Created May 20, 2020 05:52
Show Gist options
  • Save iKhushPatel/ed1f837656b155d9b94d45b42e00f5e4 to your computer and use it in GitHub Desktop.
Save iKhushPatel/ed1f837656b155d9b94d45b42e00f5e4 to your computer and use it in GitHub Desktop.
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','testing']:
image_path = os.path.join(os.getcwd(), 'images/{}'.format(directory).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.')
'''
image_path = os.path.join(os.getcwd(), 'images/train')
xml_df = xml_to_csv(image_path)
xml_df.to_csv('data/train_labels.csv', index=None)
image_path = os.path.join(os.getcwd(), 'images/test')
xml_df = xml_to_csv(image_path)
xml_df.to_csv('data/test_labels.csv',index=None)
main()
@R0jER5
Copy link

R0jER5 commented Mar 4, 2021

so we have to create 2 folder named as train and test and put some images in both. and then run this code. right??

@tzelalouzeir
Copy link

i dont know why we label test images. They are already for test and we shouldn't label them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment