face tracking (eigen face) opencv

232 views Asked by At

I want to do eigen face by opencv ,and this is my code.

#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 void read_csv(const string& filename, vector<Mat>& images,vector<int>& labels, char separator = ';') {
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 fn_haar = string("C:/opencv/sources/data/haarcascades/haarcascade_frontalface_alt_tree.xml");
string fn_csv = string("C:/Users/faho0odywbas/Desktop/csv.ext");

int deviceId = atoi("0");
vector<Mat> images;
vector<int> labels;
try {
    read_csv(fn_csv, images, labels);
} catch (cv::Exception& e) {
    cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl;
    exit(1);
}

int im_width = images[0].cols;
int im_height = images[0].rows;
Ptr<FaceRecognizer> model = createEigenFaceRecognizer(23,2500.0);


CascadeClassifier haar_cascade;
haar_cascade.load(fn_haar);
VideoCapture cap(deviceId);
namedWindow("face_recognizer", CV_WINDOW_AUTOSIZE);


if(!cap.isOpened()) {
    cerr << "Capture Device ID " << deviceId << "cannot be opened." << endl;
    return -1;
}
vector< Rect_<int> > faces;
Mat frame;
waitKey(1500);
for(;;) {
    cap >> frame;
    Mat original = frame.clone();
    Mat gray;
    cvtColor(original, gray, CV_BGR2GRAY);
    haar_cascade.detectMultiScale(gray, faces);

    for(int i = 0; i < faces.size(); i++) {
        Rect face_i = faces[i];
        Mat face = gray(face_i);
        Mat face_resized;
        cv::resize(face, face_resized, Size(im_width, im_height), 1.0, 1.0, INTER_CUBIC);
        int prediction = model->predict(face_resized);
        rectangle(original, face_i, CV_RGB(255, 0,0), 1);
        string box_text = format("Sujeto = %d", prediction);
        int pos_x = std::max(face_i.tl().x - 10, 0);
        int pos_y = std::max(face_i.tl().y - 10, 0);
        putText(original, box_text, Point(pos_x, pos_y), FONT_HERSHEY_PLAIN, 1.0, CV_RGB(0,255,0), 2.0);
    }
    imshow("face_recognizer", original);
    char key = (char) waitKey(1);
    if(key == 27){
        destroyAllWindows();
        break;
    }



}
return 0;
   }

I'm sure it's right because it was work with my friend's computer, so I think the problem with my visual studio or the windows.

The error that comes to me is

" First-chance exception at 0x74C44598 in Project3.exe: Microsoft C++ exception: cv::Exception at memory location 0x00D7ED2C.

If there is a handler for this exception, the program may be safely continued. "

and when I click break, it shows also to me this error

wkernelbase.pdb not loaded

so, what I have to do ?

I have done this Tools->Options->Debugging->Symbols->Select “Microsoft Symbol Servers”.

but also same error

I have another question which is if I want hit "s" to start recording face for every frame, and hit "d" to end the recording, how can I do that ?

Thanks a lot

0

There are 0 answers