facerecognition using fisherfaces [closed]

asked 2018-03-22 06:34:47 -0600

shive gravatar image

Hello.. Iam trying to recognize face using fisherfaces, am using opencv 3.2.0 on ubuntu 16.04.I want to recognize face through web cam and also from image file path. But am unable to get the output. Any help would be appreciated. Thanks in advance..

Sample.cpp

#include "lda_mine.h"
#include "unistd.h"

static Mat norm_0_255(InputArray _src) {
    Mat src = _src.getMat();
// Create and return normalized image:
    Mat dst;
    switch(src.channels()) {
    case 1:
    cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC1);
    break;
    case 3:
    cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC3);
    break;
    default:
    src.copyTo(dst);
    break;
    }
    return dst;
}

void SetLabelInfo( Ptr<BasicFaceRecognizer> model, int Id, string name)
{
model->setLabelInfo(Id,name);
cout << model->getLabelInfo(Id);
return;
} 

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(Error::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()) 
          {
        Mat I = imread(path, 0);
            images.push_back(I);
            labels.push_back(atoi(classlabel.c_str()));  
         char* db = getenv("ENHANCE_DB");
        if(db && strcmp(db,"1") == 0)
        {
        Mat flip;
        cv::flip(I,flip,1);
        images.push_back(flip);
        labels.push_back(atoi(classlabel.c_str()));
        if(0)
        {
            int x,y;
            x = flip.rows;
            y = flip.cols;
            Point2f p (x/2,y/2);
            Mat A = I(Range(0,x-1),Range(0,y/2)); 
            Mat B = flip(Range(0,x-1),Range(y/2+1,y)); 
            Mat C = Mat::zeros(x,y,I.type());
            cv::hconcat(A, B, C);

            images.push_back(C);
            labels.push_back(atoi(classlabel.c_str()));
            A = I(Range(0,x-1),Range(y/2+1,y)); 
            B = flip(Range(0,x-1),Range(0,y/2)); 
            C = Mat::zeros(x,y,I.type());
            cv::hconcat(A, B, C);

            images.push_back(C);
            labels.push_back(atoi(classlabel.c_str()));
        }
     }  
        }
      } 
 } 
// int main(int argc, const char *argv[]) 
Ptr<BasicFaceRecognizer> lda_train(int argc, const char *argv[])
 {

      Mat image;
 if (argc != 3) {
        cout << "usage: " << argv[0] << " -train" <<  " <train.csv> " << endl;
        exit(1);
}   
      string output_folder = ".";
    if (argc == 3) {
        output_folder = string(argv[2]);
  }  
  string fn_csv = string(argv[2]);

  vector<Mat> images;
  vector<int> labels;

  try{
    read_csv(fn_csv,images,labels,';');
    }
   catch (cv::Exception& e) {
        cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl;
        exit(1);
  }


  if(images.size()<=1)
  {
  string error_message="This demo needs atleast 2 images.";
  CV_Error(Error::StsError, error_message);
  }

  int height= images[0].rows;

    Mat testSample = image;
    int testLabel = 1;
    //images.pop_back();
   // labels.pop_back();



Ptr<BasicFaceRecognizer>model = createFisherFaceRecognizer(num_components, threshold);
   if(access("train_csv.xml", F_OK ) != -1 )
 {
  model->load("train_csv.xml");
//  model->update(images,labels);
}
 else
  { 
    model->train(images,labels);
/*  int predicted = model->predict(testSample);
    int predictedLabel = -1;
    double confidence = 0.0;
    model->predict(testSample,predictedLabel,confidence);
*/ 
    Mat eigenvalues = model->getEigenValues();
    Mat vec = model->getEigenVectors(); 
    Mat mean = model->getMean();
    if(argc==2)
    {
     imshow("mean",norm_0_255(mean.reshape(1,images[0].rows)));
    }
     else
     {
      imwrite(format("%s/mean.png", output_folder.c_str()), mean.reshape(1, images[0].rows));
     }

    for(int i=0 ...
(more)
edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-08-30 03:15:59.366241

Comments

  • do you really expect, anyone will debug 450 lines of code for you ?
  • "But am unable to get the output." -- please try to give a proper description of your problem.
berak gravatar imageberak ( 2018-03-22 06:46:12 -0600 )edit