OpenCV 2.4.3.1

org.opencv.contrib
Class FaceRecognizer

java.lang.Object
  extended by org.opencv.core.Algorithm
      extended by org.opencv.contrib.FaceRecognizer

public class FaceRecognizer
extends Algorithm

All face recognition models in OpenCV are derived from the abstract base class "FaceRecognizer", which provides a unified access to all face recongition algorithms in OpenCV.

class FaceRecognizer : public Algorithm

// C++ code:

public:

//! virtual destructor

virtual ~FaceRecognizer() {}

// Trains a FaceRecognizer.

virtual void train(InputArray src, InputArray labels) = 0;

// Updates a FaceRecognizer.

virtual void update(InputArrayOfArrays src, InputArray labels);

// Gets a prediction from a FaceRecognizer.

virtual int predict(InputArray src) const = 0;

// Predicts the label and confidence for a given sample.

virtual void predict(InputArray src, int &label, double &confidence) const = 0;

// Serializes this object to a given filename.

virtual void save(const string& filename) const;

// Deserializes this object from a given filename.

virtual void load(const string& filename);

// Serializes this object to a given cv.FileStorage.

virtual void save(FileStorage& fs) const = 0;

// Deserializes this object from a given cv.FileStorage.

virtual void load(const FileStorage& fs) = 0;

};

See Also:
org.opencv.contrib.FaceRecognizer : public Algorithm

Field Summary
 
Fields inherited from class org.opencv.core.Algorithm
nativeObj
 
Constructor Summary
protected FaceRecognizer(long addr)
           
 
Method Summary
protected  void finalize()
           
 void load(java.lang.String filename)
          Loads a "FaceRecognizer" and its model state.
 void predict(Mat src, int[] label, double[] confidence)
          Predicts a label and associated confidence (e.g. distance) for a given input image.
 void save(java.lang.String filename)
          Saves a "FaceRecognizer" and its model state.
 void train(java.util.List<Mat> src, Mat labels)
          Trains a FaceRecognizer with given data and associated labels.
 void update(java.util.List<Mat> src, Mat labels)
          Updates a FaceRecognizer with given data and associated labels.
 
Methods inherited from class org.opencv.core.Algorithm
getBool, getDouble, getInt, getMat, getMatVector, getString, paramHelp, paramType, setBool, setDouble, setInt, setMat, setMatVector, setString
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FaceRecognizer

protected FaceRecognizer(long addr)
Method Detail

finalize

protected void finalize()
                 throws java.lang.Throwable
Overrides:
finalize in class Algorithm
Throws:
java.lang.Throwable

load

public void load(java.lang.String filename)

Loads a "FaceRecognizer" and its model state.

Loads a persisted model and state from a given XML or YAML file. Every "FaceRecognizer" has to overwrite FaceRecognizer.load(FileStorage& fs) to enable loading the model state. FaceRecognizer.load(FileStorage& fs) in turn gets called by FaceRecognizer.load(const string& filename), to ease saving a model.

Parameters:
filename - a filename
See Also:
org.opencv.contrib.FaceRecognizer.load

predict

public void predict(Mat src,
                    int[] label,
                    double[] confidence)

Predicts a label and associated confidence (e.g. distance) for a given input image.

The suffix const means that prediction does not affect the internal model state, so the method can be safely called from within different threads.

The following example shows how to get a prediction from a trained model: using namespace cv;

// C++ code:

// Do your initialization here (create the cv.FaceRecognizer model)...

//...

// Read in a sample image:

Mat img = imread("person1/3.jpg", CV_LOAD_IMAGE_GRAYSCALE);

// And get a prediction from the cv.FaceRecognizer:

int predicted = model->predict(img);

Or to get a prediction and the associated confidence (e.g. distance):

using namespace cv;

// C++ code:

// Do your initialization here (create the cv.FaceRecognizer model)...

//...

Mat img = imread("person1/3.jpg", CV_LOAD_IMAGE_GRAYSCALE);

// Some variables for the predicted label and associated confidence (e.g. distance):

int predicted_label = -1;

double predicted_confidence = 0.0;

// Get the prediction and associated confidence from the model

model->predict(img, predicted_label, predicted_confidence);

Parameters:
src - Sample image to get a prediction from.
label - The predicted label for the given image.
confidence - Associated confidence (e.g. distance) for the predicted label.
See Also:
org.opencv.contrib.FaceRecognizer.predict

save

public void save(java.lang.String filename)

Saves a "FaceRecognizer" and its model state.

Saves this model to a given filename, either as XML or YAML.

Saves this model to a given "FileStorage".

