-
Notifications
You must be signed in to change notification settings - Fork 0
/
voc_utils.py
154 lines (130 loc) · 4.21 KB
/
voc_utils.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from xml.etree import ElementTree
from pathlib import Path
from boxes import BoundingBox
background_class_name = 'background'
classes = [
# Default class
background_class_name,
# People
'person',
# Animals
'bird',
'cat',
'cow',
'dog',
'horse',
'sheep',
# Vehicles
'aeroplane',
'bicycle',
'boat',
'bus',
'car',
'motorbike',
'train',
# Objects
'bottle',
'chair',
'diningtable',
'pottedplant',
'sofa',
'tvmonitor'
]
n_classes = len(classes)
def get_background_class_name():
return background_class_name
def get_number_of_classes():
return n_classes
def get_index_to_class_map():
return dict(enumerate(classes))
def get_class_to_index_map():
return dict(zip(classes, range(len(classes))))
def parse_single_VOC_annotation(annotation_file):
"""Parses a single XML file representing an annotation in the Pascal
VOC format.
Args:
annotation_file (string or file object): Either a path to an XML
file containing an annotation in the Pascal VOC format or a
file object.
Returns:
tuple(string, dict): Pair containing the name of the file and a
dictionary having the following structure:
{
'width': Width of the image the annotation refers to,
'height': Height of the image the annotation refers to,
'objects': [
{
'class': The class label for this object,
'bounding_box': The bounding box for this
object. It must be an instance of the
`BoundingBox` class
},
...
]
}
"""
annotation = ElementTree.parse(annotation_file).getroot()
filename = annotation.find('filename').text
image_width = int(annotation.find('size/width').text)
image_height = int(annotation.find('size/height').text)
objects = []
for obj in annotation.findall('object'):
class_ = obj.find('name').text
x_min, y_min, x_max, y_max = obj.find('bndbox')
bounding_box = BoundingBox(
int(x_min.text),
int(y_min.text),
int(x_max.text),
int(y_max.text)
)
objects.append({
'class': class_,
'bounding_box': bounding_box
})
return filename, {
'width': image_width,
'height': image_height,
'objects': objects
}
def parse_VOC_annotations(annotations_dir):
"""Parses all the annotation files contained in `annotations_dir`.
The annotations must be XML files in the Pascal VOC format.
Args:
annotations_dir (string or pathlib.Path): Path to a directory
containing annotation files in the Pascal VOC format.
Raises:
ValueError: `annotations_dir` does not exist.
ValueError: `annotations_dir` is not a directory.
Returns:
dict: Dictionary mapping filenames of the files contained in
`annotations_dir` to dictionaries having the following
structure:
{
'width': Width of the image the annotation refers to,
'height': Height of the image the annotation refers to,
'objects': [
{
'class': The class label for this object,
'bounding_box': The bounding box for this
object. It must be an instance of the
`BoundingBox` class
},
...
]
}
"""
annotations_dir = Path(annotations_dir)
if not annotations_dir.exists():
raise ValueError(
f'Directory "{annotations_dir}" does not exist.'
)
if not annotations_dir.is_dir():
raise ValueError(
'`annotations_dir` must be a directory containing XML '
'annotations files in VOC format.'
)
annotations = {}
for xml_file in annotations_dir.iterdir():
filename, annotation = parse_single_VOC_annotation(xml_file)
annotations[filename] = annotation
return annotations