opencv 人脸识别 (二)训练和识别

opencv 人脸识别 (二)训练和识别上一篇中我们对训练数据做了一些预处理,检测出人脸并保存在\pic\color\x文件夹下(x=1,2,3,…类别号),本文做训练和识别。为了识别,首先将人脸训练数据转为灰度、对齐、归一化,再放入分类器(EigenFaceRecognizer),最后用训练出的model进行predict。—————————————–环境:vs2010+op

大家好,又见面了,我是你们的朋友全栈君。

上一篇中我们对训练数据做了一些预处理,检测出人脸并保存在\pic\color\x文件夹下(x=1,2,3,…类别号),本文做训练和识别。为了识别,首先将人脸训练数据 转为灰度、对齐、归一化,再放入分类器(EigenFaceRecognizer),最后用训练出的model进行predict。


—————————————–


环境:vs2010+opencv 2.4.6.0

特征:eigenface

Input:一个人脸数据库,15个人,每人20个样本(左右)。

Output:人脸检测,并识别出每张检测到的人脸。


—————————————–


1. 为训练数据预处理( 转为灰度、对齐、归一化 )

  • 转为灰度和对齐是后面做训练时EigenFaceRecognizer的要求;
  • 归一化是防止光照带来的影响

在上一篇的 2.2 Prehelper.cpp文件中加入函数

void resizeandtogray(char* dir,int k, vector<Mat> &images, vector<int> &labels,
vector<Mat> &testimages, vector<int> &testlabels);

void resizeandtogray(char* dir,int K, vector<Mat> &images, vector<int> &labels,	vector<Mat> &testimages, vector<int> &testlabels){	IplImage* standard = cvLoadImage("D:\\privacy\\picture\\photo\\2.jpg",CV_LOAD_IMAGE_GRAYSCALE);	string cur_dir;	char id[5];	int i,j;	for(int i=1; i<=K; i++)	{		cur_dir = dir;		cur_dir.append("gray\\");			_itoa(i,id,10);		cur_dir.append(id);		const char* dd = cur_dir.c_str();		CStatDir statdir;		if (!statdir.SetInitDir(dd))		{			puts("Dir not exist");			return;		}		cout<<"Processing samples in Class "<<i<<endl;		vector<char*>file_vec = statdir.BeginBrowseFilenames("*.*");		for (j=0;j<file_vec.size();j++)		{			IplImage* cur_img = cvLoadImage(file_vec[j],CV_LOAD_IMAGE_GRAYSCALE);			cvResize(cur_img,standard,CV_INTER_AREA);			Mat cur_mat = cvarrToMat(standard,true),des_mat;			cv::normalize(cur_mat,des_mat,0, 255, NORM_MINMAX, CV_8UC1);			cvSaveImage(file_vec[j],cvCloneImage(&(IplImage) des_mat));			if(j!=file_vec.size())			{					images.push_back(des_mat);					labels.push_back(i);			}			else			{				testimages.push_back(des_mat);				testlabels.push_back(i);			}		}		cout<<file_vec.size()<<" images."<<endl;	}}


并在main中调用:

int main( )
{
	CvCapture* capture = 0;
	Mat frame, frameCopy, image;
	string inputName;	
	int mode;

	char dir[256] = "D:\\Courses\\CV\\Face_recognition\\pic\\"; 
	//preprocess_trainingdata(dir,K); //face_detection and extract to file
	vector<Mat> images,testimages;
	vector<int> labels,testlabels;
	resizeandtogray(dir,K,images,labels,testimages,testlabels); //togray, normalize and resize
	
	system("pause");
	return 0;
}

2. 训练

有了vector<Mat> images,testimages; vector<int> labels,testlabels; 可以开始训练了,我们采用EigenFaceRecognizer建模。

在Prehelper.cpp中加入函数

Ptr<FaceRecognizer> Recognition(vector<Mat> images, vector<int> labels,vector<Mat> testimages, vector<int> testlabels);

