【计算机视觉】OpenCV人脸识别facerec源码分析1——FaceRecognizer概述

FaceRecognizer类

OpenCV中所有人脸识别的模型都是继承自FaceRecognizer这个基类,这个类提供了人脸识别算法的统一接口。

 
 
[cpp]  view plain  copy
  1. class FaceRecognizer : public Algorithm  
  2. {  
  3. public:  
  4.     //! virtual destructor  
  5.     virtual ~FaceRecognizer() {}  
  6.   
  7.     // Trains a FaceRecognizer.  
  8.     virtual void train(InputArray src, InputArray labels) = 0;  
  9.   
  10.     // Updates a FaceRecognizer.  
  11.     virtual void update(InputArrayOfArrays src, InputArray labels);  
  12.   
  13.     // Gets a prediction from a FaceRecognizer.  
  14.     virtual int predict(InputArray src) const = 0;  
  15.   
  16.     // Predicts the label and confidence for a given sample.  
  17.     virtual void predict(InputArray src, int &label, double &confidence) const = 0;  
  18.   
  19.     // Serializes this object to a given filename.  
  20.     virtual void save(const string& filename) const;  
  21.   
  22.     // Deserializes this object from a given filename.  
  23.     virtual void load(const string& filename);  
  24.   
  25.     // Serializes this object to a given cv::FileStorage.  
  26.     virtual void save(FileStorage& fs) const = 0;  
  27.   
  28.     // Deserializes this object from a given cv::FileStorage.  
  29.     virtual void load(const FileStorage& fs) = 0;  
  30. };  

Algorithm类

Algorithm所有的派生类都提供了一下功能:

  • 虚拟构造器。每个继承Algorithm的算法在程序开始时注册,你可以得到已注册算法的列表,并且根据指定算法名字来创建一个实例。如果你想添加自己的算法,比较好的方法是在你算法名上添加一个前缀,以示区别。(详见Algorithm::create())
  • 通过参数名来设置或检索参数。Algorithm可以通过你指定参数名字符串来提供这样的功能。(详见Algorithm::set()、Algorithm::get())
  • 从XML或YAML文件中读写参数。每个算法都可以将参数写到文件中,并且在需要时读出参数。

FaceRecognizer支持的接口

  • FaceRecognizer::train()用于将人脸图像数据进行训练
  • 根据一张输入的人脸图片进行预测
  • 从指定XML或YAML文件中上载或存储模型参数

设置阈值

人脸识别的一个常用情景是待识别人脸是不是输入训练数据集。这个阈值的设置是在具体的FaceRecognizer的构造器中,并通过从Algorithm继承的set和get方法来使用阈值。

  • 创建模型时设置阈值

    // 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 appropiate parameters:
    Ptr<FaceRecognizer> model = createEigenFaceRecognizer(num_components, threshold);
  • 运行程序时获取或设置阈值

    // 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);
  • 将阈值设置为0.0,如果预测类别号为-1,则说明人脸时未知的,没有在数据库中记录

    Mat img = imread("person1/3.jpg", CV_LOAD_IMAGE_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);

FaceRecognizer函数接口

FaceRecognizer::train

函数定义:

void FaceRecognizer::train(InputArrayOfArrays src, InputArray labels) = 0

函数参数为训练的图片,类型为vector<Mat>;图片所对应的标签,类型为vector<int>。

FaceRecognizer::update

函数定义:

void FaceRecognizer::update(InputArrayOfArrays src, InputArray labels)

该方法为更新一个模型(条件是给模型可以被更新,在OpenCV中提供的人脸识别算法中,只有LBP直方图的方法才可以被更新),更新和训练接口的区别是,训练是清空已有的模型,学习一个新的模型,而更新不会删除之前的模型数据。

FaceRecognizer::predict

函数定义:

int FaceRecognizer::predict(InputArray src) const = 0
void FaceRecognizer::predict(InputArray src, int& label, double& confidence) const = 0

第三个参数是对于预测类别的相关置信度。
const后缀表示predict函数不会影响模型内部状态,所以这个方法可以安全被不同线程调用。
下面的小例子是根据训练模型来进行预测。

using namespace cv;
// 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);

FaceRecognizer::save

函数定义:

void FaceRecognizer::save(const string& filename) const
void FaceRecognizer::save(FileStorage& fs) const

第一个定义是根据XML或者YAML文件名来存储模型,第二个定义是将模型存成FileStorage实例。

FaceRecognizer::load

函数定义:

void FaceRecognizer::load(const string& filename)
void FaceRecognizer::load(const FileStorage& fs) = 0

这里载入模型参数和save函数的参数意义一样。

建立模型接口

Ptr<FaceRecognizer> createEigenFaceRecognizer(int num_components=0, double threshold=DBL_MAX);

Ptr<FaceRecognizer> createFisherFaceRecognizer(int num_components=0, double threshold=DBL_MAX);

Ptr<FaceRecognizer> createLBPHFaceRecognizer(int radius=1, int neighbors=8, int grid_x=8, int grid_y=8, double threshold=DBL_MAX);

这三个接口分别对应OpenCV的三个人脸识别算法,均返回一个智能指针Ptr类的实例。

小结

这一小节,介绍了OpenCV中人脸识别功能对应的类FaceRecognizer的大体框架,介绍了相关接口和使用方法。在后面的博客,将具体以LBPH算法为例,介绍其原理和匹配源码。

参考资料

OpenCV doc:Algorithm

转载请注明作者Jason Ding及其出处
Github主页(http://jasonding1354.github.io/)
CSDN博客(http://blog.csdn.net/jasonding1354)
简书主页(http://www.jianshu.com/users/2bd9b48f6ea8/latest_articles)

猜你喜欢

转载自blog.csdn.net/dp323/article/details/80539399