Publishing 2019 R1 content
[platform/upstream/dldt.git] / tools / accuracy_checker / accuracy_checker / annotation_converters / detection_opencv_storage.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 from ..config import PathField, NumberField
17 from ..representation import DetectionAnnotation
18 from ..utils import convert_bboxes_xywh_to_x1y1x2y2, read_xml, read_txt
19
20 from .format_converter import BaseFormatConverter, BaseFormatConverterConfig
21
22
23 class DetectionOpenCVConverterConfig(BaseFormatConverterConfig):
24     annotation_file = PathField()
25     image_names_file = PathField(optional=True)
26     label_start = NumberField(floats=False, optional=True)
27     background_label = NumberField(floats=False, optional=True)
28
29
30 class DetectionOpenCVStorageFormatConverter(BaseFormatConverter):
31     __provider__ = 'detection_opencv_storage'
32
33     _config_validator_type = DetectionOpenCVConverterConfig
34
35     def configure(self):
36         self.annotation_file = self.config['annotation_file']
37         self.image_names_file = self.config.get('image_names_file')
38         self.label_start = self.config.get('label_start', 1)
39         self.background_label = self.config.get('background_label')
40
41     def convert(self):
42         root = read_xml(self.annotation_file)
43
44         labels_set = self.get_label_set(root)
45
46         labels_set = sorted(labels_set)
47         class_to_ind = dict(zip(labels_set, list(range(self.label_start, len(labels_set) + self.label_start + 1))))
48         label_map = {}
49         for class_label, ind in class_to_ind.items():
50             label_map[ind] = class_label
51
52         annotations = []
53         for frames in root:
54             for frame in frames:
55                 identifier = '{}.png'.format(frame.tag)
56                 labels, x_mins, y_mins, x_maxs, y_maxs = [], [], [], [], []
57                 difficult_indices = []
58                 for annotation in frame:
59                     label = annotation.findtext('type')
60                     if not label:
61                         raise ValueError('"{}" contains detection without "{}"'.format(self.annotation_file, 'type'))
62
63                     box = annotation.findtext('roi')
64                     if not box:
65                         raise ValueError('"{}" contains detection without "{}"'.format(self.annotation_file, 'roi'))
66                     box = list(map(float, box.split()))
67
68                     is_ignored = annotation.findtext('is_ignored', 0)
69                     if int(is_ignored) == 1:
70                         difficult_indices.append(len(labels))
71
72                     labels.append(class_to_ind[label])
73                     x_min, y_min, x_max, y_max = convert_bboxes_xywh_to_x1y1x2y2(*box)
74                     x_mins.append(x_min)
75                     y_mins.append(y_min)
76                     x_maxs.append(x_max)
77                     y_maxs.append(y_max)
78
79                 detection_annotation = DetectionAnnotation(identifier, labels, x_mins, y_mins, x_maxs, y_maxs)
80                 detection_annotation.metadata['difficult_boxes'] = difficult_indices
81                 annotations.append(detection_annotation)
82
83         if self.image_names_file:
84             self.rename_identifiers(annotations, self.image_names_file)
85
86         meta = {}
87         if self.background_label:
88             label_map[self.background_label] = '__background__'
89             meta['background_label'] = self.background_label
90         meta['label_map'] = label_map
91
92         return annotations, meta
93
94     @staticmethod
95     def rename_identifiers(annotation_list, images_file):
96         for annotation, image in zip(annotation_list, read_txt(images_file)):
97             annotation.identifier = image
98
99         return annotation_list
100
101
102     @staticmethod
103     def get_label_set(xml_root):
104         labels_set = set()
105         for frames in xml_root:
106             for frame in frames:
107                 for annotation in frame:
108                     label = annotation.findtext('type')
109                     if not label:
110                         raise ValueError('annotation contains detection without label')
111
112                     labels_set.add(label)
113
114         return labels_set