Publishing 2019 R1 content
[platform/upstream/dldt.git] / tools / accuracy_checker / accuracy_checker / postprocessor / clip_boxes.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 ..config import BoolField, NumberField
18 from ..representation import DetectionPrediction, DetectionAnnotation
19 from .postprocessor import PostprocessorWithSpecificTargets, PostprocessorWithTargetsConfigValidator
20
21
22 class ClipBoxes(PostprocessorWithSpecificTargets):
23     __provider__ = 'clip_boxes'
24
25     annotation_types = (DetectionAnnotation, )
26     prediction_types = (DetectionPrediction, )
27
28     def validate_config(self):
29         class _ClipConfigValidator(PostprocessorWithTargetsConfigValidator):
30             dst_width = NumberField(floats=False, optional=True, min_value=1)
31             dst_height = NumberField(floats=False, optional=True, min_value=1)
32             size = NumberField(floats=False, optional=True, min_value=1)
33             boxes_normalized = BoolField(optional=True)
34
35         clip_config_validator = _ClipConfigValidator(
36             self.__provider__, on_extra_argument=_ClipConfigValidator.ERROR_ON_EXTRA_ARGUMENT
37         )
38         clip_config_validator.validate(self.config)
39
40     def configure(self):
41         size = self.config.get('size')
42         self.dst_height = size or self.config.get('dst_height')
43         self.dst_width = size or self.config.get('dst_width')
44
45         self.boxes_normalized = self.config.get('boxes_normalized', False)
46
47     def process_image(self, annotation, prediction):
48         target_height = self.dst_height or self.image_size[0]
49         target_width = self.dst_width or self.image_size[1]
50
51         max_width = target_width if not self.boxes_normalized else 1
52         max_height = target_height if not self.boxes_normalized else 1
53
54         for target in annotation:
55             self._clip_boxes(target, (0, max_width), (0, max_height))
56         for target in prediction:
57             self._clip_boxes(target, (0, max_width), (0, max_height))
58
59         return annotation, prediction
60
61     @staticmethod
62     def _clip_boxes(entry, width_range, height_range):
63         entry.x_mins = entry.x_mins.clip(width_range[0], width_range[1])
64         entry.x_maxs = entry.x_maxs.clip(width_range[0], width_range[1])
65         entry.y_mins = entry.y_mins.clip(height_range[0], height_range[1])
66         entry.y_maxs = entry.y_maxs.clip(height_range[0], height_range[1])
67
68         return entry