|
a |
|
b/preprocess/parse_annotation.py |
|
|
1 |
import xml.etree.ElementTree as ET |
|
|
2 |
|
|
|
3 |
|
|
|
4 |
def parse_annotation(ann_dir, labels=None): |
|
|
5 |
all_imgs = [] |
|
|
6 |
seen_labels = {} |
|
|
7 |
img = {'object': []} |
|
|
8 |
tree = ET.parse(ann_dir) |
|
|
9 |
|
|
|
10 |
for elem in tree.iter(): |
|
|
11 |
if 'width' in elem.tag: |
|
|
12 |
img['width'] = int(elem.text) |
|
|
13 |
if 'height' in elem.tag: |
|
|
14 |
img['height'] = int(elem.text) |
|
|
15 |
if 'object' in elem.tag or 'part' in elem.tag: |
|
|
16 |
obj = {} |
|
|
17 |
|
|
|
18 |
for attr in list(elem): |
|
|
19 |
if 'name' in attr.tag: |
|
|
20 |
obj['name'] = attr.text |
|
|
21 |
|
|
|
22 |
if obj['name'] in seen_labels: |
|
|
23 |
seen_labels[obj['name']] += 1 |
|
|
24 |
else: |
|
|
25 |
seen_labels[obj['name']] = 1 |
|
|
26 |
|
|
|
27 |
if len(labels) > 0 and obj['name'] not in labels: |
|
|
28 |
break |
|
|
29 |
else: |
|
|
30 |
img['object'] += [obj] |
|
|
31 |
|
|
|
32 |
if 'bndbox' in attr.tag: |
|
|
33 |
for dim in list(attr): |
|
|
34 |
if 'xmin' in dim.tag: |
|
|
35 |
obj['xmin'] = int(round(float(dim.text))) |
|
|
36 |
if 'ymin' in dim.tag: |
|
|
37 |
obj['ymin'] = int(round(float(dim.text))) |
|
|
38 |
if 'xmax' in dim.tag: |
|
|
39 |
obj['xmax'] = int(round(float(dim.text))) |
|
|
40 |
if 'ymax' in dim.tag: |
|
|
41 |
obj['ymax'] = int(round(float(dim.text))) |
|
|
42 |
|
|
|
43 |
if len(img['object']) > 0: |
|
|
44 |
all_imgs += [img] |
|
|
45 |
|
|
|
46 |
return all_imgs, seen_labels |