您的位置:首页 > 运维架构

opencv3.0使用Eigen方法进行人脸识别的方法

2016-05-08 01:34 561 查看
直接上代码

#include <opencv2\core.hpp>
#include <opencv2\face.hpp>
#include <opencv2\highgui.hpp>

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <algorithm>
#include <vector>
#include <list>
#include <set>
#include <iostream>

using namespace cv;
using namespace cv::face;
using namespace std;

void read_csv(const char *filename, vector<Mat>& images, vector<int>labels, char separator = ';')
{
FILE *fp = fopen(filename, "r");
if (fp == NULL)
{
cout << "File not valid.\n";
return;
}
char path[128];
int lab;

while (memset(path,0,sizeof(path)),fscanf(fp, "%s;%d", path, &lab) != EOF)
{
images.push_back(imread(path));
labels.push_back(lab);
}

return;
}
int main()
{
vector<Mat>images;
vector<int>labels;
char *filename = "C:\\faces\\csv.txt";
read_csv(filename, images, labels);
if (images.size() <= 1)
{
cout << "Need More pics\n";
return 0;
}
int height = images[0].rows;
Mat testSample = images[images.size() - 1];
int testLable = labels[labels.size() - 1];
images.pop_back();
labels.pop_back();
Ptr<FaceRecognizer> model = createEigenFaceRecognizer();
model->train(images, labels);
int prediclabel;
double confidence;
model->predict(testSample, prediclabel, confidence);
imshow("test",testSample);

cout << testLable << "\t" << confidence << endl;
waitKey(0);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: