visageSDK
Public Member Functions | Public Attributes | List of all members
VisageSDK::VisageFaceRecognition Class Reference

VisageFaceRecognition class contains a face recognition algorithm capable of measuring similarity between human faces and recognizing a person's identity from frontal facial images (yaw angle approximately from -20 to 20 degrees) by comparing face descriptors. More...

#include <VisageFaceRecognition.h>

Public Member Functions

 VisageFaceRecognition (const char *dataPath)
 Constructor. More...
 
 ~VisageFaceRecognition ()
 Destructor.
 
int getDescriptorSize ()
 Gets the descriptor's size. More...
 
int extractDescriptor (FaceData *facedata, VsImage *image, short *descriptor)
 Extracts the face descriptor for face recognition from a facial image. More...
 
float descriptorsSimilarity (short *first_descriptor, short *second_descriptor)
 Calculates similarity between two descriptors. More...
 
int addDescriptor (VsImage *image, FaceData *facedata, const char *name)
 Extracts a face descriptor from the input RGB, RGBA or grayscale image and adds it to the gallery. More...
 
int addDescriptor (short *descriptor, const char *name)
 Adds face descriptor to the gallery. More...
 
int getDescriptorCount ()
 Gets number of descriptors in the gallery. More...
 
int getDescriptorName (char *name, int index)
 Gets the name of a descriptor at the given index in the gallery. More...
 
int replaceDescriptorName (const char *name, int index)
 Replaces the name of a descriptor at the given index in the gallery with new name. More...
 
int removeDescriptor (int index)
 Removes a descriptor at the given index from the gallery. More...
 
int saveGallery (const char *file_name)
 Save gallery as a binary file. More...
 
int loadGallery (const char *file_name)
 Load gallery from a binary file. More...
 
void resetGallery ()
 Clear all face descriptors from the gallery.
 
int recognize (short *descriptor, int n, const char **names, float *similarities)
 Compare a face to all faces in the current gallery and return n names of the most similar faces. More...
 

Public Attributes

bool is_initialized
 If the model used in the VisageFaceRecognition is loaded and initialized successfully value of the is_initialized variable is true, otherwise it is false. More...
 

Detailed Description

VisageFaceRecognition class contains a face recognition algorithm capable of measuring similarity between human faces and recognizing a person's identity from frontal facial images (yaw angle approximately from -20 to 20 degrees) by comparing face descriptors.

The face descriptor is a condensed representation of a face. It is an array of short values. The dimension of the array is obtained using getDescriptorSize() function, from now on in the text referred to as DESCRIPTOR_SIZE.

To extract descriptors from facial image, method extractDescriptor() is provided.
Similarity between two faces is calculated as distance between their face descriptors and returned as a value between 0 (no similarity) and 1 (maximum similarity) using descriptorsSimilarity().

For a person to be recognized, its identity has to be stored beforehand. Database of stored face descriptors will be referred to as face recognition gallery. For smaller sets of face descriptors (up to several hundred), higher-level API for recognizing person's identity using face recognition gallery is provided. The gallery is an array of face descriptors, with a corresponding array of names, so that the gallery may contain n descriptors and a corresponding name for each descriptor. To perform recognition, the face descriptor extracted from the input image is then compared with face descriptors from the gallery and the similarity between the input face descriptor and all face descriptors from the gallery is calculated in order to find the face(s) from the gallery that are most similar to the input face. The VisageFaceRecognition class includes the following set of functions for manipulating the gallery, including adding, removing, naming descriptors in the gallery as well as loading and saving the gallery to file:

The gallery is stored as a simple txt file and as such it is not intended to be used to save large number of face descriptors. The current implementation of gallery is only a demonstration of how Face Recognition API can be used and it is recommended to implement more efficient solution for scenarios which require large face recognition gallery.

Definition at line 87 of file VisageFaceRecognition.h.

Constructor & Destructor Documentation

◆ VisageFaceRecognition()

VisageSDK::VisageFaceRecognition::VisageFaceRecognition ( const char *  dataPath)

Constructor.

