Ask Your Question
0

Face Recognition in Video using OpenCV gives unhandled exception

asked 2012-11-02 06:32:07 -0600

karanjthakkar gravatar image

updated 2012-11-02 06:35:10 -0600

I am trying to use the Face Recognition in video sample provided with OpenCV. The only modification I've done is: Instead of using command line arguments to provide CSV and Cascade classifier paths, I have given them directly in the code. This is the code:

#include "stdafx.h"
#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 != 4) {
    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;
    //exit(1);
}
// Get the path to your CSV:
string fn_haar = "C:\\OpenCV-2.4.2\\opencv\\data\\haarcascades\\haarcascade_frontalface_default.xml";
string fn_csv = "C:\\Users\\gaspl\\Desktop\\train.txt";
int deviceId = 1;
// 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(deviceId);
// Check if we can use this device at all:
if(!cap.isOpened()) {
    cerr << "Capture Device ID " << deviceId << "cannot be opened." << endl;
    return -1;
}
// Holds the current frame from the Video device:
Mat frame;
for(;;) {
    cap >> frame;
    // Clone the current frame:
    Mat original = frame.clone();
    // Convert the current frame to grayscale:
    Mat gray;
    cvtColor(original, gray, CV_BGR2GRAY);
    // Find the faces ...
(more)
edit retag flag offensive close merge delete

Comments

Silly question... What exactly do you type into the command line to run this program? When I run facerec_video.exe <C:\Users\Name\Documents\facerec\haarcascade_frontalface_default.xml> <C:\Users\Name\Documents\facerec\traing.csv> <1> I get "< was unexpected at this time" is it becuase i moved the xml file to a new folder? Thanks

hammerhands gravatar imagehammerhands ( 2012-11-18 09:56:42 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2012-11-04 06:33:40 -0600

updated 2012-11-04 07:35:10 -0600

As we have already concluded by mail. This happens, because only one label is given in your CSV file:

C:\Training\extract0.jpg;0
C:\Training\extract1.jpg;0
C:\Training\extract2.jpg;0

The Fisherfaces method needs at least two classes to learn a model. This case should have been caught and OpenCV 2.4.2 throws the following exception on my system:

OpenCV Error: Bad argument (At least two classes are needed to perform a LDA. Reason: Only one class was given!) in lda, file /home/philipp/github/libfacerec/src/subspace.cpp, line 150
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/philipp/github/libfacerec/src/subspace.cpp:150: error: (-5) At least two classes are needed to perform a LDA. Reason: Only one class was given! in function lda

Which describes the reasons for not learning a Fisherfaces model and is probably the same exception you encounter.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2012-11-02 06:32:07 -0600

Seen: 3,177 times

Last updated: Nov 04 '12