用ORL人脸数据库和opencv的facererc_demo.cpp做人脸检测
来源:互联网 发布:20142015cba总决赛数据 编辑:程序博客网 时间:2024/06/06 06:01
用OpenCV的\opencv\soruce\samples\cpp\facerrc_demo.cpp文件可以做人脸特征检测的训练和测试。要使用这个程序主要做的工作就是提供一个图像集,ORL,然后再生成图像位置和标签的文件,用于faceerc_demo.cpp的训练和测试,生成的“face_at.txt”文件如下
//util.h#ifndef __UTIL_H__#define __UTIL_H__//获取目录先的文件void getFiles(std::string path, std::vector<std::string>& files);//分离字符串std::string splitString(const std::string& source, const std::string& toc1, const std::string& toc2);//将图像文件的位置和标签信息写到保存文件里void writeGraphInfo(const std::string& fileName, std::vector<std::string>& files, const std::string& toc1, const std::string& toc2);#endif
//util.cpp#include <Windows.h>#include <iostream>#include <cstdlib>#include <io.h>#include <stdlib.h>#include <stdio.h>#include <vector>using namespace std;void getFiles(string path, vector<string> & files){ //文件句柄 long hFile = 0; //文件信息 struct _finddata_t fileinfo; string p; if((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) != -1) { do { //如果是目录,迭代之 //如果不是,加入列表 if((fileinfo.attrib & _A_SUBDIR)) { if(strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0) getFiles( p.assign(path).append("\\").append(fileinfo.name), files ); } else { files.push_back(p.assign(path).append("\\").append(fileinfo.name)); } }while(_findnext(hFile, &fileinfo) == 0); _findclose(hFile); }}string splitString(const string& s, const string& toc1, const string& toc2){ string::size_type pos1, pos2; string result; pos1 = s.find(toc1); pos2 = s.find(toc2); result = s.substr(++pos1, pos2-pos1); return result;}void writeGraphInfo(const string& fileName, vector<string>& files, const string& toc1, const string& toc2){ FILE* pFile; string label; pFile = fopen(fileName.c_str(), "w"); vector<string>::iterator iter = files.begin(); while(iter!=files.end()) { label = splitString(*iter, toc1, toc2); fprintf(pFile, "%s;%s\n", (*iter).c_str(), label.c_str()); iter++; } fclose(pFile);}
//prep.h#ifndef __PREP_H__#define __PREP_H__#include "opencv2/core/core.hpp"#include "opencv2/highgui/highgui.hpp"#include "opencv2/contrib/contrib.hpp"#include <iostream>#include <fstream>#include <sstream>#include <stdlib.h>#endif
#include "prep.h"#include "util.h"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[]) { string grpPath; //人脸图像路径 string fn_csv; //保存人脸和相应标签的文件 vector<string> files; vector<Mat> images; vector<int> labels; grpPath = "ORL"; fn_csv = "face_at.txt"; getFiles(grpPath, files); writeGraphInfo(fn_csv, files, "s", "_"); // 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); system("pause"); return 0;}
0 0
- 用ORL人脸数据库和opencv的facererc_demo.cpp做人脸检测
- ORL人脸数据库
- OpenCV实例:对实时视频做人脸检测
- 利用Opencv自带源码,调试摄像头做人脸检测
- 用OpenCV做人脸识别和性别识别contrib
- OpenCV:人脸检测和行人检测
- OpenCV学习笔记(五十五)——用OpenCV做人脸识别和性别识别contrib
- OpenCV的人脸检测
- 人脸识别之遍历ORL人脸文件
- ORL Face Database 人脸识别数据集
- Python和OpenCV简单的人脸检测程序
- 基于Jetsontk1和OpenCV下的人脸检测
- cvSmooth函数 和 OpenCV自带的人脸检测
- 一个可以用来做人脸检测的视频帧间差分代码
- opencv 人脸识别和行人检测
- opencv人脸检测和跟踪
- 用OpenCV做人脸识别和性别识别contrib(本文包含最全人脸库)
- opencv 检测人脸
- uva 657
- 基于Python两种跟踪算法
- Spring 核心DI, AOP简介
- NDK开发 从入门到放弃(三:类型对应、字符串的操作、类型签名)
- android JNI 多线程 C函数回调
- 用ORL人脸数据库和opencv的facererc_demo.cpp做人脸检测
- 第一章 数据结构和算法 (1)
- EJB到底是什么,真的那么神秘吗??简简单单解释清楚。就这么直白,终于明白了
- srping 基于maven的打包配置(jar)
- C++引用和指针简单样例对比
- linq下 多条件 on后面多条件 左连接
- bzoj 3172 [Tjoi2013]单词 后缀自动机
- 基于Retrofit+Okio+RxBus实现文件下载(带下载进度)
- YUV主要采样格式理解