-
Notifications
You must be signed in to change notification settings - Fork 2
/
fromXMLtopoints.py
48 lines (35 loc) · 1.46 KB
/
fromXMLtopoints.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
from xml.dom import minidom
import pandas as pd
import json
no_of_files = 8
xmldoc = []
imagenamelist = []
itemlist = []
for i in range(no_of_files):
xmldoc.append(minidom.parse('resources/new-car/polyline-annotatioins/annotations-0' + str(i+1) + '.xml'))
imagenamelist.append(xmldoc[i].getElementsByTagName('image'))
itemlist.append(xmldoc[i].getElementsByTagName('polyline'))
df = pd.DataFrame(columns=['count', 'points'])
def format_data(data, itemlist, imagenamelist):
for item, image in zip(itemlist, imagenamelist):
count = int(image.attributes['name'].value.replace('875-1049/','').replace('img-', '').replace('images', '').replace('.jpg', ''))
point_data = []
for point in item.attributes['points'].value.split(';'):
points = {}
points['x'] = float(point.split(',')[0])
points['y'] = float(point.split(',')[1])
point_data.append(points)
data['count'].append(count)
data['points'].append(json.dumps(point_data))
return data
if not len(imagenamelist) == len(itemlist):
print('length miss match check XML file!')
else:
data = {}
data['count'] = []
data['points'] = []
for item, imagename in zip(itemlist, imagenamelist):
data = format_data(data, item, imagename)
df = df.append(pd.DataFrame.from_dict(data), ignore_index=True)
print(df.shape)
df.to_csv('resources/new-car/cvat-polyline-annotation.csv', index=False)