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

用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;
}

这里写图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ORL是一个广泛使用的人脸数据库,由美国乔治亚理工学院(Georgia Tech)的计算机视觉研究实验室创建和维护。这个数据库包含了40个不同人的400张灰度人脸图片,每个人有10张不同角度和表情的照片。 想要下载ORL人脸数据库,可以通过以下步骤进行操作: 1. 在互联网上搜索ORL人脸数据库,可以在官方网站或其他可信的资源网站上找到下载链接。 2. 找到适合你的需求的文件格式,ORL数据库通常提供了多种格式的数据,如图片文件、Matlab格式、CSV格式等,根据需要选择相应的格式。 3. 点击下载链接,将文件保存到你的计算机中的合适位置。由于数据库文件的大小可能较大,可能需要一段时间来完成下载过程,取决于你的网络速度。 4. 下载完成后,解压缩文件。如果下载的是压缩文件,你需要使用解压缩软件(如WinRAR、7-Zip等)将文件解压缩到你想要保存的文件夹中。 5. 打开解压缩后的文件夹,你将看到包含400张人脸图片的文件。一般来说,这些图片文件以数字来命名,可以通过相应的标签或文件名来识别每个人的照片。 通过上述步骤,你将成功下载ORL人脸数据库,并可以在你的计算机上使用这些数据进行人脸识别、表情识别等相关的研究和应用。请注意,对于这个数据库的使用需要遵守其相应的许可协议,尊重数据的来源和知识产权。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值