ROC SDK  2.4.0
Scalable Face Recognition Software
roc_example_person_search.c

Source code for examples/roc_example_person_search.c.

/*
* This example application takes as input an image paths. The first image
* (the gallery) is presumed to have multiple people, and the second image (the
* probe) is presumed to have one person. The application outputs the probe's
* response according to ROC_OBJECT_REPRESENTATION. Alternatively, if a third
* and fourth arguments are passed, the application will create a
* ROC_COLOR_REPRESENTATION template to use as the probe based on the color
* passed.
*
* Notably, this script is designed to work on people, which is distinct from
* the default object color representation. Please see documenation of
* ROC_COLOR_REPRESENTATION to see how the person class differs from the other
* objects.
*
* To compile this example on OS X / Linux (bash):
*
* $ gcc -o roc-example-person-search roc_example_person_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_person_search.c /TP /I ../include/ ../lib/roc.lib /Feroc-example-person-search.exe
*
* To run this application using two of the example images:
*
* Linux
* $ ./roc-example-person-search ../lib ../data/people.jpg ../data/person.jpg
*
* Windows
* $ .\roc-example-person-search . ..\data\people.jpg black black
*/
#include <stdio.h>
#include <stdlib.h>
#include "roc.h"
int main(int argc, char *argv[])
{
const int maximum_people = 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;
size_t adaptive_minimum_size;
int i;
if (argc != 4 && argc != 5)
roc_ensure("Expected three or four arguments:\n"
#ifdef _WIN32
" > roc-example-person-search.exe path\\to\\rocsdk\\bin path\\to\\gallery.jpg [path\\to\\probe.jpg] | [torso_color thigh_color]"
#else // !_WIN32
" $ roc-example-person-search path/to/rocsdk/lib path/to/gallery.jpg [path/to/probe.jpg] | [torso_color thigh_color]"
#endif // _WIN32
);
if (argc == 5) {
// identify torso color selection
if (!strcmp(argv[3], "white")) torso_color = ROC_COLOR_WHITE;
else if(!strcmp(argv[3], "grey")) torso_color = ROC_COLOR_GREY;
else if(!strcmp(argv[3], "black")) torso_color = ROC_COLOR_BLACK;
else if(!strcmp(argv[3], "red")) torso_color = ROC_COLOR_RED;
else if(!strcmp(argv[3], "orange")) torso_color = ROC_COLOR_ORANGE;
else if(!strcmp(argv[3], "yellow")) torso_color = ROC_COLOR_YELLOW;
else if(!strcmp(argv[3], "green")) torso_color = ROC_COLOR_GREEN;
else if(!strcmp(argv[3], "blue")) torso_color = ROC_COLOR_BLUE;
else if(!strcmp(argv[3], "violet")) torso_color = ROC_COLOR_VIOLET;
else if(!strcmp(argv[3], "pink")) torso_color = ROC_COLOR_PINK;
else if(!strcmp(argv[3], "brown")) torso_color = ROC_COLOR_BROWN;
else if(!strcmp(argv[3], "none")) torso_color = ROC_COLOR_NONE;
else roc_ensure("Unexpected color argument at torso_color.\n"
"Expects one of: white, grey, black, red, orange, yellow, green, blue, violet, pink, brown, none");
// identify thigh color selection
if (!strcmp(argv[4], "white")) thigh_color = ROC_COLOR_WHITE;
else if(!strcmp(argv[4], "grey")) thigh_color = ROC_COLOR_GREY;
else if(!strcmp(argv[4], "black")) thigh_color = ROC_COLOR_BLACK;
else if(!strcmp(argv[4], "red")) thigh_color = ROC_COLOR_RED;
else if(!strcmp(argv[4], "orange")) thigh_color = ROC_COLOR_ORANGE;
else if(!strcmp(argv[4], "yellow")) thigh_color = ROC_COLOR_YELLOW;
else if(!strcmp(argv[4], "green")) thigh_color = ROC_COLOR_GREEN;
else if(!strcmp(argv[4], "blue")) thigh_color = ROC_COLOR_BLUE;
else if(!strcmp(argv[4], "violet")) thigh_color = ROC_COLOR_VIOLET;
else if(!strcmp(argv[4], "pink")) thigh_color = ROC_COLOR_PINK;
else if(!strcmp(argv[4], "brown")) thigh_color = ROC_COLOR_BROWN;
else if(!strcmp(argv[4], "none")) thigh_color = ROC_COLOR_NONE;
else roc_ensure("Unexpected color argument at thigh_color.\n"
"Expects one of: white, grey, black, red, orange, yellow, green, blue, violet, pink, brown, none");
}
// Initialize SDK
roc_ensure(roc_initialize(NULL, NULL));
// Open both images
roc_ensure(roc_read_image(argv[2], ROC_BGR24, &gallery_image));
// Construct gallery by finding all people in the gallery image
roc_ensure(roc_open_gallery(NULL, &gallery, NULL));
gallery_templates = (roc_template*) malloc(maximum_people * sizeof(roc_template));
for (i=0; i<maximum_people; i++) {
if (!gallery_templates[i].algorithm_id) {
if (i == 0)
roc_ensure("Failed to find a person in the gallery image!");
break;
}
roc_ensure(roc_enroll(gallery, gallery_templates[i], NULL));
}
if (torso_color == ROC_COLOR_NONE && thigh_color == ROC_COLOR_NONE) {
// Find a single person in the probe image
roc_ensure(roc_read_image(argv[3], ROC_BGR24, &probe_image));
if (!probe.algorithm_id)
roc_ensure("Failed to find a person in the probe image!");
} else {
probe_image.data = NULL;
roc_ensure(roc_new_person_color_template(&probe, torso_color, thigh_color));
}
// 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));
if (probe_image.data)
roc_ensure(roc_free_image(probe_image));
for (i=0; i<maximum_people; i++)
roc_ensure(roc_free_template(&gallery_templates[i]));
free(gallery_templates);
free(candidates);
return EXIT_SUCCESS;
}