Publishing 2019 R1 content
[platform/upstream/dldt.git] / tools / accuracy_checker / accuracy_checker / annotation_converters / vgg_face_regression.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 PathField
20 from ..representation import FacialLandmarksAnnotation
21 from ..utils import convert_bboxes_xywh_to_x1y1x2y2, read_csv
22 from .format_converter import BaseFormatConverter, BaseFormatConverterConfig
23
24
25 class LandmarksRegressionConfig(BaseFormatConverterConfig):
26     landmarks_csv_file = PathField()
27     bbox_csv_file = PathField(optional=True)
28
29
30 class LandmarksRegression(BaseFormatConverter):
31     __provider__ = 'landmarks_regression'
32
33     _config_validator_type = LandmarksRegressionConfig
34
35     def configure(self):
36         self.landmarks_csv = self.config['landmarks_csv_file']
37         self.bbox_csv = self.config.get('bbox_csv_file')
38
39     def convert(self):
40         annotations = []
41         for row in read_csv(self.landmarks_csv):
42             identifier = row['NAME_ID'] + '.jpg'
43             x_values = np.array(
44                 [float(row["P1X"]), float(row["P2X"]), float(row["P3X"]), float(row["P4X"]), float(row["P5X"])]
45             )
46             y_values = np.array(
47                 [float(row["P1Y"]), float(row["P2Y"]), float(row["P3Y"]), float(row["P4Y"]), float(row["P5Y"])]
48             )
49
50             annotation = FacialLandmarksAnnotation(identifier, x_values, y_values)
51             annotation.metadata['left_eye'] = 0
52             annotation.metadata['right_eye'] = 1
53             annotations.append(annotation)
54
55         if self.bbox_csv:
56             for index, row in enumerate(read_csv(self.bbox_csv)):
57                 annotations[index].metadata['rect'] = convert_bboxes_xywh_to_x1y1x2y2(
58                     int(row["X"]), int(row["Y"]), int(row["W"]), int(row["H"])
59                 )
60
61         meta = {
62             'label_map': {0: 'Left Eye', 1: 'Right Eye', 2: 'Nose', 3: 'Left Mouth Corner', 4: 'Right Mouth Corner'}
63         }
64         return annotations, meta