训练数据采集分为正样本和负样本
正样本:正脸、侧脸、抬头、低头、表情,4000张
负样本:平面照片、弯曲照片、褶皱照片、照片抠眼鼻嘴做简单面具、电子屏,4000张
分辨率为128*128,因为1米以内的人脸框大小在100多左右
保存格式为.xml
FileStorage fs(savename, FileStorage::WRITE);//savename表示保存的文件名
fs << "depth" << image2;//image2表示128*128的16位深度人脸框信息
cout << savename << " ,save successful" << endl;
fs.release();
然后开始训练模型
LBP.h
#ifndef __LBP_H__
#define __LBP_H__
#include "opencv2/opencv.hpp"
#include<vector>
using namespace std;
using namespace cv;
class LBP
{
public:
void elbp(const Mat &src, Mat &dst, int radius, int neighbors);
};
#endif
//SVMTest.h
#ifndef __SVMTEST__
#define __SVMTEST__
#include "opencv2/ml.hpp"
//#include"../Utility/CommonUtility.h"
//#include"../Utility/LogInterface.h"
#include<fstream>
#include"LBP.h"
using namespace cv::ml;
// if you do not need log,comment it,just like :#define LOG_WARN_SVM_TEST(...) //LOG4CPLUS_MACRO_FMT_BODY ("SVMTest", WARN_LOG_LEVEL, __VA_ARGS__)
#define LOG_DEBUG_SVM_TEST(...) //LOG4CPLUS_MACRO_FMT_BODY ("SVMTest", DEBUG_LOG_LEVEL, __VA_ARGS__)
#define LOG_ERROR_SVM_TEST(...) //LOG4CPLUS_MACRO_FMT_BODY ("SVMTest", ERROR_LOG_LEVEL, __VA_ARGS__)
#define LOG_INFO_SVM_TEST(...) //LOG4CPLUS_MACRO_FMT_BODY ("SVMTest", INFO_LOG_LEVEL, __VA_ARGS__)
#define LOG_WARN_SVM_TEST(...) //LOG4CPLUS_MACRO_FMT_BODY ("SVMTest", WARN_LOG_LEVEL, __VA_ARGS__)
//#define CONFIG_FILE "./Resource/Configuration.xml"
#define CELL_SIZE 16
class SVMTest
{
public:
SVMTest(const string &_trainDataFileList,
const string &_testDataFileList,
const string &_svmModelFilePath,
const string &_predictResultFilePath,
SVM::Types svmType, // See SVM::Types. Default value is SVM::C_SVC.
SVM::KernelTypes kernel,
double c, // For SVM::C_SVC, SVM::EPS_SVR or SVM::NU_SVR. Default value is 0.
double coef, // For SVM::POLY or SVM::SIGMOID. Default value is 0.
double degree, // For SVM::POLY. Default value is 0.
double gamma, // For SVM::POLY, SVM::RBF, SVM::SIGMOID or SVM::CHI2. Default value is 1.
double nu, // For SVM::NU_SVC, SVM::ONE_CLASS or SVM::NU_SVR. Default value is 0.
double p // For SVM::EPS_SVR. Default value is 0.
);
bool Initialize();
virtual ~SVMTest();
void Train();
void Predict();
private:
string trainDataFileList;
string testDataFileList;
string svmModelFilePath;
string predictResultFilePath;
string predictResultError;
// SVM
Ptr<SVM> svm;
// feature extracting(HOG,LBP,Haar,etc)
LBP lbp;
};
#endif // SVMTEST
SVM训练模型主要参考了http://blog.youkuaiyun.com/qianqing13579,自己改了些东西,先表示感谢!
//LBP.cpp
#include"LBP.h"
void elbp(const Mat &src, Mat &dst, int radius, int neighbors)
{
CV_As