-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmain.cpp
More file actions
336 lines (291 loc) · 11.5 KB
/
main.cpp
File metadata and controls
336 lines (291 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include <fsdk/FaceEngine.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <array>
// Helper function to load images.
bool loadImages(
const char *imagesDirPath,
const char *listPath,
std::vector<std::string> &imagesNamesList,
std::vector<fsdk::Image> &imagesList
);
// Extract face descriptor.
fsdk::IDescriptorPtr extractDescriptor(
fsdk::IFaceEnginePtr faceEngine,
fsdk::IDetectorPtr faceDetector,
fsdk::IDescriptorExtractorPtr descriptorExtractor,
fsdk::Image &image
);
int main(int argc, char *argv[])
{
// LSH (Local Sensitive Hashing) table interface.
// The LSH tables allow to pick a given number of nearest neighbors, i.e. ones having the closest distance
// to the user provided reference descritpor from a batch.
// Each LSH table is tied to one descriptor batch. LSH tables are immutable objects
// and therefore should be rebuilt if the corresponding batch is changed.
// LSH table methods are not thread safe; users should create a table per thread
// if parallel processing is required.
// Number of required nearest neighbors.
const int numberNearestNeighbors = 3;
// Number of matching result.
const int numberMatchingResult = 6;
// Parse command line arguments.
// Arguments:
// 1) path to a image,
// 2) path to a images directory,
// 3) path to a images names list,
// 4) matching threshold.
// If matching score is above the threshold, then both images
// belong to the same person, otherwise they belong to different persons.
// Images should be in ppm format.
if (argc != 5) {
std::cout << "Usage: "<< argv[0] << " <image> <imagesDir> <list> <threshold>\n"
" *image - path to image\n"
" *imagesDir - path to images directory\n"
" *list - path to images names list\n"
" *threshold - similarity threshold in range (0..1]\n"
<< std::endl;
return -1;
}
char *imagePath = argv[1];
char *imagesDirPath = argv[2];
char *listPath = argv[3];
float threshold = (float)atof(argv[4]);
std::clog << "imagePath: \"" << imagePath << "\"" << std::endl;
std::clog << "imagesDirPath: \"" << imagesDirPath << "\"" << std::endl;
std::clog << "listPath: \"" << listPath << "\"" << std::endl;
std::clog << "threshold: " << threshold << std::endl;
// Create FaceEngine root SDK object.
fsdk::IFaceEnginePtr faceEngine = fsdk::acquire(fsdk::createFaceEngine("./data", "./data/faceengine.conf"));
if (!faceEngine) {
std::cerr << "Failed to create face engine instance." << std::endl;
return -1;
}
if (faceEngine->getFaceEngineEdition() != fsdk::FaceEngineEdition::CompliteEdition) {
std::cerr << "FaceEngine SDK Frontend edition doesn't support face descriptors. Use FaceEngine SDK Complite edition" <<
std::endl;
return -1;
}
// Create MTCNN detector.
fsdk::IDetectorPtr faceDetector = fsdk::acquire(faceEngine->createDetector(fsdk::ODT_MTCNN));
if (!faceDetector) {
std::cerr << "Failed to create face detector instance." << std::endl;
return -1;
}
// Create descriptor extractor.
fsdk::IDescriptorExtractorPtr descriptorExtractor =
fsdk::acquire(faceEngine->createExtractor());
if (!descriptorExtractor) {
std::cerr << "Failed to create face descriptor extractor instance." << std::endl;
return -1;
}
// Create descriptor matcher.
fsdk::IDescriptorMatcherPtr descriptorMatcher =
fsdk::acquire(faceEngine->createMatcher());
if (!descriptorMatcher) {
std::cerr << "Failed to create face descriptor matcher instance." << std::endl;
return -1;
}
// Load images.
std::vector<std::string> imagesNamesList;
std::vector<fsdk::Image> imagesList;
if (!loadImages(imagesDirPath, listPath, imagesNamesList, imagesList)) {
std::cerr << "Failed to load images." << std::endl;
return -1;
}
std::clog << "Creating descriptor barch." << std::endl;
// Create face descriptor batch.
fsdk::IDescriptorBatchPtr descriptorBatch =
fsdk::acquire(faceEngine->createDescriptorBatch(static_cast<int>(imagesList.size())));
if (!descriptorBatch) {
std::cerr << "Failed to create face descriptor batch instance." << std::endl;
return -1;
}
// Extract faces descriptors.
for (fsdk::Image &image : imagesList) {
fsdk::IDescriptorPtr descriptor = extractDescriptor(
faceEngine,
faceDetector,
descriptorExtractor,
image
);
if (!descriptor)
return -1;
fsdk::Result<fsdk::DescriptorBatchError> descriptorBatchAddResult =
descriptorBatch->add(descriptor);
if (descriptorBatchAddResult.isError()) {
std::cerr << "Failed to add descriptor to descriptor batch." << std::endl;
return -1;
}
}
std::clog << "Creating LSH table." << std::endl;
// Create LSH table.
fsdk::ILSHTablePtr lsh =
fsdk::acquire(faceEngine->createLSHTable(descriptorBatch.get()));
if (!lsh) {
std::cerr << "Failed to create LSH table instance." << std::endl;
return -1;
}
// KNN index array.
std::array<int, numberNearestNeighbors> nearestNeighbors;
fsdk::Image image;
if (!image.loadFromPPM(imagePath)) {
std::cerr << "Failed to load image: \"" << imagePath << "\"" << std::endl;
return -1;
}
// Extract face descriptor.
fsdk::IDescriptorPtr descriptor = extractDescriptor(
faceEngine,
faceDetector,
descriptorExtractor,
image
);
if (!descriptor)
return -1;
// Get numberNearestNeighbors nearest neighbours.
lsh->getKNearestNeighbours(descriptor, numberNearestNeighbors, &nearestNeighbors[0]);
std::clog << "Name image: \"" << imagePath <<
"\", nearest neighbors: \"" << imagesNamesList[nearestNeighbors[0]] <<
"\", \"" << imagesNamesList[nearestNeighbors[1]] <<
"\", \"" << imagesNamesList[nearestNeighbors[2]] << "\"" << std::endl;
// Match descriptor and descriptor batch.
fsdk::MatchingResult matchingResult[numberMatchingResult];
fsdk::Result<fsdk::FSDKError> descriptorMatcherResult =
descriptorMatcher->match(
descriptor,
descriptorBatch,
&nearestNeighbors[0],
numberNearestNeighbors,
&matchingResult[0]
);
if (!descriptorMatcherResult) {
std::cerr << "Failed to match. Reason: " << descriptorMatcherResult.what() << std::endl;
return -1;
}
std::ostringstream oss;
for (size_t j = 0; j < imagesList.size(); ++j) {
std::clog << "Images: \"" << imagePath <<
"\" and \"" << imagesNamesList[j] << "\" matched with score: " <<
matchingResult[j].similarity * 100.f << std::endl;
oss << "Images: \"" << imagePath << "\" and \""
<< imagesNamesList[j] << "\" ";
if (matchingResult[j].similarity > threshold)
oss << "belong to one person." << std::endl;
else
oss << "belong to different persons." << std::endl;
}
std::cout << oss.str();
return 0;
}
bool loadImages(
const char *imagesDirPath,
const char *listPath,
std::vector<std::string> &imagesNamesList,
std::vector<fsdk::Image> &imagesList
) {
std::ifstream listFile(listPath);
if (!listFile) {
std::cerr << "Failed to open file: " << listPath << std::endl;
return false;
}
std::string imageName;
while (listFile >> imageName) {
fsdk::Image image;
std::string imagePath = std::string(imagesDirPath) + "/" + imageName;
if (!image.loadFromPPM(imagePath.c_str())) {
std::cerr << "Failed to load image: \"" << imagePath << "\"" << std::endl;
return false;
}
imagesNamesList.push_back(imageName);
imagesList.push_back(image);
}
listFile.close();
return true;
}
fsdk::IDescriptorPtr extractDescriptor(
fsdk::IFaceEnginePtr faceEngine,
fsdk::IDetectorPtr faceDetector,
fsdk::IDescriptorExtractorPtr descriptorExtractor,
fsdk::Image &image
) {
// Facial feature detection confidence threshold.
const float confidenceThreshold = 0.25f;
if (!image) {
std::cerr << "Request image is invalid." << std::endl;
return nullptr;
}
// Create color image.
fsdk::Image imageBGR;
image.convert(imageBGR, fsdk::Format::B8G8R8);
if (!imageBGR) {
std::cerr << "Conversion to BGR has failed." << std::endl;
return nullptr;
}
std::clog << "Detecting faces." << std::endl;
// Detect no more than 10 faces in the image.
enum { MaxDetections = 10 };
// Data used for detection.
fsdk::Detection detections[MaxDetections];
int detectionsCount(MaxDetections);
fsdk::Landmarks5 landmarks5[MaxDetections];
// Detect faces in the image.
fsdk::ResultValue<fsdk::FSDKError, int> detectorResult = faceDetector->detect(
image,
image.getRect(),
&detections[0],
&landmarks5[0],
detectionsCount
);
if (detectorResult.isError()) {
std::cerr << "Failed to detect face detection. Reason: " << detectorResult.what() << std::endl;
return nullptr;
}
detectionsCount = detectorResult.getValue();
if (detectionsCount == 0) {
std::clog << "Faces is not found." << std::endl;
return nullptr;
}
std::clog << "Found " << detectionsCount << " face(s)." << std::endl;
int bestDetectionIndex(-1);
float bestScore(-1.f);
// Loop through all the faces and find one with the best score.
for (int detectionIndex = 0; detectionIndex < detectionsCount; ++detectionIndex) {
fsdk::Detection &detection = detections[detectionIndex];
std::clog << "Detecting facial features (" << detectionIndex + 1 << "/" << detectionsCount << ")" << std::endl;
// Choose the best detection.
if (detection.score > bestScore) {
bestScore = detection.score;
bestDetectionIndex = detectionIndex;
}
}
// If detection confidence score is too low, abort.
if (bestScore < confidenceThreshold) {
std::clog << "Face detection succeeded, but no faces with good confidence found." << std::endl;
return nullptr;
}
std::clog << "Best face confidence is " << bestScore << std::endl;
// Stage 2. Create face descriptor.
std::clog << "Extracting descriptor." << std::endl;
// Create face descriptor.
fsdk::IDescriptorPtr descriptor = fsdk::acquire(faceEngine->createDescriptor());
if (!descriptor) {
std::cerr << "Failed to create face descrtiptor instance." << std::endl;
return nullptr;
}
// Extract face descriptor.
// This is typically the most time-consuming task.
fsdk::ResultValue<fsdk::FSDKError, float> descriptorExtractorResult = descriptorExtractor->extract(
image,
detections[bestDetectionIndex],
landmarks5[bestDetectionIndex],
descriptor
);
if(descriptorExtractorResult.isError()) {
std::cerr << "Failed to extract face descriptor. Reason: " << descriptorExtractorResult.what() << std::endl;
return nullptr;
}
return descriptor;
}