facerec_demo model->predict exception thrown

asked 2013-02-20 11:41:07 -0500

Shoule101 gravatar image

updated 2013-02-22 10:08:47 -0500

berak gravatar image

Hello, I am trying to use the facerec_demo.cpp file to start off with face recognition using a video feed. This demo does extactly that.

So I can get it to compile properly inputting all the command arguments and it runs except when it reaches the predict function of the code.

int prediction = model->predict(face_resized);

I know its throwing the exception here because I inset a breakpoint at this location the exception happens when this line executes.

Does any one have any ideas as to why this may be happening?

UPDATE: 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 = ';') {
    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[]) {
    // Check for valid command line arguments, print usage
    // if no arguments were given.
    if (argc != 2) {
        cout << "usage: " << argv[0] << " </path/to/haar_cascade> </path/to/csv.ext> </path/to/device id>" << endl;
     //   cout << "\t </path/to/haar_cascade> -- Path to the Haar Cascade for face detection." << endl;
        cout << "\t </path/to/csv.ext> -- Path to the CSV file with the face database." << endl;
      //  cout << "\t <device id> -- The webcam device id to grab frames from." << endl;
        system("pause");
        exit(1);
    }
    // Get the path to your CSV:
    //string fn_haar = string(argv[1]);

    string fn_haar = "C:\\opencv\\data\\haarcascades\\haarscascade_frontalface_default.xml";

    string fn_csv = string(argv[1]);
    //int deviceId = atoi(argv[3]);

    // These vectors hold the images and corresponding labels:
    vector<Mat> images;
    vector<int> labels;
    // Read in the data (fails if no valid input filename is given, but you'll get an error message):
    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);
    }
    // Get the height from the first image. We'll need this
    // later in code to reshape the images to their original
    // size AND we need to reshape incoming faces to this size:
    int im_width = images[0].cols;
    int im_height = images[0].rows;
    // Create a FaceRecognizer and train it on the given images:
    Ptr<FaceRecognizer> model = createFisherFaceRecognizer();
    model->train(images, labels);
    // That's it for learning the Face Recognition model. You now
    // need to create the classifier for the task of Face Detection.
    // We are going to use the haar cascade you have specified in the
    // command line arguments:
    //
    CascadeClassifier haar_cascade;
    haar_cascade.load(fn_haar);
    // Get a handle to the Video device:
    VideoCapture cap("C:\\ImageFolder\\Video2.mpg");
    // Check if we can use this device at all ...
(more)
edit retag flag offensive close merge delete

Comments

since you modified the demo, show some more code, please

berak gravatar imageberak ( 2013-02-20 11:47:13 -0500 )edit

oh, and it does crash on invalid input, e.g. face_resized being empty

berak gravatar imageberak ( 2013-02-20 11:57:35 -0500 )edit

Hello again, Ive come back to this and now there is a different problem similar to the one Ive had before with the haarcascade face recognizer. When the code gets to the line of trying to detect the faces. haar_cascade.detectMultiScale(gray, faces); it finds nothing and the for loop doesnt execute. because the faces vector is empty. Ive made sure the file is loaded as well. There is an update showing my code in my question.

Shoule101 gravatar imageShoule101 ( 2013-02-22 09:37:16 -0500 )edit

Ok now Ive figured out that the Cascade Classifier is not loading the xml file. and Ive placed it in the directory of the project and tried giving it the full path. Nothing is working. Please help

Shoule101 gravatar imageShoule101 ( 2013-02-22 10:05:57 -0500 )edit

if ( ! haar_cascade.load(fn_haar) ) { cerr &lt;&lt; "could not load cascade!" &lt;&lt; endl; return -1; }

rrr I HATE MARKDOWN !

berak gravatar imageberak ( 2013-02-22 10:17:44 -0500 )edit

Ive figured it out. Turns out I need to check my spelling better aha.

Shoule101 gravatar imageShoule101 ( 2013-02-22 11:13:17 -0500 )edit

Thanks, for your help @berak

Shoule101 gravatar imageShoule101 ( 2013-02-22 11:13:35 -0500 )edit