Guest User

Untitled

a guest
Jul 19th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. <annotation>
  2. <folder>images</folder>
  3. <filename>elon_musk.jpeg</filename>
  4. <path><Path_to>/elon_musk.jpeg</path>
  5. <source>
  6. <database>Unknown</database>
  7. </source>
  8. <size>
  9. <width>225</width>
  10. <height>225</height>
  11. <depth>3</depth>
  12. </size>
  13. <segmented>0</segmented>
  14. <object>
  15. <name>elon_musk</name>
  16. <pose>Unspecified</pose>
  17. <truncated>0</truncated>
  18. <difficult>0</difficult>
  19. <bndbox>
  20. <xmin>42</xmin>
  21. <ymin>12</ymin>
  22. <xmax>199</xmax>
  23. <ymax>171</ymax>
  24. </bndbox>
  25. </object>
  26. </annotation>
  27.  
  28. import os
  29. import glob
  30. import pandas as pd
  31. import xml.etree.ElementTree as ET
  32.  
  33.  
  34. def xml_to_csv(path):
  35. xml_list = []
  36. for xml_file in glob.glob(path + '/*.xml'):
  37. tree = ET.parse(xml_file)
  38. root = tree.getroot()
  39. for member in root.findall('object'):
  40. value = (root.find('filename').text,
  41. int(root.find('size')[0].text),
  42. int(root.find('size')[1].text),
  43. member[0].text,
  44. int(member[4][0].text),
  45. int(member[4][1].text),
  46. int(member[4][2].text),
  47. int(member[4][3].text)
  48. )
  49. xml_list.append(value)
  50. column_name = ['filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax']
  51. xml_df = pd.DataFrame(xml_list, columns=column_name)
  52. return xml_df
  53.  
  54.  
  55. def main():
  56. for directory in ['train','test']:
  57. image_path = os.path.join(os.getcwd(), 'images/{}'.format(directory))
  58. xml_df = xml_to_csv(image_path)
  59. #Storing the csv file into the data directory.
  60. xml_df.to_csv('data/{}.csv'.format(directory), index=None)
  61. print('Successfully converted xml to csv.')
  62.  
  63. main()
Add Comment
Please, Sign In to add comment