Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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_NORM_L2) : normFlag(normFlag) {}

    virtual void train(InputArray src, InputArray lbls)    {
        src.getMatVector(imgs);
        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;
        predict(src,pred,conf);
        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> createLinearFaceRecognizer(int norm)
{
    return new Plain(norm);
}

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_NORM_L2) : normFlag(normFlag) {}

    virtual void train(InputArray src, InputArray lbls)    {
        src.getMatVector(imgs);
        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;
        predict(src,pred,conf);
        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> createLinearFaceRecognizer(int norm)
{
    return new Plain(norm);
}

btw, shameless plug ;)

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_NORM_L2) normFlag=CV_L2) : normFlag(normFlag) {}

    virtual void train(InputArray src, InputArray lbls)    {
        src.getMatVector(imgs);
        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;
        predict(src,pred,conf);
        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> createLinearFaceRecognizer(int norm)
{
    return new Plain(norm);
}

btw, shameless plug ;)

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)    {
        src.getMatVector(imgs);
        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;
        predict(src,pred,conf);
        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> createLinearFaceRecognizer(int createPlainFaceRecognizer(int norm)
{
    return new Plain(norm);
}

btw, shameless plug ;)