用ORL人脸数据库和opencv的facererc_demo.cpp做人脸检测

用OpenCV的\opencv\soruce\samples\cpp\facerrc_demo.cpp文件可以做人脸特征检测的训练和测试。要使用这个程序主要做的工作就是提供一个图像集,ORL,然后再生成图像位置和标签的文件,用于faceerc_demo.cpp的训练和测试,生成的“face_at.txt”文件如下

用ORL人脸数据库和opencv的facererc_demo.cpp做人脸检测_第1张图片

//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 
#include 
#include 
#include 
#include 
#include 
#include 

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 
#include 
#include 
#include 

#endif
#include "prep.h"
#include "util.h"

using namespace cv;
using namespace std;

static void read_csv(const string& filename, vector& 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 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 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;
}

用ORL人脸数据库和opencv的facererc_demo.cpp做人脸检测_第2张图片

你可能感兴趣的:(opencv)