android opencv学习第三天 FaceRecognizer(翻译)(2)

http://docs.opencv.org/modules/contrib/doc/facerec/facerec_api.html

可通过点击FaceRecognizer API跳转



FaceRecognizer

class  FaceRecognizer :  public  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.

在opencv中,所有的人脸识别模式(face recognition models)都是由抽象基础类(abstract base class FaceRecognizer)衍生的,他提供了统一接口给所有的人脸识别算法  在opencv中。 

class FaceRecognizer : public Algorithm
{
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;
};

Description

我会更纤细的解释 FaceRecognizer,因为他一看起来不像是一个强大的接口。 但是: 任何 FaceRecognizer 都是 Algorithm(算法), 因此你可以很容易的获取或者设置内部模型(get/set all model internals) (如果允许实施(if allowed by the implementation)). Algorithm 是一个相对新的 OpenCV 概念, 他出现于 2.4 release版本. 我建议你稍微看看他的描述。

Algorithm 提供了下面的特效对于所有衍生类(继承于他的类?)(derived classes):

  • 这样调用(So called)虚构函数 “virtual constructor”. 这就是仍和Algorithm衍生者( Algorithm derivative )都是在程序开始的时候注册,并且,你可以获得一个注册算法列表和创建一个实例对于特别的算法通过他的名字 (see Algorithm::create()). 如果你计划添加你自己的算法,一个很好的做法是添加一个前缀用来和其他算法区分。
  • Setting/Retrieving algorithm parameters by name(通过名字设置或者检索算法).如果你使用视频捕获功能从OpenCV highgui(图形交互和媒体接口) module,你可能熟悉cvSetCaptureProperty(),cvGetCaptureProperty()VideoCapture::set() and VideoCapture::get()Algorithm 提供了相似的方法替代integer id’s 你指定的参数名字names as text strings. See Algorithm::set() and Algorithm::get() for details.(点击查看更多细节)
  • Reading and writing parameters from/to XML or YAML files(读写参数从xml或者yaml文件).任何衍生的算法(Algorithm derivative)能够存数所有它的参数并且读取回他们,这里没有必要去重新继承他们,任何时候( There is no need to re-implement it each time.)

Moreover every FaceRecognizer supports the:(此外,任何FaceRecognizer都支持这些)

  • Training of a FaceRecognizer with FaceRecognizer::train() on a given set of images (your face database!).训练FaceRecognizer通过FaceRecognizer::train()
  • Prediction of a given sample image, that means a face. The image is given as a Mat.(预测给定的相同图片,脸,这图片会给出一个Mat的数据类型)
  • Loading/Saving the model state from/to a given XML or YAML.(加载或者保存状态从给定的xml或者yaml)

Note

 

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.(貌似是关于Python的,不管)

Setting the Thresholds(设置门槛或者说是阈)

