Training the face recognition system from collected faces

After you have collected enough faces for each person to recognize, you must train the system to learn the data using a machine learning algorithm suited for face recognition. There are many different face recognition algorithms in the literature, the simplest of which are Eigenfaces and artificial neural networks. Eigenfaces tends to work better than ANNs, and despite its simplicity, it tends to work almost as well as many more complex face recognition algorithms, so it has become very popular as the basic face recognition algorithm for beginners, as well as for new algorithms to be compared to.

Any reader who wishes to work further on face recognition is recommended to read the theory behind the following:

However, you don't need to understand the theory of these algorithms in order to use them as shown in this book. Thanks to the OpenCV team and Philipp Wagner's libfacerec contribution, OpenCV v2.4.1 provided cv::Algorithm as a simple and generic method to perform face recognition using one of several different algorithms (even selectable at runtime) without necessarily understanding how they are implemented. You can find the available algorithms in your version of OpenCV by using the Algorithm::getList() function, such as with the following code:

    vector<string> algorithms; 
Algorithm::getList(algorithms);
cout << "Algorithms: " << algorithms.size() << endl;
for (auto& algorithm:algorithms) {
cout << algorithm << endl;
}

Here are the three face recognition algorithms available in OpenCV v2.4.1:

More information on these face recognition algorithm implementations can be found with documentation, samples, and Python equivalents for each of them on Philipp Wagner's websites (http://bytefish.de/blog and http://bytefish.de/dev/libfacerec/).

These face recognition-algorithms are available through the FaceRecognizer class in OpenCV's contrib module. Due to dynamic linking, it is possible that your program is linked to the contrib module, but it is not actually loaded at runtime (if it was deemed as not required). So it is recommended to call the cv::initModule_contrib() function before trying to access the FaceRecognizer algorithms. This function is only available from OpenCV v2.4.1, so it also ensures that the face recognition algorithms are at least available to you at compile time:

    // Load the "contrib" module is dynamically at runtime. 
bool haveContribModule = initModule_contrib();
if (!haveContribModule) {
cerr << "ERROR: The 'contrib' module is needed for ";
cerr << "FaceRecognizer but hasn't been loaded to OpenCV!";
cerr << endl;
exit(1);
}

To use one of the face recognition algorithms, we must create a FaceRecognizer object using the cv::Algorithm::create<FaceRecognizer>() function. We pass the name of the face recognition algorithm we want to use as a string to this create function. This will give us access to that algorithm, if it is available in the OpenCV version. So, it may be used as a runtime error check to ensure the user has OpenCV v2.4.1 or newer. An example of this is shown as follows:

    string facerecAlgorithm = "FaceRecognizer.Fisherfaces"; 
Ptr<FaceRecognizer> model;
// Use OpenCV's new FaceRecognizer in the "contrib" module:
model = Algorithm::create<FaceRecognizer>(facerecAlgorithm);
if (model.empty()) {
cerr << "ERROR: The FaceRecognizer [" << facerecAlgorithm;
cerr << "] is not available in your version of OpenCV. ";
cerr << "Please update to OpenCV v2.4.1 or newer." << endl;
exit(1);
}

Once we have loaded the FaceRecognizer algorithm, we simply call the FaceRecognizer::train() function with our collected face data, as follows:

    // Do the actual training from the collected faces. 
model->train(preprocessedFaces, faceLabels);

This one line of code will run the whole face recognition training algorithm that you selected (for example, Eigenfaces, Fisherfaces, or potentially other algorithms). If you have just a few people with less than 20 faces, then this training should return very quickly, but if you have many people with many faces, it is possible that the train() function will take several seconds, or even minutes, to process all the data.