Publishing 2019 R2 content (#223)
[platform/upstream/dldt.git] / tools / accuracy_checker / accuracy_checker / annotation_converters / bitvehicle.py
1 """
2 Copyright (c) 2019 Intel Corporation
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 """
16
17 from pathlib import Path
18
19 from ..representation import DetectionAnnotation
20 from ..utils import get_key_by_value, read_json, read_xml
21
22 from .format_converter import FileBasedAnnotationConverter
23
24
25 class BITVehicleJSON(FileBasedAnnotationConverter):
26     __provider__ = 'bitvehicle_json'
27
28     def convert(self):
29         annotations = []
30         for annotation_image in read_json(self.annotation_file):
31             labels, x_mins, y_mins, x_maxs, y_maxs, is_ignored, occluded = [], [], [], [], [], [], []
32             for detection in annotation_image['objects']:
33                 x_min, y_min, x_max, y_max = detection['bbox']
34                 label = detection['label']
35
36                 if label == 'ignored':
37                     for class_ in _CLASS_TO_IND.values():
38                         is_ignored.append(len(labels))
39                         labels.append(class_)
40                         x_mins.append(x_min)
41                         y_mins.append(y_min)
42                         x_maxs.append(x_max)
43                         y_maxs.append(y_max)
44                 else:
45                     is_occluded = detection.get('is_occluded', False) or detection.get('occluded', False)
46                     is_difficult = detection.get('difficult', False)
47                     if is_occluded or is_difficult:
48                         occluded.append(len(labels))
49
50                     labels.append(_CLASS_TO_IND[label])
51                     x_mins.append(x_min)
52                     y_mins.append(y_min)
53                     x_maxs.append(x_max)
54                     y_maxs.append(y_max)
55
56             identifier = Path(annotation_image['image']).name
57             annotation = DetectionAnnotation(identifier, labels, x_mins, y_mins, x_maxs, y_maxs)
58             annotation.metadata['is_occluded'] = occluded
59             annotation.metadata['difficult_boxes'] = is_ignored
60
61             annotations.append(annotation)
62
63         return annotations, get_meta()
64
65
66 class BITVehicle(FileBasedAnnotationConverter):
67     __provider__ = 'bitvehicle'
68
69     def convert(self):
70         annotations = []
71         for annotation_image in read_xml(self.annotation_file):
72             if annotation_image.tag != 'image':
73                 continue
74
75             identifier = annotation_image.get('name')
76             labels, x_mins, y_mins, x_maxs, y_maxs, occluded = [], [], [], [], [], []
77             for roi in annotation_image.findall('box'):
78                 label = roi.get("label")
79                 x_left = int(roi.get('xtl'))
80                 x_right = int(roi.get('xbr'))
81                 y_top = int(roi.get('ytl'))
82                 y_bottom = int(roi.get('ybr'))
83                 x_min, y_min, x_max, y_max = x_left, y_top, x_right - x_left, y_bottom - y_top
84                 is_occluded = bool(int(roi.get('occluded')))
85
86                 labels.append(_CLASS_TO_IND[label])
87                 x_mins.append(x_min)
88                 y_mins.append(y_min)
89                 x_maxs.append(x_max)
90                 y_maxs.append(y_max)
91                 if is_occluded:
92                     occluded.append(len(labels) - 1)
93
94             annotation = DetectionAnnotation(identifier, labels, x_mins, y_mins, x_maxs, y_maxs)
95             annotation.metadata['is_occluded'] = occluded
96
97             annotations.append(annotation)
98
99         return annotations, get_meta()
100
101
102 _CLASSES = (
103     '__background__',  # always index 0
104     'vehicle',
105     'plate'
106 )
107
108 _CLASS_TO_IND = dict(zip(_CLASSES, list(range(len(_CLASSES)))))
109
110
111 def get_meta():
112     labels = dict(enumerate(_CLASSES))
113     labels[-1] = 'ignored'
114
115     return {'label_map': labels, 'background_label': get_key_by_value(labels, '__background__')}