OpenCV人脸识别

时间:2015-04-08 21:02:56

标签: c++ opencv face-recognition

我正在尝试使用opencv通过将静态图像与图像数据库进行比较并将预测打印到txt文件来对静态图像执行面部识别,这是我已经放在一起的代码,但是我收到了很多错误。任何有关我出错的帮助将不胜感激。

  #include "opencv2/core/core.hpp"
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"

#include <iostream>
#include <fstream>
#include <sstream>

using namespace cv;
using namespace std;


static Mat norm_0_255(InputArray _src) {
    Mat src = _src.getMat();
    // Create and return normalized image:
    Mat dst;
    switch(src.channels()) {
        case 1:
            cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC1);
            break;
        case 3:
            cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC3);
            break;
        default:
            src.copyTo(dst);
            break;
    }
    return dst;
}

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()));
        }
    }
}
//path to csv
string fn_csv = ("./at.csv");
//patch to haar cascade
string fn_haar = ("./haarcascade_frontalface_alt.xml");
// These vectors hold the images and corresponding labels.
vector<Mat> images;
vector<int> labels;
read_csv(fn_csv, images, labels);





    int im_width = images[0].cols;
    int im_height = images[0].rows;

Mat testSample = images[images.size() - 1];
int testLabel = labels[labels.size() - 1];

//cv::createEigenFaceRecognizer(10);

    //create the model

    Ptr<FaceRecognizer> model = createEigenFaceRecognizer();
    model->train(images, labels);

//load cascade

    CascadeClassifier haar_cascade;
    haar_cascade.load(fn_haar);

    Mat Image
    for(;;) {
   Mat Image* img = cvLoadImage ("./Lena.png")

        Mat gray;
        cvtColor(original, gray, CV_BGR2GRAY);
        // Find the face
        vector< Rect_<int> > faces;
        haar_cascade.detectMultiScale(gray, faces);

        vector< Rect_<int> > faces;
        haar_cascade.detectMultiScale(gray, faces);

        for(int i = 0; i < faces.size(); i++) {
            // Process face
            Rect face_i = faces[i];
            // Crop the face from the image.
            Mat face = gray(face_i);

            Mat face_resized;
            cv::resize(face, face_resized, Size(im_width, im_height), 1.0, 1.0, INTER_CUBIC);
            // prediction
            int prediction = model->predict(face_resized);

string text = format("Prediction = %d", prediction);
        }

    }

    int send () {
        std::string s= text;
        std::ofstream os("prediction.txt");
        if (!os) { std::cerr<<"Error writing to prediction"<<std::endl; } else {
            os << s;  
        }

    return 0;
    }

1 个答案:

答案 0 :(得分:1)

您的代码是否完整?通过阅读错误信息,可以轻松纠正大量粗心错误。

  • 没有 main()功能
  • 缺少分号行83和85
  • 同一行85:

    Mat Image* img = cvLoadImage ("./Lena.png");
    

    应该是

    Image = imread("./Lena.png");
    

    cvLoadImage()是C(返回IplImage*),而不是C ++。 (虽然我不明白为什么你在无限循环中一遍又一遍地加载这个相同的图像。)

  • original未定义第88行

  • 第90-94行是同一件事的两倍

  • send()函数中的未定义text变量行113,可能是您应该作为参数传递的第107行。