FaceRecognizer類
OpenCV中所有人臉識(shí)別的模型都是繼承自FaceRecognizer這個(gè)基類,這個(gè)類提供了人臉識(shí)別算法的統(tǒng)一接口刑然。
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;
};
Algorithm類
Algorithm所有的派生類都提供了一下功能:
- 虛擬構(gòu)造器作烟。每個(gè)繼承Algorithm的算法在程序開(kāi)始時(shí)注冊(cè)愉粤,你可以得到已注冊(cè)算法的列表,并且根據(jù)指定算法名字來(lái)創(chuàng)建一個(gè)實(shí)例拿撩。如果你想添加自己的算法衣厘,比較好的方法是在你算法名上添加一個(gè)前綴,以示區(qū)別压恒。(詳見(jiàn)Algorithm::create())
- 通過(guò)參數(shù)名來(lái)設(shè)置或檢索參數(shù)影暴。Algorithm可以通過(guò)你指定參數(shù)名字符串來(lái)提供這樣的功能。(詳見(jiàn)Algorithm::set()探赫、Algorithm::get())
- 從XML或YAML文件中讀寫參數(shù)型宙。每個(gè)算法都可以將參數(shù)寫到文件中,并且在需要時(shí)讀出參數(shù)伦吠。
FaceRecognizer支持的接口
- FaceRecognizer::train()用于將人臉圖像數(shù)據(jù)進(jìn)行訓(xùn)練
- 根據(jù)一張輸入的人臉圖片進(jìn)行預(yù)測(cè)
- 從指定XML或YAML文件中上載或存儲(chǔ)模型參數(shù)
設(shè)置閾值
人臉識(shí)別的一個(gè)常用情景是待識(shí)別人臉是不是輸入訓(xùn)練數(shù)據(jù)集妆兑。這個(gè)閾值的設(shè)置是在具體的FaceRecognizer的構(gòu)造器中魂拦,并通過(guò)從Algorithm繼承的set和get方法來(lái)使用閾值。
- 創(chuàng)建模型時(shí)設(shè)置閾值
// 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);
- 運(yùn)行程序時(shí)獲取或設(shè)置閾值
// 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);
- 將閾值設(shè)置為0.0搁嗓,如果預(yù)測(cè)類別號(hào)為-1芯勘,則說(shuō)明人臉時(shí)未知的,沒(méi)有在數(shù)據(jù)庫(kù)中記錄
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函數(shù)接口
FaceRecognizer::train
函數(shù)定義:
void FaceRecognizer::train(InputArrayOfArrays src, InputArray labels) = 0
函數(shù)參數(shù)為訓(xùn)練的圖片腺逛,類型為vector<Mat>;圖片所對(duì)應(yīng)的標(biāo)簽荷愕,類型為vector<int>。
FaceRecognizer::update
函數(shù)定義:
void FaceRecognizer::update(InputArrayOfArrays src, InputArray labels)
該方法為更新一個(gè)模型(條件是給模型可以被更新棍矛,在OpenCV中提供的人臉識(shí)別算法中安疗,只有LBP直方圖的方法才可以被更新),更新和訓(xùn)練接口的區(qū)別是够委,訓(xùn)練是清空已有的模型茂契,學(xué)習(xí)一個(gè)新的模型,而更新不會(huì)刪除之前的模型數(shù)據(jù)慨绳。
FaceRecognizer::predict
函數(shù)定義:
int FaceRecognizer::predict(InputArray src) const = 0
void FaceRecognizer::predict(InputArray src, int& label, double& confidence) const = 0
第三個(gè)參數(shù)是對(duì)于預(yù)測(cè)類別的相關(guān)置信度掉冶。
const后綴表示predict函數(shù)不會(huì)影響模型內(nèi)部狀態(tài),所以這個(gè)方法可以安全被不同線程調(diào)用脐雪。
下面的小例子是根據(jù)訓(xùn)練模型來(lái)進(jìn)行預(yù)測(cè)厌小。
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
函數(shù)定義:
void FaceRecognizer::save(const string& filename) const
void FaceRecognizer::save(FileStorage& fs) const
第一個(gè)定義是根據(jù)XML或者YAML文件名來(lái)存儲(chǔ)模型,第二個(gè)定義是將模型存成FileStorage實(shí)例战秋。
FaceRecognizer::load
函數(shù)定義:
void FaceRecognizer::load(const string& filename)
void FaceRecognizer::load(const FileStorage& fs) = 0
這里載入模型參數(shù)和save函數(shù)的參數(shù)意義一樣璧亚。
建立模型接口
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);
這三個(gè)接口分別對(duì)應(yīng)OpenCV的三個(gè)人臉識(shí)別算法,均返回一個(gè)智能指針Ptr類的實(shí)例脂信。
小結(jié)
這一小節(jié)癣蟋,介紹了OpenCV中人臉識(shí)別功能對(duì)應(yīng)的類FaceRecognizer的大體框架,介紹了相關(guān)接口和使用方法狰闪。在后面的博客疯搅,將具體以LBPH算法為例,介紹其原理和匹配源碼埋泵。
參考資料
轉(zhuǎn)載請(qǐng)注明作者Jason Ding及其出處
Github主頁(yè)(http://jasonding1354.github.io/)
CSDN博客(http://blog.csdn.net/jasonding1354)
簡(jiǎn)書(shū)主頁(yè)(http://www.reibang.com/users/2bd9b48f6ea8/latest_articles)