How to increase the accuracy of EigenFaceRecognizer, it recognizes two people as one?

asked 2015-07-02 11:15:36 -0600

I'm trying to recognize frontal faces using EigenFaceRecognizer in C++.

The problem is:

1) at high threshold, two people are recognized as same person and a "NEW" face is also recognized, instead of stating it as a new face

2) at low thresholds, a face already in the training set is recognized as a new face

3) False Positives are also encountered. Though not a concern but if an easy way is suggested to decrease them it will be appreciated>

Is there any way to improve the recognizer to identify faces accurately?

Following is what I am doing.

#include"Header.h"
#include<opencv2\opencv.hpp>                //For opencv functions
#include<opencv2\highgui\highgui.hpp>       //For window based functions
#include<fstream>                           //For dealing with I/O operations on file

using namespace std;
using namespace cv;


// Function to read the File containing paths and labels of the training images and push them into images and     labels vector
static void read_data(vector <Mat> & images,vector <int>& labels, char separator=' ')
{
ifstream file("images.txt");   //images.txt contains paths and labels separated by a space
string line;
string a[2];

 while(getline(file,line))  // read images.txt line by line 
 {
     int i=0;
    stringstream iss(line);
    while (iss.good() && i < 2)
    {
        iss>>a[i]; 
        ++i;
    }
    images.push_back(imread(a[0],CV_LOAD_IMAGE_GRAYSCALE)); // a[0] = "path of images"
    labels.push_back(atoi(a[1].c_str()));  //a[1] = "labels"
}

file.close();
  }


  // Function to take input from webcam and recognize faces 
   int face_recognition::face_rec(int time_flag, int trigger_flag)
  {
vector<Mat> images;    //stores the paths of all images
vector<int> labels;    //stores the corresponding labels

//function call to function read_data
read_data(images,labels);    

//take the size of the sample images
int im_width = images[0].cols;           
int im_height = images[0].rows;

//threshold is the minimum value of magnitude of vector of EigenFaces
double threshold=10.0;    

//create instance of EigenFaceRecognizer
Ptr<FaceRecognizer> model = createEigenFaceRecognizer(10,threshold);  
double current_threshold =model->getDouble("threshold");

// set a threshold value, for face prediction
model->set("threshold",5000.0);      

// train the face recognizer using the sample images
model->train(images,labels);         

// Create face_cascade to detect people
CascadeClassifier face_cascade;
if(!face_cascade.load("haarcascade_frontalface_default.xml"))   // load haarcascade_frontaface_default.xml
{ 
    cout<<"ERROR Loading cascade file";
    return 1;
}

// capture the video input from webcam
VideoCapture capture(CV_CAP_ANY);    

capture.set(CV_CAP_PROP_FRAME_WIDTH, 320);
capture.set(CV_CAP_PROP_FRAME_HEIGHT, 240); 

Size frameSize(static_cast<int>(320), static_cast<int>(240));

//initialize the VideoWriter object
VideoWriter oVideoWriter ("MyVideo.avi", CV_FOURCC('P','I','M','1'), 20, frameSize, true);  // video is save in the  VS  project

if(!capture.isOpened())
{
    cout<<"Error in camera";
    return 1;
}

Mat cap_img, gray_img;

//store the detected faces
vector<Rect> faces;   
while(1)
{
    //capture frame by frame in cap_img
    capture>>cap_img;   
    waitKey(10);


    // Image conversion: Color to Gray
    cvtColor(cap_img,gray_img,CV_BGR2GRAY);   

    //Histogram Equilization to increase contrast by stretching intensity ranges
    equalizeHist(gray_img,gray_img);      




    // detects faces in the frame
    //CV_HAAR_SCALE_IMAGE to scale the size of the detect face 
    //CV_HAAR_DO_CANNY_PRUNING to increase speed as it skips image regions that are unlikely to contain a face
    face_cascade.detectMultiScale(gray_img,faces,1 ...
(more)
edit retag flag offensive close merge delete