赞
踩
近几天在群里面发现很多人在用3.3.1的人脸识别在做小项目,很多人一心只想快点得到效果,四处搜索博客,问人,忙活了几天也无功而返(3.3的资料相对较少),其实OpenCV官方doc以及sample里面都有很详细的说明和例程,只要稍微耐心看一下,就可以解决了。
前几天帮人调了一下OpenCV3.3.1的人脸识别,在此做一下记录:
然后我又无聊的在某宝搜了下与OpenCV相关的书籍,结果发现近期有很多新书出版,看了下目录又是大同小异,基本上又是对opencv doc的翻译和sample的抄袭,稍微整理一下,就出书了。。在这里我建议大家,多看官方同步的doc,以及module下的例程。
/************************************
OpenCV3.3.1 + contrib
faceRecognize
训练+识别
***********************************/
#include <opencv2/opencv.hpp>
#include "opencv2/face.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
using namespace cv;
using namespace cv::face;
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(Error::StsBadArg, error_message);
}
string line, path, classlabel;
while (getline(file, line))
{
stringstream liness(line);
getline(liness, path, separator);
cout << path << endl;
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[])
{
string fn_csv = "at.txt"; // 和cpp同目录,或者加完整路径
string fn_haar = "haarcascade_frontalface_alt2.xml";//haarcasade目录下有很多训练好的文件
vector<Mat> images;
vector<int> labels;
read_csv(fn_csv, images, labels);
if (images.size() <= 1)
{
cout << "needs at least 2 images to work." << endl;
return 0;
}
int im_width = images[0].cols;
int im_height = images[0].rows;
//开始训练
cout << "training..." << endl;
//对就是这里改了,可以通过设置调整参数,来提高精度
Ptr<EigenFaceRecognizer> model0 = EigenFaceRecognizer::create();
model0->train(images, labels);
//保存模型到 eigenfaces_at.yaml
model0->save("eigenfaces_at.yml");
//创建一个脸部特征识别器,读取训练模型
Ptr<EigenFaceRecognizer> model1 = Algorithm::load<EigenFaceRecognizer>("eigenfaces_at.yml");
//通过哈尔级联分类器粗定位出人脸
CascadeClassifier haar_cascade;
haar_cascade.load(fn_haar);
VideoCapture cap(0); //
Mat frame;
for (;;)
{
cap >> frame;
Mat original = frame.clone();
Mat gray;
cvtColor(original, gray, COLOR_BGR2GRAY);
vector< Rect_<int> > faces;
haar_cascade.detectMultiScale(gray, faces);
for (size_t i = 0; i < faces.size(); i++)
{
Rect face_i = faces[i];
Mat face = gray(face_i);
Mat face_resized;
cv::resize(face, face_resized, Size(im_width, im_height), 1.0, 1.0, INTER_CUBIC);
//预测识别出该人脸的标签
int prediction = model1->predict(face_resized);
rectangle(original, face_i, Scalar(0, 255, 0), 1);
string box_text = "Xiong";
int pos_x = std::max(face_i.tl().x - 10, 0);
int pos_y = std::max(face_i.tl().y - 10, 0);
//我的人脸在数据集里标签为1,所以如果预测也为1,则就是我
if(prediction==1)
putText(original, box_text, Point(pos_x, pos_y), FONT_HERSHEY_PLAIN, 1.0, Scalar(0, 255, 0), 2);
}
imshow("face_recognizer", original);
char key = (char)waitKey(20);
if (key == 27)
break;
}
waitKey(0);
return 0;
}
C:/Users/12478/Desktop/faces/f1/1.pgm;1
C:/Users/12478/Desktop/faces/f1/2.pgm;1
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。