OpenCV453
公開メンバ関数 | 公開変数類 | 限定公開変数類 | 全メンバ一覧
cv::face::FaceRecognizer クラスabstract

Abstract base class for all face recognition models [詳解]

#include <face.hpp>

cv::Algorithmを継承しています。

cv::face::BasicFaceRecognizer, cv::face::LBPHFaceRecognizerに継承されています。

公開メンバ関数

virtual CV_WRAP void train (InputArrayOfArrays src, InputArray labels)=0
 Trains a FaceRecognizer with given data and associated labels. [詳解]
 
virtual CV_WRAP void update (InputArrayOfArrays src, InputArray labels)
 Updates a FaceRecognizer with given data and associated labels. [詳解]
 
 CV_WRAP_AS (predict_label) int predict(InputArray src) const
 
CV_WRAP void predict (InputArray src, CV_OUT int &label, CV_OUT double &confidence) const
 Predicts a label and associated confidence (e.g. distance) for a given input image. [詳解]
 
 CV_WRAP_AS (predict_collect) virtual void predict(InputArray src
 
  • if implemented - send all result of prediction to collector that can be used for somehow custom result handling
[詳解]
 
virtual CV_WRAP void write (const String &filename) const
 Saves a FaceRecognizer and its model state. [詳解]
 
virtual CV_WRAP void read (const String &filename)
 Loads a FaceRecognizer and its model state. [詳解]
 
virtual void write (FileStorage &fs) const CV_OVERRIDE=0
 
virtual void read (const FileNode &fn) CV_OVERRIDE=0
 
virtual bool empty () const CV_OVERRIDE=0
 
virtual CV_WRAP void setLabelInfo (int label, const String &strInfo)
 Sets string info for the specified model's label. [詳解]
 
virtual CV_WRAP String getLabelInfo (int label) const
 Gets string information by label. [詳解]
 
virtual CV_WRAP std::vector< int > getLabelsByString (const String &str) const
 Gets vector of labels by string. [詳解]
 
virtual double getThreshold () const =0
 threshold parameter accessor - required for default BestMinDist collector [詳解]
 
virtual void setThreshold (double val)=0
 Sets threshold of model [詳解]
 
- 基底クラス cv::Algorithm に属する継承公開メンバ関数
virtual CV_WRAP void clear ()
 Clears the algorithm state [詳解]
 
CV_WRAP void write (const Ptr< FileStorage > &fs, const String &name=String()) const
 simplified API for language bindings これはオーバーロードされたメンバ関数です。利便性のために用意されています。元の関数との違いは引き数のみです。
 
virtual CV_WRAP void save (const String &filename) const
 
virtual CV_WRAP String getDefaultName () const
 

公開変数類

Ptr< PredictCollector > collector const = 0
 

限定公開変数類

std::map< int, String > _labelsInfo
 

その他の継承メンバ

- 基底クラス cv::Algorithm に属する継承静的公開メンバ関数
template<typename _Tp >
static Ptr< _Tp > read (const FileNode &fn)
 Reads algorithm from the file node [詳解]
 
template<typename _Tp >
static Ptr< _Tp > load (const String &filename, const String &objname=String())
 Loads algorithm from the file [詳解]
 
template<typename _Tp >
static Ptr< _Tp > loadFromString (const String &strModel, const String &objname=String())
 Loads algorithm from a String [詳解]
 
- 基底クラス cv::Algorithm に属する継承限定公開メンバ関数
void writeFormat (FileStorage &fs) const
 

詳解

Abstract base class for all face recognition models

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.

Description

I'll go a bit more into detail explaining FaceRecognizer, because it doesn't look like a powerful interface at first sight. But: Every FaceRecognizer is an Algorithm, so you can easily get/set all model internals (if allowed by the implementation). Algorithm is a relatively new OpenCV concept, which is available since the 2.4 release. I suggest you take a look at its description.

Algorithm provides the following features for all derived classes:

Moreover every FaceRecognizer supports the:

覚え書き
When using the FaceRecognizer interface in combination with Python, please stick to Python 2. Some underlying scripts like create_csv will not work in other versions, like Python 3. Setting the Thresholds +++++++++++++++++++++++

Sometimes you run into the situation, when you want to apply a threshold on the prediction. A common scenario in face recognition is to tell, whether a face belongs to the training dataset or if it is unknown. You might wonder, why there's no public API in FaceRecognizer to set the threshold for the prediction, but rest assured: It's supported. It just means there's no generic way in an abstract class to provide an interface for setting/getting the thresholds of every possible FaceRecognizer algorithm. The appropriate place to set the thresholds is in the constructor of the specific FaceRecognizer and since every FaceRecognizer is a Algorithm (see above), you can get/set the thresholds at runtime!

Here is an example of setting a threshold for the Eigenfaces method, when creating the model:

// Let's say we want to keep 10 Eigenfaces and have a threshold value of 10.0
int num_components = 10;
double threshold = 10.0;
// Then if you want to have a cv::FaceRecognizer with a confidence threshold,
// create the concrete implementation with the appropriate parameters:
Ptr<FaceRecognizer> model = EigenFaceRecognizer::create(num_components, threshold);
static CV_WRAP Ptr< EigenFaceRecognizer > create(int num_components=0, double threshold=DBL_MAX)
CV_EXPORTS_W double threshold(InputArray src, OutputArray dst, double thresh, double maxval, int type)
Applies a fixed-level threshold to each array element.

Sometimes it's impossible to train the model, just to experiment with threshold values. Thanks to Algorithm it's possible to set internal model thresholds during runtime. Let's see how we would set/get the prediction for the Eigenface model, we've created above:

// The following line reads the threshold from the Eigenfaces model:
double current_threshold = model->getDouble("threshold");
// And this line sets the threshold to 0.0:
model->set("threshold", 0.0);

If you've set the threshold to 0.0 as we did above, then:

//
Mat img = imread("person1/3.jpg", IMREAD_GRAYSCALE);
// Get a prediction from the model. Note: We've set a threshold of 0.0 above,
// since the distance is almost always larger than 0.0, you'll get -1 as
// label, which indicates, this face is unknown
int predicted_label = model->predict(img);
// ...
@ IMREAD_GRAYSCALE
If set, always convert image to the single channel grayscale image (codec internal conversion).
Definition: imgcodecs.hpp:71
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR)
Loads an image from a file.

is going to yield -1 as predicted label, which states this face is unknown.

Getting the name of a FaceRecognizer

Since every FaceRecognizer is a Algorithm, you can use Algorithm::name to get the name of a FaceRecognizer:

// Create a FaceRecognizer:
Ptr<FaceRecognizer> model = EigenFaceRecognizer::create();
// And here's how to get its name:
String name = model->name();

関数詳解

◆ CV_WRAP_AS() [1/2]

cv::face::FaceRecognizer::CV_WRAP_AS ( predict_collect  )

  • if implemented - send all result of prediction to collector that can be used for somehow custom result handling

引数
srcSample image to get a prediction from.
collectorUser-defined collector object that accepts all results

To implement this method u just have to do same internal cycle as in predict(InputArray src, CV_OUT int &label, CV_OUT double &confidence) but not try to get "best@ result, just resend it to caller side with given collector

◆ CV_WRAP_AS() [2/2]

cv::face::FaceRecognizer::CV_WRAP_AS ( predict_label  ) const

これはオーバーロードされたメンバ関数です。利便性のために用意されています。元の関数との違いは引き数のみです。

◆ empty()

virtual bool cv::face::FaceRecognizer::empty ( ) const
pure virtual

これはオーバーロードされたメンバ関数です。利便性のために用意されています。元の関数との違いは引き数のみです。

cv::Algorithmを再実装しています。

cv::face::BasicFaceRecognizerで実装されています。

◆ getLabelInfo()

virtual CV_WRAP String cv::face::FaceRecognizer::getLabelInfo ( int  label) const
virtual

Gets string information by label.

If an unknown label id is provided or there is no label information associated with the specified label id the method returns an empty string.

◆ getLabelsByString()

virtual CV_WRAP std::vector< int > cv::face::FaceRecognizer::getLabelsByString ( const String &  str) const
virtual

Gets vector of labels by string.

The function searches for the labels containing the specified sub-string in the associated string info.

◆ getThreshold()

virtual double cv::face::FaceRecognizer::getThreshold ( ) const
pure virtual

threshold parameter accessor - required for default BestMinDist collector

cv::face::BasicFaceRecognizer, cv::face::LBPHFaceRecognizerで実装されています。

◆ predict()

CV_WRAP void cv::face::FaceRecognizer::predict ( InputArray  src,
CV_OUT int &  label,
CV_OUT double &  confidence 
) const

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

引数
srcSample image to get a prediction from.
labelThe predicted label for the given image.
confidenceAssociated confidence (e.g. distance) for the predicted label.

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;
// Do your initialization here (create the cv::FaceRecognizer model) ...
// ...
// Read in a sample image:
Mat img = imread("person1/3.jpg", IMREAD_GRAYSCALE);
// And get a prediction from the cv::FaceRecognizer:
int predicted = model->predict(img);
"black box" representation of the file storage associated with a file on disk.
Definition: aruco.hpp:75

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

using namespace cv;
// Do your initialization here (create the cv::FaceRecognizer model) ...
// ...
Mat img = imread("person1/3.jpg", IMREAD_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);

◆ read() [1/2]

