Publishing 2019 R1 content
[platform/upstream/dldt.git] / tools / accuracy_checker / accuracy_checker / postprocessor / clip_points.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 import numpy as np
18 from ..config import BoolField, NumberField
19 from ..representation import TextDetectionAnnotation, TextDetectionPrediction
20 from ..utils import get_size_from_config
21 from .postprocessor import PostprocessorWithSpecificTargets, PostprocessorWithTargetsConfigValidator
22
23
24 class ClipPointsConfigValidator(PostprocessorWithTargetsConfigValidator):
25     dst_width = NumberField(floats=False, optional=True, min_value=1)
26     dst_height = NumberField(floats=False, optional=True, min_value=1)
27     size = NumberField(floats=False, optional=True, min_value=1)
28     points_normalized = BoolField(optional=True)
29
30
31 class ClipPoints(PostprocessorWithSpecificTargets):
32     __provider__ = 'clip_points'
33
34     annotation_types = (TextDetectionAnnotation, )
35     prediction_types = (TextDetectionPrediction, )
36
37     def validate_config(self):
38         clip_points_config_validator = ClipPointsConfigValidator(
39             self.__provider__, on_extra_argument=ClipPointsConfigValidator.ERROR_ON_EXTRA_ARGUMENT
40         )
41         clip_points_config_validator.validate(self.config)
42
43     def configure(self):
44         self.dst_height, self.dst_width = get_size_from_config(self.config, allow_none=True)
45         self.points_normalized = self.config.get('points_normalized', False)
46
47     def process_image(self, annotation, prediction):
48         target_width = self.dst_width or self.image_size[1] - 1
49         target_height = self.dst_height or self.image_size[0] - 1
50
51         max_width = target_width if not self.points_normalized else 1
52         max_height = target_height if not self.points_normalized else 1
53         for target in annotation:
54             points = []
55             for polygon in target.points:
56                 polygon[:, 0] = np.clip(polygon[:, 0], 0, max_width)
57                 polygon[:, 1] = np.clip(polygon[:, 1], 0, max_height)
58                 points.append(polygon)
59             target.points = points
60         for target in prediction:
61             points = []
62             for polygon in target.points:
63                 polygon[:, 0] = np.clip(polygon[:, 0], 0, max_width)
64                 polygon[:, 1] = np.clip(polygon[:, 1], 0, max_height)
65                 points.append(polygon)
66             target.points = points
67
68         return annotation, prediction