Ask Your Question

face recognition using opencv2.4.3

asked 2013-02-11 10:08:23 -0500

Neha gravatar image

updated 2013-02-12 01:05:47 -0500

rics gravatar image

I'm new to opencv and visual studio and still learning its basics.I'm also doing face recognition project using opencv2.4.3 and visual studio 2010.I have read the tutorial on Face recognition in videos using opencv and implemented the code.the code is as follows with name main.cpp

#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));

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] << " C:\opencv243\data\haarcascades\haarcascade_frontalface_default.xml C:\att\att1.txt 0" << endl;
        cout << "\t C:\opencv243\data\haarcascades\haarcascade_frontalface_default.xml -- Path to the Haar Cascade for face detection." << endl;
        cout << "\t C:\att\att1.txt -- Path to the CSV file with the face database." << endl;
        cout << "\t 0 -- The webcam device id to grab frames from." << endl;
    // Get the path to your CSV:
    string fn_haar = C:\opencv243\data\haarcascades\haarcascade_frontalface_default.xml ;
    string fn_csv = C:\att\att1.txt ;
    int deviceId = 0
    // 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
    // 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;
    // 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 ...
edit retag flag offensive close merge delete



can you please reformat your question, to make it more readable ? edit it , select the code part and press the '011011' button, so the code shows as 'code'

berak gravatar imageberak ( 2013-02-11 10:24:27 -0500 )edit

3 answers

Sort by ยป oldest newest most voted

answered 2013-02-12 01:58:27 -0500

updated 2013-02-12 07:58:39 -0500

By default Visual Studio runs your application without command prompt parameters. Open Project Properties and set your command line options to Configuration Properties -> Debuging -> Command arguments. It solves both your issues. Visual Studio command prompt is a system command prompt (cmd.exe) with ready build environment. You do not need build environment to run sample, you can use just cmd.exe.

edit flag offensive delete link more


thanks for your reply.but in property pages->common properties->framework and references option was there.there wasn't any Common Properties ->general->command line link. however in configuration properties under C/C++;Linker; Manifest tool; XML document generator; Browse information; Custom build set->General all the above titles have command line option witnin them so where should i make command line related changes and what should i add .also on debugging the command prompt is not stable it comes and goes could you provide a solution for this.please guide..thank you

Neha gravatar imageNeha ( 2013-02-12 07:37:57 -0500 )edit

Sorry, it's my fault. I check with ms vc 2010 and fix options name.

Alexander Smorkalov gravatar imageAlexander Smorkalov ( 2013-02-12 07:59:42 -0500 )edit

answered 2013-06-07 11:43:49 -0500

mijl43 gravatar image

hi!... When I use this demo in OPENCV 2.4.5, with VS 2010 in windows 8, the command vector<mat> images; give me an error, somthing like (images not defined), what can I do to fix it?

edit flag offensive delete link more


can you please ask a question instead of this answer ?

berak gravatar imageberak ( 2013-06-07 11:59:45 -0500 )edit

answered 2013-02-12 02:15:13 -0500

I'm starting with just few things in your code:

  • First of all, argv is useless
  • The filename are suppose to be string, ie: "C:\opencv243..."
  • Are you not suppose to use "C:\opencv243..." instead of "C:\opencv243..." on windows?
  • I suppose the bip is coming from C:\att because \a in cout produce a bip, try C:\att...
  • I think your program stop quickly by catch(cv::Exception& e) because you cannot open the file.

Try to fix it, the rest of the code seems correct.

edit flag offensive delete link more


Thanks Mathieu,i changed the file name from C:\att to C:\catt... and the beep has gone also i gave file names in "...."(string) however the command prompt doesnt remain still,i.e. it comes and goes.please suggest the reason for this.also could you please ellaborate the 1st,3rd and 5th points in your previous reply.thank you

Neha gravatar imageNeha ( 2013-02-12 07:31:35 -0500 )edit
Login/Signup to Answer

Question Tools


Asked: 2013-02-11 10:08:23 -0500

Seen: 1,707 times

Last updated: Jun 07 '13