opencv学习-建立人脸识别分类器
来源:互联网 发布:决战萨斯城 知乎 编辑:程序博客网 时间:2024/05/21 22:42
在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;
- }
这段代码做的事情是这样的,读取400张图片,将399张用于训练,再用最后一张去做识别
代码很短,也很容易看懂
下面是我的输出
Predicted class = 37 / Actual class = 37.
输出表示识别正确
注释中说了这是用PCA实现的,下次得好好看看论文仔细研究算法了!
有了这个,你也可以快速建立你的人脸识别分类器了!
0 0
- opencv学习-建立人脸识别分类器
- opencv学习-建立人脸识别分类器
- OpenCV学习记录(一):使用haar分类器进行人脸识别
- opencv 人脸识别 眼睛识别 鼻子嘴巴等等的分类器haarcascade
- OpenCV学习记录(二):自己训练haar特征的adaboost分类器进行人脸识别
- 目标检测与分类学习之路----opencv人脸检测识别(zero)
- 人脸识别系统,分类学习
- (人脸识别2-2)——利用opencv内置分类器检测人脸
- opencv自带的haar分类器进行人脸识别
- Opencv学习(人脸识别)
- opencv学习-pca人脸识别
- OpenCV学习:人脸识别 简单程序
- 学习opencv——人脸识别
- Dlib+OpenCV深度学习人脸识别
- 学习OpenCV——行人识别&人脸识别
- OpenCV学习笔记:人脸检测(级联分类器)
- OpenCV机器学习:SVM分类器实现MNIST手写数字识别
- 利用opencv中的级联分类器进行人脸检测-opencv学习(1)
- 英超争冠 曼城掉队
- android控件实现抖动的效果
- Linux学习:第一章-Linux简介
- Java 泛型
- 【转】我与selerant之 劳动仲裁篇
- opencv学习-建立人脸识别分类器
- 将txt文件转化为mat文件
- 求职规划
- https通信
- 科技股阴云密布 阿里京东IPO或受影响
- HTTP Session原理
- 【转】我与selerant之 被搞篇
- 判断两线段是否相交(快速排斥和跨立)
- 排序算法大全之计数排序