Ask Your Question

FisherFaceRecognizer- prediction result always 1

asked 2013-03-03 15:18:57 -0500

giarandrea gravatar image

updated 2013-03-04 01:18:04 -0500

Hi guys, I'm, trying to replicate what posted at: (it's Facial Recognition from Video, using FisherFace algorithm)

I've changed the quoted code only to retrieve vector<mat> images and vector<int> labels from Database. They are input parameters for my function, and I've verified that the right .bmp images are opened What is happening is that I always get result 1 for "prediction" vector..Even if i use different photos with different expressions (of my brother too).

I know it may seem vague, and for that reason I attach the code, but have you any suggestion on what the problem can be? thanks A

#include "F_imageprocessing.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include "opencv/cv.h"
#include "opencv/cvaux.h"
#include "main_derp.h"

#include "opencv2/contrib/contrib.hpp"
imageprocessing::imageprocessing(QObject *parent) :

int imageprocessing::imagecheck(QVector<QString> img_paths, QVector<int> img_ID)
    int prediction = 0;
    //string fn_haar = (QDir::currentPath()+"/haarcascade_frontalface_default.xml").toStdString();
    string fn_haar = "/opt/local/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml";
    vector<Mat> images;
    vector<int> labels;
    for (int j=0; j<img_paths.count(); j++)
            QImage q_image;
            images.push_back(imread(img_paths[j].toStdString().c_str(), 0));
        }else return -1;
    int im_width = images[0].cols;
    int im_height = images[0].rows;

    // Get a handle to the Video device:
    VideoCapture cap(0);
    Mat frame;

    //start of Algorithm (fisherface)
    Ptr<FaceRecognizer> model = createFisherFaceRecognizer();
    //Ptr<FaceRecognizer> model = createEigenFaceRecognizer();
    model->train(images, labels);
    CascadeClassifier haar_cascade;
    for(int zz=0;zz<3;zz++) {
    //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 in the frame:
        vector< Rect_<int> > faces;
        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);
            prediction = model->predict(face_resized);
            /*create rectangle around detected image
            rectangle(original, face_i, CV_RGB(0, 255,0), 1);
            string box_text = format("Prediction = %d", prediction);
            // Calculate the position for annotated text and put the rectangle into image
            int pos_x = std::max( - 10, 0);
            int pos_y = std::max( - 10, 0);
            putText(original, box_text, Point(pos_x, pos_y), FONT_HERSHEY_PLAIN, 1.0, CV_RGB(0,255,0), 2.0);*/


        /*Show the result:
        imshow("face_recognizer", original);
        char key = (char) waitKey(20);*/
        // Exit this loop on escape:
    return prediction;
edit retag flag offensive close merge delete


show code please, so we can see, what you changed, and have a guess, what might be leading to that error. ( eg., did you get that part about using grayscales right ? )

berak gravatar imageberak ( 2013-03-03 15:24:53 -0500 )edit

also, linking to something on your local disc, like c:\something won't work on the internet ;)

berak gravatar imageberak ( 2013-03-03 15:32:22 -0500 )edit

thanks for your answer (actually I felt something strange in the link :)) I paste my code in a comment! (grayscale I confirm, and I verified that Mat gray was present and "gray")

giarandrea gravatar imagegiarandrea ( 2013-03-03 15:36:47 -0500 )edit

hmm, there's some /* comment there(near the end), that does not match the loop, that won't even compile .. please fix the /* thing, none of your 2 there actually ends with */

berak gravatar imageberak ( 2013-03-03 15:52:39 -0500 )edit

it compiles, perhaps the "/" is hidden and you have to move right the scrollbar */ } / Show the result:

giarandrea gravatar imagegiarandrea ( 2013-03-03 15:55:02 -0500 )edit
        putText(original, box_text, Point(pos_x, pos_y), FONT_HERSHEY_PLAIN, 1.0, CV_RGB(0,255,0), 2.0);*/
    /* Show the result:
giarandrea gravatar imagegiarandrea ( 2013-03-03 15:55:32 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2013-03-04 02:41:12 -0500

What I strongly suggest for every computer vision project is proper validation. You really need figures to talk about. A lot of computer vision algorithms need a lot of fine tuning to work with a high recognition rate. Especially for face recognition you will need preprocessing to achieve higher recognition rates (for example the proper alignment of faces). What you need for a cross validation is the data you expect in your use case, in the video examples it is the faces found by the cascade classifier.

Once you have collected your data, perform a 10-fold cross validation for example. This will give you a hint of the recognition rate you can expect for the classifier. I have shown how to do this with OpenCV and Python in this post:

If the recognition rate is too low, go for preprocessing. I have written down a list of relevant publications (and source code) in this post:

edit flag offensive delete link more


thanks for your reply! I'll follow the steps you wrote and let u know

giarandrea gravatar imagegiarandrea ( 2013-03-04 04:27:17 -0500 )edit

Question Tools

1 follower


Asked: 2013-03-03 15:18:57 -0500

Seen: 2,008 times

Last updated: Mar 04 '13