Parameters
dataPathpath to the data file required for face recognition - fr.vino.bin provided in Samples/data/vfr. It is required that fr.vino.xml be present in the same folder.

Member Function Documentation

◆ addDescriptor() [1/2]

int VisageSDK::VisageFaceRecognition::addDescriptor ( short *  descriptor,
const char *  name 
)

Adds face descriptor to the gallery.

Parameters
descriptordescriptor to be added to the gallery obtained by calling extractDescriptor.
namename of the descriptor.
Returns
1 on success, 0 on failure. The function fails if the descriptor is not a valid pointer.

◆ addDescriptor() [2/2]

int VisageSDK::VisageFaceRecognition::addDescriptor ( VsImage *  image,
FaceData *  facedata,
const char *  name 
)

Extracts a face descriptor from the input RGB, RGBA or grayscale image and adds it to the gallery.

Parameters
imageVsImage pointer that contains the input RGB, RGBA or grayscale image. The image should contain only one face and this face will be added to the gallery. In case of multiple faces in the image, it is not defined which face would be used.
facedatafacial data obtained from VisageTracker or VisageFeaturesDetector.
namename of the face in the image.
Returns
1 on success, 0 on failure. The function fails if the face is not found in the image or if the image argument is not a valid image pointer.

See also: VsImage

◆ descriptorsSimilarity()

float VisageSDK::VisageFaceRecognition::descriptorsSimilarity ( short *  first_descriptor,
short *  second_descriptor 
)

Calculates similarity between two descriptors.

The function returns a float value between 0 and 1. Two descriptors are equal if the similarity is 1. Two descriptors are completely different if the similarity is 0.

Parameters
first_descriptorpointer to the first descriptor, an array of DESCRIPTOR_SIZE short values.
second_descriptorpointer to the second descriptor, an array of DESCRIPTOR_SIZE short values.
Returns
float value between 0 and 1, 1 means full similarity and 0 means full diversity.

◆ extractDescriptor()

int VisageSDK::VisageFaceRecognition::extractDescriptor ( FaceData *  facedata,
VsImage *  image,
short *  descriptor 
)

Extracts the face descriptor for face recognition from a facial image.

Prior to using this function, it is necessary to process the facial image or video frame using VisageTracker or VisageFeaturesDetector and pass the obtained facial data to this function. An example of use:

IplImage* iplImage = cvLoadImage(imageFile.c_str(), CV_LOAD_IMAGE_COLOR);
VsImage* image = vsCreateImageHeader(vsSize(iplImage->width, iplImage->height), iplImage->depth, iplImage->nChannels);
vsSetData(image, iplImage->imageData, iplImage->widthStep);
vsCvtColor(image, image, VS_BGR2RGB); //convert image to RGB; by default, OpenCV loads image in BGR color space
string dataPathFeaturesDetector(".");
VisageFeaturesDetector *m_Detector = new VisageFeaturesDetector("Face Detector.cfg");
string dataPathFaceRecognition("vfr/fr.bin");
VisageFaceRecognition *m_Recognition = new VisageFaceRecognition(dataPathFaceRecognition.c_str());
if(m_Detector->Initialize() && m_Recognition->is_initialized)
{
int DESCRIPTOR_SIZE = m_Recognition->getDescriptorSize();
FaceData* data = new FaceData[MAX_FACES];
int n_faces = m_Detector->detectFacialFeatures(image, data, MAX_FACES);
short** allDescriptors = new short*[n_faces];
for (int i = 0; i < n_faces; i++){
short* descriptor = new short[DESCRIPTOR_SIZE];
int status = m_Recognition->extractDescriptor(&data[i], image, descriptor);
allDescriptors[i] = descriptor;
}
// do something with descriptors..
for (int i = 0; i < n_faces; i++)
delete[] allDescriptors[i];
delete[] allDescriptors;
delete[] data;
}
delete m_Recognition;
delete m_Detector;
vsReleaseImageHeader(&image);
cvReleaseImage(&iplImage);
Parameters
facedatafacial data obtained from VisageTracker or VisageFeaturesDetector.
imageVsImage pointer to the input RGB, RGBA or grayscale image.
descriptorpointer to a DESCRIPTOR_SIZE-dimensional array of short. The resulting face descriptor is returned in this array.
Returns
1 on success, 0 on failure.

