Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <annotation>
- <folder>images</folder>
- <filename>elon_musk.jpeg</filename>
- <path><Path_to>/elon_musk.jpeg</path>
- <source>
- <database>Unknown</database>
- </source>
- <size>
- <width>225</width>
- <height>225</height>
- <depth>3</depth>
- </size>
- <segmented>0</segmented>
- <object>
- <name>elon_musk</name>
- <pose>Unspecified</pose>
- <truncated>0</truncated>
- <difficult>0</difficult>
- <bndbox>
- <xmin>42</xmin>
- <ymin>12</ymin>
- <xmax>199</xmax>
- <ymax>171</ymax>
- </bndbox>
- </object>
- </annotation>
- 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)
- #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()
Add Comment
Please, Sign In to add comment