有时候你运行在这种情况,你想要用这个阈用于预测。一个通用的脚本在人脸识别中告诉,当人脸属于训练的数据几何或者他是未知的你可能不知道为什么这里没有公开的Api在(public API )in FaceRecognizer 中为了预测去设置这个阈to set the threshold for the prediction,但是放心(rest assured): 它是支持的。这只是意味着这里没有痛哟in个的方法在一个抽象类中去提供这个接口用于设置或者获取(setting/getting ) 这个阈于任何 可能的人脸识别算法(FaceRecognizer algorithm.这个适当的地方去设置这个阈是在构造方法中于明确的人脸识别(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 appropiate parameters:
Ptr<FaceRecognizer> model = createEigenFaceRecognizer(num_components, threshold);

Sometimes it’s impossible(不可能) to train the model, just to experiment(实验) with threshold values. Thanks toAlgorithm 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:(有时候他不可能去训练这个模式,做做关于阈值的实验,感谢Algorithm,他可能去设置内部模式阈在运行期间,让我们看看怎么预测)

// 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", 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);
// ...

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 aFaceRecognizer:

// Create a FaceRecognizer:
Ptr<FaceRecognizer> model = createEigenFaceRecognizer();
// And here's how to get its name:
std::string name = model->name();

FaceRecognizer::train

Trains a FaceRecognizer with given data and associated labels(相关标签).

C++:  void  FaceRecognizer:: train (InputArrayOfArrays  src, InputArray  labels )  = 0
Parameters:
  • src – The training images, that means the faces you want to learn. The data has to be given as a vector<Mat>.
  • labels – The labels corresponding to the images have to be given either as avector<int> or a

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(不用关系labels的顺序), just make sure same persons have the same label:只要确定同一个人有同样的label

// holds images and labels
vector<Mat> images;
vector<int> 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:(读取了一些图片,我们可以创建FaceRecognizer了,在这个例子中,我会船桨一个FisherFaces Model并且决定保持所有的可能FisherFaces(我也不明白什么意思))

// Create a new Fisherfaces model and retain all available Fisherfaces,
// this is the most common usage of this specific FaceRecognizer:
//
Ptr<FaceRecognizer> 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
// implementations:
//
model->train(images, labels);

FaceRecognizer::update

Updates a FaceRecognizer with given data and associated(相关的) labels.

C++:  void  FaceRecognizer:: update (InputArrayOfArrays  src, InputArray  labels )
Parameters:
  • src – The training images, that means the faces you want to learn. The data has to be given as a vector<Mat>.
  • labels – The labels corresponding to the images have to be given either as avector<int> or a

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.(任何情况清空这已经存在的model和学习一个新的model,调用train当update没有删除任何model数据的时候)

// 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 =  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<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!

Calling update on an Eigenfaces model (see createEigenFaceRecognizer()), (一个不支持update,会爆出一个异常和下面显示的类似)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'

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.

FaceRecognizer::predict(预测)

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

Predicts a label and associated(有联系的) confidence(信心) (e.g. distance) for a given input image.

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.

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:(怎么获得预测,从一个训练好的model中)

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

Saves a FaceRecognizer and its model state.

C++:  void  FaceRecognizer:: save (const string&  filename )  const

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

Parameters:
  • filename – The filename to store this FaceRecognizer to (either XML/YAML).
C++:  void  FaceRecognizer:: save (FileStorage&  fs )  const

Saves this model to a given FileStorage.

Parameters:

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.把model的状态写入一个给定的文件名

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

FaceRecognizer::load

Loads a FaceRecognizer and its model state.

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

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.

createEigenFaceRecognizer

C++:  Ptr<FaceRecognizer>  createEigenFaceRecognizer (int  num_components=0, double threshold=DBL_MAX )
Parameters:
  • num_components – The number of components (read: Eigenfaces) kept for this Prinicpal Component Analysis. As a hint: There’s no rule how many components (read: Eigenfaces) should be kept for good reconstruction capabilities. It is based on your input data, so experiment with the number. Keeping 80 components should almost always be sufficient.
  • threshold – The threshold applied in the prediciton.

Notes:

  • Training and prediction must be done on grayscale(灰度) images, use cvtColor() to convert between the color spaces.
  • THE EIGENFACES METHOD MAKES THE ASSUMPTION(假设), THAT THE TRAINING AND TEST IMAGES ARE OF EQUAL SIZE. (caps-lock, because I got so many mails asking for this). You have to make sure your input data has the correct shape(确认你的输入数据有对的形状), else a meaningful exception is thrown. Use resize() to resize the images.
  • This model does not support updating.(这个model不支持updating)

Model internal data:

  • num_components see createEigenFaceRecognizer().
  • threshold see createEigenFaceRecognizer().
  • eigenvalues The eigenvalues (特征值)for this Principal Component Analysis(主成分分析) (ordered descending).
  • eigenvectors The eigenvectors(特征向量) for this Principal Component Analysis(主成分分析) (ordered by their eigenvalue).
  • mean The sample mean calculated from the training data.
  • projections The projections(投影) of the training data.
  • labels The threshold applied in the prediction. If the distance to the nearest neighbor is larger than the threshold, this method returns -1.(这个阀在预测中被使用,如果这个距离最近的邻居大于阀,这个方法返回-1)

createFisherFaceRecognizer

C++:  Ptr<FaceRecognizer>  createFisherFaceRecognizer (int  num_components=0, double threshold=DBL_MAX )
Parameters:
  • num_components – The number of components (read: Fisherfaces) kept for this Linear Discriminant Analysis with the Fisherfaces criterion. It’s useful to keep all components, that means the number of your classes c (read: subjects, persons you want to recognize). If you leave this at the default (0) or set it to a value less-equal 0 or greater (c-1), it will be set to the correct number (c-1) automatically.
  • threshold – The threshold applied in the prediction. If the distance to the nearest neighbor is larger than the threshold, this method returns -1.

Notes:

  • Training and prediction must be done on grayscale images, use cvtColor() to convert between the color spaces.
  • THE FISHERFACES METHOD MAKES THE ASSUMPTION, THAT THE TRAINING AND TEST IMAGES ARE OF EQUAL SIZE. (caps-lock, because I got so many mails asking for this). You have to make sure your input data has the correct shape, else a meaningful exception is thrown. Useresize() to resize the images.
  • This model does not support updating.

Model internal data:

  • num_components see createFisherFaceRecognizer().
  • threshold see createFisherFaceRecognizer().
  • eigenvalues The eigenvalues for this Linear Discriminant Analysis (ordered descending).
  • eigenvectors The eigenvectors for this Linear Discriminant Analysis (ordered by their eigenvalue).
  • mean The sample mean calculated from the training data.
  • projections The projections of the training data.
  • labels The labels corresponding to the projections.

createLBPHFaceRecognizer

C++:  Ptr<FaceRecognizer>  createLBPHFaceRecognizer (int  radius=1, int  neighbors=8, int grid_x=8, int  grid_y=8, double  threshold=DBL_MAX )
Parameters:
  • radius – The radius used for building the Circular Local Binary Pattern. The greater the radius, the
  • neighbors – The number of sample points to build a Circular Local Binary Pattern from. An appropriate value is to use `` 8`` sample points. Keep in mind: the more sample points you include, the higher the computational cost.
  • grid_x – The number of cells in the horizontal direction, 8 is a common value used in publications. The more cells, the finer the grid, the higher the dimensionality of the resulting feature vector.
  • grid_y – The number of cells in the vertical direction, 8 is a common value used in publications. The more cells, the finer the grid, the higher the dimensionality of the resulting feature vector.
  • threshold – The threshold applied in the prediction. If the distance to the nearest neighbor is larger than the threshold, this method returns -1.

Notes:

  • The Circular(圆形的) Local Binary Patterns (模式)(used in training and prediction) expect the data given as grayscale(灰度) images, use cvtColor() to convert between the color spaces.
  • This model supports updating.

Model internal data:




——————————————————————

最后欢迎关注我的微信公众号:云端看大地



——————————————————————

最后欢迎关注我的微信公众号:云端看大地

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值