Created
April 14, 2018 09:49
-
-
Save rotemtam/88d9a4efae243fc77ed4a0f9917c8f6c to your computer and use it in GitHub Desktop.
pascal voc xml to csv table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'): | |
bbx = member.find('bndbox') | |
xmin = int(bbx.find('xmin').text) | |
ymin = int(bbx.find('ymin').text) | |
xmax = int(bbx.find('xmax').text) | |
ymax = int(bbx.find('ymax').text) | |
label = member.find('name').text | |
value = (root.find('filename').text, | |
int(root.find('size')[0].text), | |
int(root.find('size')[1].text), | |
label, | |
xmin, | |
ymin, | |
xmax, | |
ymax | |
) | |
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(): | |
datasets = ['train', 'dev', 'test'] | |
for ds in datasets: | |
image_path = os.path.join(os.getcwd(), ds, 'annotations') | |
xml_df = xml_to_csv(image_path) | |
xml_df.to_csv('labels_{}.csv'.format(ds), index=None) | |
print('Successfully converted xml to csv.') | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
##I'm working on the same code. If you are still interested in guiding me through, help me change from XML to CSV file