Publishing 2019 R1 content
[platform/upstream/dldt.git] / tools / accuracy_checker / accuracy_checker / postprocessor / normalize_landmarks_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
19 from ..config import BoolField
20 from ..postprocessor.postprocessor import Postprocessor, BasePostprocessorConfig
21 from ..representation import FacialLandmarksAnnotation, FacialLandmarksPrediction
22
23
24 class NormalizeLandmarksPoints(Postprocessor):
25     __provider__ = 'normalize_landmarks_points'
26
27     annotation_types = (FacialLandmarksAnnotation, )
28     prediction_types = (FacialLandmarksPrediction, )
29
30     def validate_config(self):
31         class _ConfigValidator(BasePostprocessorConfig):
32             use_annotation_rect = BoolField(optional=True)
33
34         config_validator = _ConfigValidator(
35             self.__provider__, on_extra_argument=_ConfigValidator.ERROR_ON_EXTRA_ARGUMENT
36         )
37         config_validator.validate(self.config)
38
39     def configure(self):
40         self.use_annotation_rect = self.config.get('use_annotation_rect', False)
41
42     def process_image(self, annotation, prediction):
43         for target in annotation:
44             height, width, _ = self.image_size
45             x_start, y_start = 0, 0
46             if self.use_annotation_rect:
47                 resized_box = annotation[0].metadata.get('rect')
48                 x_start, y_start, x_max, y_max = resized_box
49                 width = x_max - x_start
50                 height = y_max - y_start
51
52             target.x_values = (
53                 (np.array(target.x_values, dtype=float) - x_start) / np.maximum(width, np.finfo(np.float64).eps)
54             )
55             target.y_values = (
56                 (np.array(target.y_values, dtype=float) - y_start) / np.maximum(height, np.finfo(np.float64).eps)
57             )
58
59         return annotation, prediction