Publishing 2019 R1 content
[platform/upstream/dldt.git] / tools / accuracy_checker / accuracy_checker / adapters / dummy_adapters.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 ..representation import DetectionPrediction
18 from ..adapters import Adapter
19
20
21 class XML2DetectionAdapter(Adapter):
22     """
23     Class for converting xml detection results in OpenCV FileStorage format to DetectionPrediction representation.
24     """
25
26     __provider__ = 'xml_detection'
27
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()))))
30
31         result = {}
32         for frames in tree.getroot():
33             for frame in frames:
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'):
38                         continue
39
40                     label = prediction.find('type')
41                     if not label:
42                         raise ValueError('Detection predictions contains detection without "{}"'.format('type'))
43                     label = class_to_ind[label.text]
44
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)
49
50                     box = prediction.find('roi')
51                     if not box:
52                         raise ValueError('Detection predictions contains detection without "{}"'.format('roi'))
53                     box = list(map(float, box.text.split()))
54
55                     labels.append(label)
56                     scores.append(confidence)
57                     x_mins.append(box[0])
58                     y_mins.append(box[1])
59                     x_maxs.append(box[0] + box[2])
60                     y_maxs.append(box[1] + box[3])
61
62                     result[identifier] = DetectionPrediction(identifier, labels, scores, x_mins, y_mins, x_maxs, y_maxs)
63
64         return result