-
Notifications
You must be signed in to change notification settings - Fork 1
/
xml2csv.py
73 lines (65 loc) · 2.55 KB
/
xml2csv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python3.5
# coding=utf-8
'''
@date = '17/12/1'
@author = 'lynnchan'
@email = '[email protected]'
'''
import os
import glob
import pandas as pd
import xml.etree.ElementTree as ET
from gconfig import *
train_path = Train_Data_Path
test_path = Test_Data_Path
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'):
if os.path.splitext(root.find('filename').text)[1] == '.jpg':
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)
)
else:
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 creat_csv():
if type(train_path) !=list:
xml_train = xml_to_csv(train_path)
xml_train.to_csv(train_path+'/'+Train_File_Name+'.csv', index=None)
print('Successfully converted train xml to csv.')
else:
for i in train_path:
xml_train = xml_to_csv(i)
xml_train.to_csv(i + '/' + Train_File_Name + '.csv', index=None)
print('Successfully converted list train xml to csv.')
if type(test_path) != list:
xml_test = xml_to_csv(test_path)
xml_test.to_csv(test_path+'/'+Test_File_Name+'.csv', index=None)
print('Successfully converted test xml to csv.')
else:
for i in test_path:
xml_train = xml_to_csv(i)
xml_train.to_csv(i + '/' + Test_File_Name + '.csv', index=None)
print('Successfully converted list train xml to csv.')
if __name__ == '__main__':
creat_csv()