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.
17 from ..representation import DetectionPrediction
18 from ..adapters import Adapter
21 class XML2DetectionAdapter(Adapter):
23 Class for converting xml detection results in OpenCV FileStorage format to DetectionPrediction representation.
26 __provider__ = 'xml_detection'
28 def process(self, tree, identifiers=None, frame_meta=None):
29 class_to_ind = dict(zip(self.label_map.values(), range(len(self.label_map.values()))))
32 for frames in tree.getroot():
34 identifier = frame.tag + '.png'
35 labels, scores, x_mins, y_mins, x_maxs, y_maxs = [], [], [], [], [], []
36 for prediction in frame:
37 if prediction.find('is_ignored'):
40 label = prediction.find('type')
42 raise ValueError('Detection predictions contains detection without "{}"'.format('type'))
43 label = class_to_ind[label.text]
45 confidence = prediction.find('confidence')
46 if confidence is None:
47 raise ValueError('Detection predictions contains detection without "{}"'.format('confidence'))
48 confidence = float(confidence.text)
50 box = prediction.find('roi')
52 raise ValueError('Detection predictions contains detection without "{}"'.format('roi'))
53 box = list(map(float, box.text.split()))
56 scores.append(confidence)
59 x_maxs.append(box[0] + box[2])
60 y_maxs.append(box[1] + box[3])
62 result[identifier] = DetectionPrediction(identifier, labels, scores, x_mins, y_mins, x_maxs, y_maxs)