在sample/cpp下面有一个文件叫做facerec_demo.cpp,还有一个文件叫做facerec_at_t.txt
首先我们要去 AT&T人脸库下载400张图片,分别时40个人,每个人有10张照片
下载完成后,随便放在哪里,但是注意要修改facerec_at_t.txt文件里面的路径,下面是我的文件路径
/home/myname/Desktop/opencv-2.4.7/orl_faces/s13/2.pgm;12
/home/myname/Desktop/opencv-2.4.7/orl_faces/s13/7.pgm;12
/home/myname/Desktop/opencv-2.4.7/orl_faces/s13/6.pgm;12
;后面的12代表类别,如果你要创建自己和同学的人脸库,记住要给不同的人赋予不同的label
然后我们看看facerec_demo.cpp,先贴上源代码
#include "opencv2/core/core.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/contrib/contrib.hpp" #include <iostream> #include <fstream> #include <sstream> using namespace cv; using namespace std; static void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, char separator = ';') { std::ifstream file(filename.c_str(), ifstream::in); if (!file) { string error_message = "No valid input file was given, please check the given filename."; CV_Error(CV_StsBadArg, error_message); } string line, path, classlabel; while (getline(file, line)) { stringstream liness(line); getline(liness, path, separator); getline(liness, classlabel); if(!path.empty() && !classlabel.empty()) { images.push_back(imread(path, 0)); labels.push_back(atoi(classlabel.c_str())); } } } int main(int argc, const char *argv[]) { // Check for valid command line arguments, print usage // if no arguments were given. if (argc != 2) { cout << "usage: " << argv[0] << " <csv.ext>" << endl; exit(1); } // Get the path to your CSV. string fn_csv = string(argv[1]); // These vectors hold the images and corresponding labels. vector<Mat> images; vector<int> labels; // Read in the data. This can fail if no valid // input filename is given. try { read_csv(fn_csv, images, labels); } catch (cv::Exception& e) { cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl; // nothing more we can do exit(1); } // Quit if there are not enough images for this demo. if(images.size() <= 1) { string error_message = "This demo needs at least 2 images to work. Please add more images to your data set!"; CV_Error(CV_StsError, error_message); } // Get the height from the first image. We'll need this // later in code to reshape the images to their original // size: int height = images[0].rows; //作为我的唯一test图片 Mat testSample = images[images.size() - 1]; int testLabel = labels[labels.size() - 1]; images.pop_back(); labels.pop_back(); Ptr<FaceRecognizer> model = createEigenFaceRecognizer(); model->train(images, labels); // The following line predicts the label of a given // test image: int predictedLabel = model->predict(testSample); // // To get the confidence of a prediction call the model with: // // int predictedLabel = -1; // double confidence = 0.0; // model->predict(testSample, predictedLabel, confidence); // string result_message = format("Predicted class = %d / Actual class = %d.", predictedLabel, testLabel); cout << result_message << endl; waitKey(0); return 0; }
代码很短,也很容易看懂
下面是我的输出
Predicted class = 37 / Actual class = 37.
输出表示识别正确
注释中说了这是用PCA实现的,下次得好好看看论文仔细研究算法了!
有了这个,你也可以快速建立你的人脸识别分类器了!