opencv学习-建立人脸识别分类器
2014-04-18 12:53
369 查看
在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,先贴上源代码
这段代码做的事情是这样的,读取400张图片,将399张用于训练,再用最后一张去做识别
代码很短,也很容易看懂
下面是我的输出
Predicted class = 37 / Actual class = 37.
输出表示识别正确
注释中说了这是用PCA实现的,下次得好好看看论文仔细研究算法了!
有了这个,你也可以快速建立你的人脸识别分类器了!
首先我们要去
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; }
这段代码做的事情是这样的,读取400张图片,将399张用于训练,再用最后一张去做识别
代码很短,也很容易看懂
下面是我的输出
Predicted class = 37 / Actual class = 37.
输出表示识别正确
注释中说了这是用PCA实现的,下次得好好看看论文仔细研究算法了!
有了这个,你也可以快速建立你的人脸识别分类器了!
相关文章推荐
- Muduo 多线程模型:一个 Sudoku 服务器演变
- 接活网站
- TOP 10 PROGRAMMING FONTS
- Ganymed SSH-2 for Java系列3之执行远程shell 命令
- 15_Linux基础知识及命令――――修改用户、组的属性和查看用户信息
- Hadoop学习资料、博客及网站汇总
- linux 中断与时钟
- mysql超时设置的问题,如果连接闲置8小时 (8小时内没有进行数据库操作), mysql就会自动断开连接, 要重启tomcat
- CentOS常用命令
- Linux下安装LAMP的步骤
- linux下yum工具的安装使用
- linux下定时执行mysql备份
- nginx配置ThinkPHP Rewrite
- litespeed+nginx+mysql+php+pureftpd一键安装包
- 转:sock_ev——linux平台socket事件框架(event loop) .
- 优秀Android开发博客和网站大全
- linux centos yum 安装 mysql
- centOS下制作RPM包
- Hadoop平台配置总结
- linux qt环境变量设置