-
Notifications
You must be signed in to change notification settings - Fork 3
/
xml-converter.py
executable file
·43 lines (35 loc) · 1.44 KB
/
xml-converter.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
import os
import glob
import pandas as pd
import xml.etree.ElementTree as ET
INPUT_PATH = 'input'
OUTPUT_PATH = 'config'
TEST_PERCENTAGE = 20
class XmlConvertor:
def parseXmls(self, xmls):
xmlList = []
for xml in xmls:
tree = ET.parse(xml)
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)
)
xmlList.append(value)
return xmlList
def toCSV(self, xmls, output):
columnsNames = ['filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax']
dataFrame = pd.DataFrame(self.parseXmls(xmls), columns=columnsNames)
return dataFrame.to_csv(output, index=None)
xmls = glob.glob('{}/*.xml'.format(os.path.join(os.getcwd(), INPUT_PATH)))
trainXmls = xmls[TEST_PERCENTAGE:]
testXmls = xmls[0:TEST_PERCENTAGE]
xmlConvertor = XmlConvertor()
xmlConvertor.toCSV(trainXmls, './{}/train.csv'.format(OUTPUT_PATH))
xmlConvertor.toCSV(testXmls, './{}/test.csv'.format(OUTPUT_PATH))
print("xml to csv mession has been comlpeted!")