Publishing 2019 R1 content
[platform/upstream/dldt.git] / tools / accuracy_checker / accuracy_checker / annotation_converters / lfw.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 collections import defaultdict
18 from pathlib import Path
19
20 from ..config import PathField
21 from ..representation import ReIdentificationClassificationAnnotation
22 from ..utils import read_txt
23
24 from .format_converter import BaseFormatConverter, BaseFormatConverterConfig
25
26
27 class FaceReidPairwiseConverterConfig(BaseFormatConverterConfig):
28     pairs_file = PathField()
29     train_file = PathField(optional=True)
30     landmarks_file = PathField(optional=True)
31
32
33 class FaceReidPairwiseConverter(BaseFormatConverter):
34     __provider__ = 'face_reid_pairwise'
35
36     _config_validator_type = FaceReidPairwiseConverterConfig
37
38     def configure(self):
39         self.pairs_file = self.config['pairs_file']
40         self.train_file = self.config.get('train_file')
41         self.landmarks_file = self.config.get('landmarks_file')
42
43     def convert(self):
44         landmarks_map = {}
45         if self.landmarks_file:
46             for landmark_line in read_txt(self.landmarks_file):
47                 landmark_line = landmark_line.split('\t')
48                 landmarks_map[landmark_line[0]] = [int(point) for point in landmark_line[1:]]
49
50         test_annotations = self.prepare_annotation(self.pairs_file, True, landmarks_map)
51         if self.train_file:
52             train_annotations = self.prepare_annotation(self.train_file, True, landmarks_map)
53             test_annotations += train_annotations
54
55         return test_annotations, None
56
57     @staticmethod
58     def get_image_name(person, image_id):
59         image_path_pattern = '{}/{}_{}{}.jpg'
60         return image_path_pattern.format(person, person, '0' * (4 - len(image_id)), image_id)
61
62     def convert_positive(self, pairs, all_images):
63         positives = defaultdict(set)
64         for data in pairs:
65             image1 = self.get_image_name(data[0], data[1])
66             image2 = self.get_image_name(data[0], data[2])
67             positives[image1].add(image2)
68             all_images.add(image1)
69             all_images.add(image2)
70
71         return positives, all_images
72
73     def convert_negative(self, pairs, all_images):
74         negatives = defaultdict(set)
75         for data in pairs:
76             image1 = self.get_image_name(data[0], data[1])
77             image2 = self.get_image_name(data[2], data[3])
78             negatives[image1].add(image2)
79             all_images.add(image1)
80             all_images.add(image2)
81
82         return negatives, all_images
83
84     def prepare_annotation(self, ann_file: Path, train=False, landmarks_map=None):
85         positive_pairs, negative_pairs = [], []
86         ann_lines = read_txt(ann_file)
87         for line in ann_lines[1:]:  # skip header
88             pair = line.strip().split()
89             if len(pair) == 3:
90                 positive_pairs.append(pair)
91             elif len(pair) == 4:
92                 negative_pairs.append(pair)
93
94         all_images = set()
95         positive_data, all_images = self.convert_positive(positive_pairs, all_images)
96         negative_data, all_images = self.convert_negative(negative_pairs, all_images)
97
98         annotations = []
99         for image in all_images:
100             annotation = ReIdentificationClassificationAnnotation(image, positive_data[image], negative_data[image])
101
102             if landmarks_map:
103                 image_landmarks = landmarks_map.get(image)
104                 annotation.metadata['keypoints'] = image_landmarks
105
106             if train:
107                 annotation.metadata['train'] = True
108
109             annotations.append(annotation)
110
111         return annotations