Every "FaceRecognizer" overwrites FaceRecognizer.save(FileStorage& fs) to save the internal model state. FaceRecognizer.save(const string& filename) saves the state of a model to the given filename.

The suffix const means that prediction does not affect the internal model state, so the method can be safely called from within different threads.

Parameters:
filename - The filename to store this "FaceRecognizer" to (either XML/YAML).
See Also:
org.opencv.contrib.FaceRecognizer.save

train

public void train(java.util.List<Mat> src,
                  Mat labels)

Trains a FaceRecognizer with given data and associated labels.

The following source code snippet shows you how to learn a Fisherfaces model on a given set of images. The images are read with "imread" and pushed into a std.vector. The labels of each image are stored within a std.vector (you could also use a "Mat" of type "CV_32SC1"). Think of the label as the subject (the person) this image belongs to, so same subjects (persons) should have the same label. For the available "FaceRecognizer" you don't have to pay any attention to the order of the labels, just make sure same persons have the same label: // holds images and labels

// C++ code:

vector images;

vector labels;

// images for first person

images.push_back(imread("person0/0.jpg", CV_LOAD_IMAGE_GRAYSCALE)); labels.push_back(0);

images.push_back(imread("person0/1.jpg", CV_LOAD_IMAGE_GRAYSCALE)); labels.push_back(0);

images.push_back(imread("person0/2.jpg", CV_LOAD_IMAGE_GRAYSCALE)); labels.push_back(0);

// images for second person

images.push_back(imread("person1/0.jpg", CV_LOAD_IMAGE_GRAYSCALE)); labels.push_back(1);

images.push_back(imread("person1/1.jpg", CV_LOAD_IMAGE_GRAYSCALE)); labels.push_back(1);

images.push_back(imread("person1/2.jpg", CV_LOAD_IMAGE_GRAYSCALE)); labels.push_back(1);

Now that you have read some images, we can create a new "FaceRecognizer". In this example I'll create a Fisherfaces model and decide to keep all of the possible Fisherfaces:

// Create a new Fisherfaces model and retain all available Fisherfaces,

// C++ code:

// this is the most common usage of this specific FaceRecognizer:

//

Ptr model = createFisherFaceRecognizer();

And finally train it on the given dataset (the face images and labels):

// This is the common interface to train all of the available cv.FaceRecognizer

// C++ code:

// implementations:

//

model->train(images, labels);

Parameters:
src - The training images, that means the faces you want to learn. The data has to be given as a vector.
labels - The labels corresponding to the images have to be given either as a vector or a
See Also:
org.opencv.contrib.FaceRecognizer.train

update

public void update(java.util.List<Mat> src,
                   Mat labels)

Updates a FaceRecognizer with given data and associated labels.

This method updates a (probably trained) "FaceRecognizer", but only if the algorithm supports it. The Local Binary Patterns Histograms (LBPH) recognizer (see "createLBPHFaceRecognizer") can be updated. For the Eigenfaces and Fisherfaces method, this is algorithmically not possible and you have to re-estimate the model with "FaceRecognizer.train". In any case, a call to train empties the existing model and learns a new model, while update does not delete any model data. // Create a new LBPH model (it can be updated) and use the default parameters,

// C++ code:

// this is the most common usage of this specific FaceRecognizer:

//

Ptr model = createLBPHFaceRecognizer();

// This is the common interface to train all of the available cv.FaceRecognizer

// implementations:

//

model->train(images, labels);

// Some containers to hold new image:

vector newImages;

vector newLabels;

// You should add some images to the containers:

//

//...

//

// Now updating the model is as easy as calling:

model->update(newImages,newLabels);

// This will preserve the old model data and extend the existing model

// with the new features extracted from newImages!

Calling update on an Eigenfaces model (see "createEigenFaceRecognizer"), which doesn't support updating, will throw an error similar to:

OpenCV Error: The function/feature is not implemented (This FaceRecognizer (FaceRecognizer.Eigenfaces) does not support updating, you have to use FaceRecognizer.train to update it.) in update, file /home/philipp/git/opencv/modules/contrib/src/facerec.cpp, line 305

// C++ code:

terminate called after throwing an instance of 'cv.Exception'

Please note: The "FaceRecognizer" does not store your training images, because this would be very memory intense and it's not the responsibility of te "FaceRecognizer" to do so. The caller is responsible for maintaining the dataset, he want to work with.

Parameters:
src - The training images, that means the faces you want to learn. The data has to be given as a vector.
labels - The labels corresponding to the images have to be given either as a vector or a
See Also:
org.opencv.contrib.FaceRecognizer.update

Official OpenCV 2.4 Documentation