Ptr<FaceRecognizer> Recognition(vector<Mat> images, vector<int> labels,	vector<Mat> testimages, vector<int> testlabels){	Ptr<FaceRecognizer> model = createEigenFaceRecognizer(10);//10 Principal components	cout<<"train"<<endl;	model->train(images,labels);	int i,acc=0,predict_l;	for (i=0;i<testimages.size();i++)	{		predict_l = model->predict(testimages[i]);		if(predict_l != testlabels[i])		{			cout<<"An error in recognition: sample "<<i+1<<", predict "<<				predict_l<<", groundtruth "<<testlabels[i]<<endl;			imshow("error 1",testimages[i]);			waitKey();		}		else			acc++;	}	cout<<"Recognition Rate: "<<acc*1.0/testimages.size()<<endl;	return model;}


Recognization()输出分错的样本和正确率,最后返回建模结果Ptr<FaceRecognizer> model


主函数改为:

int main( )
{
	CvCapture* capture = 0;
	Mat frame, frameCopy, image;
	string inputName;	
	int mode;

	char dir[256] = "D:\\Courses\\CV\\Face_recognition\\pic\\"; 
	//preprocess_trainingdata(dir,K); //face_detection and extract to file
	vector<Mat> images,testimages;
	vector<int> labels,testlabels;
	//togray, normalize and resize; load to images,labels,testimages,testlabels
	resizeandtogray(dir,K,images,labels,testimages,testlabels); 
	//recognition
	Ptr<FaceRecognizer> model = Recognition(images,labels,testimages,testlabels);
	char* dirmodel = new char [256];
	strcpy(dirmodel,dir); strcat(dirmodel,"model.out");
	FILE* f = fopen(dirmodel,"w");
	fwrite(model,sizeof(model),1,f);
	system("pause");
	return 0;
}


最终结果:一个错分样本,正确率93.3%

opencv 人脸识别 (二)训练和识别

opencv 人脸识别 (二)训练和识别



文章所用代码打包链接:http://download.csdn.net/detail/abcjennifer/7047853



关于Computer Vision更多的学习资料将继续更新,敬请关注本博客和新浪微博Rachel Zhang。



版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/137542.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • logcat — 基本用法

    logcat — 基本用法1.Log类是一个日志类,我们可以在代码中使用logcat打印出消息常见的日志记录方法有:v(String,String)–verbose显示全部信息d(String,String)–

    2022年7月4日
  • read函数的返回值问题

    read函数的返回值问题Read函数读取字符串返回值的问题1.前言在学习socket编程的途中,通过客户端给服务端发送字符串,然后服务端通过read或者recv来读取数据,然后返回读取的字节数.我在想read返回的读取字节数有没有包含’\0’或者’\n’呢,于是通过一些简单的小例子,来看看实际情况到底如何.2.read函数我们来看一下read函数的原型:ssize_tread(intfd,void…

    2022年6月22日
  • 阿里云申请免费ssl证书及安装_阿里云服务器升级配置

    阿里云申请免费ssl证书及安装_阿里云服务器升级配置阿里云购买免费SSL证书1.操作环境2.购买免费SSL证书3.证书申请,绑定域名《下一篇:Nginx+SSL证书,配置https》1.操作环境阿里云(已实名的企业账号)2.购买免费SSL证书2.1.如图,阿里云官网登陆后,搜索框内输入SSL,点击证书控制台,进入后点击购买证书2.2.如图,选中单域名–DVSSL–免费版,点击立即购买2.3.如图,勾选阅读,点击支付,在支付成功界面,点击证书控制台3.证书申请,绑定域名3.1.如图,在证书控制台,点击证书申请

    2022年10月3日
  • MySQL常见面试题_web面试题

    MySQL常见面试题_web面试题一、存储引擎MySQL常见的两种存储引擎:MyISAM与InnoDB二、字符集及校对规则字符集指的是一种从二进制编码到某类字符符号的映射。校对规则则是指某种字符集下的排序规则。Mysql中每一种字符集都会对应一系列的校对规则。Mysql采用的是类似继承的方式指定字符集的默认值,每个数据库以及每张数据表都有自己的默认值,他们逐层继承。比如:某个库中所有表的默认字符集将是该数据库所指定…

    2022年8月26日
  • Pycharm 提示:this license * has been cancelled – Python零基础入门教程

    Pycharm 提示:this license * has been cancelled – Python零基础入门教程目录一.前言一.找到hosts文件二.修改hosts文件三.检查hosts文件是否修改成功四.pycharm安装激活详细教程五.猜你喜欢一.前言零基础Python学习路线推荐:Python学习目录>>Python基础入门Pycharm安装激活过程中,提示thislicense****hasbeencancelled。这个问题并不是你的激活码不对,而是需要修改系统的hosts文件,下面详细讲解下如何修改hosts文件

    2022年8月26日
  • resttemplate post请求发送body_java发送post请求

    resttemplate post请求发送body_java发送post请求当使用RestTemplate进行http请求时,的确很方便,但是当需要进行post请求时遇到了坑1POST传递参数:采用LinkedMultiValueMap,不能使用HashMapStringurl=’http://posturl’;MultiValueMap<String,String>map=newLinkedMultiValueMap&lt……

    2022年9月3日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号