Face SDK  1.13.0 Mozart release
Face Recognition Software Development Kit
complex.cpp
#include <face_sdk_base/image.h>
#include <face_sdk_alignment/alignment.h>
#include <face_sdk_builder/builder.h>
#include <face_sdk_face_detector/face_detector.h>
#include <face_sdk_age_gender/age_gender.h>
#include <face_sdk_fir_matcher/fir_matcher.h>
#include <iostream>
#include <algorithm>
int main(int argc, char **argv)
{
if (argc < 2) {
std::cout << "usage: <IMG_1> <IMG_2> ... <IMG_N>" << std::endl;
return 1;
}
std::shared_ptr<face_sdk::face_detector> face_detector;
std::shared_ptr<face_sdk::alignment> alignment;
std::shared_ptr<face_sdk::builder> builder;
std::shared_ptr<face_sdk::fir_matcher> matcher;
std::shared_ptr<face_sdk::age_gender> age_gender;
try {
// initialize Face Detector, Alignment, Builder and Age&Gender algorithms with batch size 1 and on CPU
face_detector = face_sdk::make_face_detector(200, 1, -1);
alignment = face_sdk::make_alignment(103, 1, -1);
builder = face_sdk::make_builder(200, 1, -1);
age_gender = face_sdk::make_age_gender(100, 1, -1);
// initialize FIRs matcher with builder version and "gn" mathing table code. See \ref matching_table_codes.
matcher = face_sdk::make_fir_matcher(builder->version(), "gn");
// load images and make faces objects.
face_sdk::image_set images;
for (int i = 1; i < argc; i++) {
auto img = face_sdk::load_image_from_file(argv[i]);
images.push_back(img);
}
// detect faces on each image
std::vector< std::shared_ptr< face_sdk::face > > faces;
for (auto img : images) {
auto faces = face_detector->detect_faces({ img }, 0.4f)[0];
if (faces.size() == 0) {
// no faces found
continue;
}
// select only best detected face (with better confidence)
std::sort(faces.begin(), faces.end(), [](const std::shared_ptr<face_sdk::face> &f1, const std::shared_ptr<face_sdk::face> &f2) -> bool { return f1->confidence() < f2->confidence(); });
faces.push_back(faces.back());
}
// perform alignment
auto align_results = alignment->calc_alignment(faces);
// build FIRs for previously aligned images
auto firs = builder->build(align_results);
// match FIRs to each other and print results
for (int i = 0; i < firs.size(); i++) {
for (int j = i + 1; j < firs.size(); j++) {
auto match_result = matcher->match(firs[i], firs[j]);
std::cout << i << " -> " << j << " = " << match_result << std::endl;
}
}
// classify age and gender for previously aligned images
auto age_gender_info = age_gender->calc_age_gender(align_results);
for (int i = 0; i < age_gender_info.size(); i++) {
std::cout << i << " -> " << " age " << age_gender_info[i].age << ", gender " << (age_gender_info[i].gender < 0 ? "female" : "male") << std::endl;
}
}
// exception handling
catch (const face_sdk::exception &exp) {
// print the stack of nested exceptions ...
std::cerr << face_sdk::format_exception(exp) << std::endl;
// ... or handle more detailed exceptions
try {
std::rethrow_if_nested(exp);
}
catch (const face_sdk::version_not_available_exception &nested) {
// ...
std::cerr << nested.what() << std::endl;
}
catch (const face_sdk::license_exception &nested) {
// ...
std::cerr << nested.what() << std::endl;
}
catch (const std::exception &nested) {
// ...
std::cerr << nested.what() << std::endl;
}
return 2;
}
return 0;
}