virtual void cv::face::FaceRecognizer::read ( const FileNode fn)
pure virtual

これはオーバーロードされたメンバ関数です。利便性のために用意されています。元の関数との違いは引き数のみです。

cv::Algorithmを再実装しています。

cv::face::BasicFaceRecognizer, cv::face::BasicFaceRecognizerで実装されています。

◆ read() [2/2]

virtual CV_WRAP void cv::face::FaceRecognizer::read ( const String &  filename)
virtual

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.

cv::face::BasicFaceRecognizerで再実装されています。

◆ setLabelInfo()

virtual CV_WRAP void cv::face::FaceRecognizer::setLabelInfo ( int  label,
const String &  strInfo 
)
virtual

Sets string info for the specified model's label.

The string info is replaced by the provided value if it was set before for the specified label.

◆ setThreshold()

virtual void cv::face::FaceRecognizer::setThreshold ( double  val)
pure virtual

Sets threshold of model

cv::face::BasicFaceRecognizer, cv::face::LBPHFaceRecognizerで実装されています。

◆ train()

virtual CV_WRAP void cv::face::FaceRecognizer::train ( InputArrayOfArrays  src,
InputArray  labels 
)
pure virtual

Trains a FaceRecognizer with given data and associated labels.

引数
srcThe training images, that means the faces you want to learn. The data has to be given as a vector<Mat>.
labelsThe labels corresponding to the images have to be given either as a vector<int> or a Mat of type CV_32SC1.

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<Mat>. The labels of each image are stored within a std::vector<int> (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
vector<Mat> images;
vector<int> labels;
// using Mat of type CV_32SC1
// Mat labels(number_of_samples, 1, CV_32SC1);
// images for first person
images.push_back(imread("person0/0.jpg", IMREAD_GRAYSCALE)); labels.push_back(0);
images.push_back(imread("person0/1.jpg", IMREAD_GRAYSCALE)); labels.push_back(0);
images.push_back(imread("person0/2.jpg", IMREAD_GRAYSCALE)); labels.push_back(0);
// images for second person
images.push_back(imread("person1/0.jpg", IMREAD_GRAYSCALE)); labels.push_back(1);
images.push_back(imread("person1/1.jpg", IMREAD_GRAYSCALE)); labels.push_back(1);
images.push_back(imread("person1/2.jpg", IMREAD_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,
// this is the most common usage of this specific FaceRecognizer:
//
Ptr<FaceRecognizer> model = FisherFaceRecognizer::create();
static CV_WRAP Ptr< FisherFaceRecognizer > create(int num_components=0, double threshold=DBL_MAX)

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
// implementations:
//
model->train(images, labels);

◆ update()

virtual CV_WRAP void cv::face::FaceRecognizer::update ( InputArrayOfArrays  src,
InputArray  labels 
)
virtual

Updates a FaceRecognizer with given data and associated labels.

引数
srcThe training images, that means the faces you want to learn. The data has to be given as a vector<Mat>.
labelsThe labels corresponding to the images have to be given either as a vector<int> or a Mat of type CV_32SC1.

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,
// this is the most common usage of this specific FaceRecognizer:
//
Ptr<FaceRecognizer> model = LBPHFaceRecognizer::create();
// 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<Mat> newImages;
vector<int> 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!
static CV_WRAP Ptr< LBPHFaceRecognizer > create(int radius=1, int neighbors=8, int grid_x=8, int grid_y=8, double threshold=DBL_MAX)

Calling update on an Eigenfaces model (see EigenFaceRecognizer::create), 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
terminate called after throwing an instance of 'cv::Exception'
virtual CV_WRAP void train(InputArrayOfArrays src, InputArray labels)=0
Trains a FaceRecognizer with given data and associated labels.
virtual CV_WRAP void update(InputArrayOfArrays src, InputArray labels)
Updates a FaceRecognizer with given data and associated labels.
CV_EXPORTS_W void line(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws a line segment connecting two points.
覚え書き
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.

◆ write() [1/2]

virtual CV_WRAP void cv::face::FaceRecognizer::write ( const String &  filename) const
virtual

Saves a FaceRecognizer and its model state.

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

引数
filenameThe filename to store this FaceRecognizer to (either XML/YAML).

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.

cv::face::BasicFaceRecognizerで再実装されています。

◆ write() [2/2]

virtual void cv::face::FaceRecognizer::write ( FileStorage fs) const
pure virtual

これはオーバーロードされたメンバ関数です。利便性のために用意されています。元の関数との違いは引き数のみです。 Saves this model to a given FileStorage.

引数
fsThe FileStorage to store this FaceRecognizer to.

cv::Algorithmを再実装しています。

cv::face::BasicFaceRecognizer, cv::face::BasicFaceRecognizerで実装されています。


このクラス詳解は次のファイルから抽出されました: