Ask Your Question

How to create a face recognition algorithm?

asked 2013-05-26 03:49:29 -0500

andrei gravatar image

After playing around with the 3 face recognition algorithms (Eigenface, Fisherface, LBPH) provided in OpenCV, I wanted to try creating my own algorithm. However, after looking through the documentations on FaceRecognizer and Algorithm, I'm at a loss on how to start. Can someone point to me to some tutorials on how to create a face recognition algorithm using OpenCV? Thank you very much!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-05-26 04:28:59 -0500

berak gravatar image

updated 2013-05-26 04:48:43 -0500

the 2 must-have methods you 'd want to implement from the FaceRecognizer interface are:

// given a vector<Mat> of trainimages, and an equally sized vector<int> of labels(1 per image),
// perform the training:
virtual void train(InputArray src, InputArray lbls)
// given 1 testimage, retrieve the closest neighbour-id, and the corresponding distance:
virtual void predict(InputArray src, int& label, double & minDist) const

i'm skipping most of the Algorithm implementation, because it's almost impossible to do without the CV_INIT_ALGORITHM macros (which require access to the contrib/precomp.hpp). thus you can't use the property get() set() functions or the inherited yml saving() / reading(), but hmmm, save that for later!

main goal here is, to get something up & running, so here's the most minimal working facerecognizer, i can think of:

#include "opencv2/contrib/contrib.hpp"
using namespace cv;

// reference impl
//  compare plain pixels 
struct Plain : public FaceRecognizer
    vector<int> labels;
    vector<Mat> imgs;
    int normFlag;

    Plain(int normFlag=CV_L2) : normFlag(normFlag) {}

    virtual void train(InputArray src, InputArray lbls)    {
        labels = lbls.getMat();

    virtual void predict(InputArray src, int& label, double & minDist) const    {
        Mat q = src.getMat();
        minDist = DBL_MAX;
        int minClass = -1;
        for(size_t i = 0; i < imgs.size(); i++) {
            double dist = norm(imgs[i], q, normFlag);
            if(dist < minDist) {
                minDist = dist;
                minClass = labels[i];
        label = minClass;
    virtual int predict(InputArray src) const 
        int pred=-1;
        double conf=-1;
        return pred;
    // save the impl of those for a rainy day:
    virtual void update(InputArrayOfArrays src, InputArray labels) {}
    virtual void save(const std::string& filename) const    {}
    virtual void save(FileStorage& fs) const    {}
    virtual void load(const std::string& filename)    {}
    virtual void load(const FileStorage& fs)    {}

// factory function:
Ptr<FaceRecognizer> createPlainFaceRecognizer(int norm)
    return new Plain(norm);

btw, shameless plug ;)

edit flag offensive delete link more



@berak From next week on I am going to have a better internet connection and then I'll start adding new algorithms and merge your suggestion for uniform patterns. :)

Philipp Wagner gravatar imagePhilipp Wagner ( 2013-05-26 05:55:58 -0500 )edit

aww, cool ;)

berak gravatar imageberak ( 2013-05-26 06:00:12 -0500 )edit

Question Tools


Asked: 2013-05-26 03:49:29 -0500

Seen: 666 times

Last updated: May 26 '13