See also: FaceData, VisageTracker, VisageFeaturesDetector

◆ getDescriptorCount()

int VisageSDK::VisageFaceRecognition::getDescriptorCount ( )

Gets number of descriptors in the gallery.

Returns
returns number of descriptors in the gallery.

◆ getDescriptorName()

int VisageSDK::VisageFaceRecognition::getDescriptorName ( char *  name,
int  index 
)

Gets the name of a descriptor at the given index in the gallery.

Parameters
namebuffer into which the name of a descriptor is filled.
indexindex of descriptor in the gallery.
Returns
1 on success, 0 on failure. The function fails if index is out of range.

◆ getDescriptorSize()

int VisageSDK::VisageFaceRecognition::getDescriptorSize ( )

Gets the descriptor's size.

The function returns size of an array which should be allocated for storing the descriptor.

Returns
descriptor's size.

◆ loadGallery()

int VisageSDK::VisageFaceRecognition::loadGallery ( const char *  file_name)

Load gallery from a binary file.

The entries from the loaded gallery are appended to the current gallery. If it is required to replace existing gallery with the loaded one, call resetGallery() first.

Parameters
file_namename of the file (including path if needed) from which the gallery will be loaded.
Returns
1 on success, 0 on failure. The function fails if the file can not be opened.

◆ recognize()

int VisageSDK::VisageFaceRecognition::recognize ( short *  descriptor,
int  n,
const char **  names,
float *  similarities 
)

Compare a face to all faces in the current gallery and return n names of the most similar faces.

Parameters
descriptorpointer to the face descriptor of the face to be recognized (compared to the gallery).
nnumber of names and similarities to be returned.
namespointer to an array of char arrays. In this array the function will return names of n faces from the gallery that are most similar to the input image ascending by similarity.
similaritiespointer to an array of n floats. The function will return the similarity values for the n most similar faces in this array, corresponding to the names array. The values are sorted, with the largest similarity value first.
Returns
number of compared faces.

◆ removeDescriptor()

int VisageSDK::VisageFaceRecognition::removeDescriptor ( int  index)

Removes a descriptor at the given index from the gallery.

The remaining descriptors in the gallery above the given index (if any) are shifted down in the gallery array by one place, filling the gap created by removing the descriptor.

Parameters
indexindex of descriptor in the gallery.
Returns
1 on success, 0 on failure. The function fails if index is out of range.

◆ replaceDescriptorName()

int VisageSDK::VisageFaceRecognition::replaceDescriptorName ( const char *  name,
int  index 
)

Replaces the name of a descriptor at the given index in the gallery with new name.

Parameters
namenew descriptor name.
indexindex of descriptor in the gallery.
Returns
1 on success, 0 on failure. The function fails if index is out of range.

◆ saveGallery()

int VisageSDK::VisageFaceRecognition::saveGallery ( const char *  file_name)

Save gallery as a binary file.

Parameters
file_namename of the file (including path if needed) into which the gallery will be saved
Returns
1 on success, 0 on failure. The function fails if the file can not be opened.

Member Data Documentation

◆ is_initialized

bool VisageSDK::VisageFaceRecognition::is_initialized

If the model used in the VisageFaceRecognition is loaded and initialized successfully value of the is_initialized variable is true, otherwise it is false.

Make sure the variable is set to true after instantiation and before using other function of VisageFaceRecognition API. An example of use:

VisageFaceRecognition *m_VFR = new VisageFaceRecognition("vfr/fr.bin");
//
if(m_VFR->is_initialized)
{
int descriptor_size = m_VFR->getDescriptorSize();
// use VisageFaceRecognition API functions
}

Definition at line 113 of file VisageFaceRecognition.h.


The documentation for this class was generated from the following file:
VisageSDK::VisageFaceRecognition::VisageFaceRecognition
VisageFaceRecognition(const char *dataPath)
Constructor.