ROC SDK  2.4.0
Scalable Face Recognition Software
roc_example_search.c

Source code for examples/roc_example_search.c.

/*
* This example application takes as input two image paths. The first image (the
* gallery) is presumed to have multiple faces, and the second image (the probe)
* is presumed to have one face. The application outputs the probe's face
* quality and the top three face bounding boxes in the gallery image with the
* greatest similarity to the probe.
*
* To compile this example on OS X / Linux (bash):
*
* $ gcc -o roc-example-search roc_example_search.c -I ../include/ -L ../lib/ -Wl,-rpath,'$ORIGIN/../lib:../lib' -lroc
*
* To compile this example on Windows (Visual Studio Command Prompt):
*
* $ cl roc_example_search.c /TP /I ../include/ ../lib/roc.lib /Feroc-example-search.exe
*
* To run this application using two of the example images:
*
* $ ./roc-example-search ../data/roc.jpg ../data/josh_2.jpg
*/
#include <stdio.h>
#include <stdlib.h>
#include "roc.h"
int main(int argc, char *argv[])
{
const int maximum_faces = 10;
const int maximum_candidates = 3;
roc_image gallery_image, probe_image;
roc_gallery gallery;
roc_template *gallery_templates;
roc_template probe, candidate_template;
roc_candidate candidate;
roc_candidate *candidates;
roc_string gender, geographic_origin;
double age, quality;
size_t adaptive_minimum_size;
int i;
if (argc != 3)
roc_ensure("Expected two image path arguments:\n"
" $ roc-example-search path/to/gallery.jpg path/to/probe.jpg");
// Initialize SDK
roc_ensure(roc_initialize(NULL, NULL));
// Open both images
roc_ensure(roc_read_image(argv[1], ROC_BGR24, &gallery_image));
roc_ensure(roc_read_image(argv[2], ROC_BGR24, &probe_image));
// Construct gallery by finding all faces in the gallery image
roc_ensure(roc_open_gallery(NULL, &gallery, NULL));
gallery_templates = (roc_template*) malloc(maximum_faces * sizeof(roc_template));
for (i=0; i<maximum_faces; i++) {
if (!gallery_templates[i].algorithm_id) {
if (i == 0)
roc_ensure("Failed to find a face in the gallery image!");
break;
}
roc_ensure(roc_enroll(gallery, gallery_templates[i], NULL));
}
// Find a single face in the probe image
if (!probe.algorithm_id)
roc_ensure("Failed to find a face in the probe image!");
// Print probe demographics and face quality
roc_ensure(roc_get_metadata(probe, "Gender", &gender));
roc_ensure(roc_get_metadata(probe, "GeographicOrigin", &geographic_origin));
roc_ensure(roc_get_metadata_double(probe, "Age", &age));
roc_ensure(roc_get_metadata_double(probe, "Quality", &quality));
printf("Probe Demographics: %s / %d / %s\nProbe Quality: %f\n", gender, (int) age, geographic_origin, quality);
roc_ensure(roc_free_string(&geographic_origin));
// Execute search
candidates = (roc_candidate*) malloc(maximum_candidates * sizeof(roc_candidate));
roc_ensure(roc_search(gallery, probe, maximum_candidates, 0.0f, candidates));
puts("Similarity\tX\tY\tWidth\tHeight");
for (i=0; i<maximum_candidates; i++) {
candidate = candidates[i];
break;
roc_ensure(roc_at(gallery, candidate.index, &candidate_template));
printf("%g\t%g\t%g\t%g\t%g\n", candidate.similarity, candidate_template.detection.x, candidate_template.detection.y, candidate_template.detection.width, candidate_template.detection.height);
roc_ensure(roc_free_template(&candidate_template));
}
// Cleanup
roc_ensure(roc_free_image(gallery_image));
roc_ensure(roc_free_image(probe_image));
for (i=0; i<maximum_faces; i++)
roc_ensure(roc_free_template(&gallery_templates[i]));
free(gallery_templates);
free(candidates);
return EXIT_SUCCESS;
}