2 Copyright (c) 2019 Intel Corporation
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
17 from collections import defaultdict
18 from pathlib import Path
20 from ..config import PathField
21 from ..representation import ReIdentificationClassificationAnnotation
22 from ..utils import read_txt
24 from .format_converter import BaseFormatConverter, BaseFormatConverterConfig
27 class FaceReidPairwiseConverterConfig(BaseFormatConverterConfig):
28 pairs_file = PathField()
29 train_file = PathField(optional=True)
30 landmarks_file = PathField(optional=True)
33 class FaceReidPairwiseConverter(BaseFormatConverter):
34 __provider__ = 'face_reid_pairwise'
36 _config_validator_type = FaceReidPairwiseConverterConfig
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')
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:]]
50 test_annotations = self.prepare_annotation(self.pairs_file, True, landmarks_map)
52 train_annotations = self.prepare_annotation(self.train_file, True, landmarks_map)
53 test_annotations += train_annotations
55 return test_annotations, None
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)
62 def convert_positive(self, pairs, all_images):
63 positives = defaultdict(set)
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)
71 return positives, all_images
73 def convert_negative(self, pairs, all_images):
74 negatives = defaultdict(set)
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)
82 return negatives, all_images
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()
90 positive_pairs.append(pair)
92 negative_pairs.append(pair)
95 positive_data, all_images = self.convert_positive(positive_pairs, all_images)
96 negative_data, all_images = self.convert_negative(negative_pairs, all_images)
99 for image in all_images:
100 annotation = ReIdentificationClassificationAnnotation(image, positive_data[image], negative_data[image])
103 image_landmarks = landmarks_map.get(image)
104 annotation.metadata['keypoints'] = image_landmarks
107 annotation.metadata['train'] = True
109 annotations.append(annotation)