2 Copyright (c) 2019 Intel Corporation
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
16 from ..config import PathField, NumberField
17 from ..representation import DetectionAnnotation
18 from ..utils import convert_bboxes_xywh_to_x1y1x2y2, read_xml, read_txt
20 from .format_converter import BaseFormatConverter, BaseFormatConverterConfig
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)
30 class DetectionOpenCVStorageFormatConverter(BaseFormatConverter):
31 __provider__ = 'detection_opencv_storage'
33 _config_validator_type = DetectionOpenCVConverterConfig
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')
42 root = read_xml(self.annotation_file)
44 labels_set = self.get_label_set(root)
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))))
49 for class_label, ind in class_to_ind.items():
50 label_map[ind] = class_label
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')
61 raise ValueError('"{}" contains detection without "{}"'.format(self.annotation_file, 'type'))
63 box = annotation.findtext('roi')
65 raise ValueError('"{}" contains detection without "{}"'.format(self.annotation_file, 'roi'))
66 box = list(map(float, box.split()))
68 is_ignored = annotation.findtext('is_ignored', 0)
69 if int(is_ignored) == 1:
70 difficult_indices.append(len(labels))
72 labels.append(class_to_ind[label])
73 x_min, y_min, x_max, y_max = convert_bboxes_xywh_to_x1y1x2y2(*box)
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)
83 if self.image_names_file:
84 self.rename_identifiers(annotations, self.image_names_file)
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
92 return annotations, meta
95 def rename_identifiers(annotation_list, images_file):
96 for annotation, image in zip(annotation_list, read_txt(images_file)):
97 annotation.identifier = image
99 return annotation_list
103 def get_label_set(xml_root):
105 for frames in xml_root:
107 for annotation in frame:
108 label = annotation.findtext('type')
110 raise ValueError('annotation contains detection without label')
112 labels_set.add(label)