Ask Your Question

Revision history [back]

FaceRecognizer

Hello Everyone,

I am new here, I am trying to write my first test with demo facereconizer, It is working but the reconize is not 100%, I think that is require something params to work perfect.

It is my code:

#include "opencv2/core.hpp"
#include "opencv2/face.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/objdetect.hpp"

#include <iostream>
#include <fstream>
#include <sstream>

using namespace cv;
using namespace cv::face;
using namespace std;

int main(int argc, const char *argv[]) {

    CascadeClassifier haar_cascade;
    haar_cascade.load("haarcascade_frontalface_alt.xml");

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

    images.push_back(imread("lucas_01.bmp", CV_LOAD_IMAGE_GRAYSCALE));
    labels.push_back(0);
    images.push_back(imread("lucas_02.bmp", CV_LOAD_IMAGE_GRAYSCALE));
    labels.push_back(0);
    images.push_back(imread("lucas_03.bmp", CV_LOAD_IMAGE_GRAYSCALE));
    labels.push_back(0);
    images.push_back(imread("lailton_01.bmp", CV_LOAD_IMAGE_GRAYSCALE));
    labels.push_back(1);
    images.push_back(imread("lailton_02.bmp", CV_LOAD_IMAGE_GRAYSCALE));
    labels.push_back(1);
    images.push_back(imread("lailton_03.bmp", CV_LOAD_IMAGE_GRAYSCALE));
    labels.push_back(1);
    images.push_back(imread("lailton_04.bmp", CV_LOAD_IMAGE_GRAYSCALE));
    labels.push_back(1);

    images.pop_back();
    labels.pop_back();

    int im_width  = images[0].cols;
    int im_height = images[0].rows;

    Ptr<BasicFaceRecognizer> model = createFisherFaceRecognizer(); // createFisherFaceRecognizer();
    model->train(images, labels);

    model->setLabelInfo(0,"Lucas");
    model->setLabelInfo(1,"Lailton");

    VideoCapture cap(0);

    Mat frame;
    for(;;) {
        cap >> frame;
        Mat original = frame.clone();
        Mat gray;
        cvtColor(original, gray, COLOR_BGR2GRAY);
        vector< Rect_<int> > faces;

        haar_cascade.detectMultiScale(gray, faces);
        for(size_t i = 0; i < faces.size(); i++) {
            Rect face_i = faces[i];
            Mat face = gray(face_i);
            Mat face_resized;
            rectangle(original, face_i, Scalar(0, 255,0), 1);

            cv::resize(face, face_resized, Size(im_width, im_height), 1.0, 1.0, INTER_CUBIC);

            int prediction = model->predict(face_resized);
            if(prediction>=0){
               string box_text = format("Prediction = %d, Label = %s", prediction, model->getLabelInfo(prediction));
               int pos_x = std::max(face_i.tl().x - 10, 0);
               int pos_y = std::max(face_i.tl().y - 10, 0);
               putText(original, box_text, Point(pos_x, pos_y), FONT_HERSHEY_PLAIN, 1.0, Scalar(0,255,0), 2);
            }
        }

        imshow("face_recognizer", original);

        char key = (char) waitKey(20);
        if(key == 27)
            break;
    }
    return 0;
}

Someone can tell me the way to adjust the check reconize, all faces is detected with 0

FaceRecognizer

Hello Everyone,

I am new here, I am trying to write my first test I continue with demo facereconizer, It is working but the reconize is not 100%, I think that is require something params to work perfect.

It is my problem with facereconizer: Please see the code:

#include "opencv2/core.hpp"
#include "opencv2/face.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/objdetect.hpp"

#include <iostream>
#include <fstream>
#include <sstream>

using namespace cv;
using namespace cv::face;
using namespace std;

int main(int argc, const char *argv[]) {

    CascadeClassifier haar_cascade;
    haar_cascade.load("haarcascade_frontalface_alt.xml");

    

I have this variables where is the faces

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

images.push_back(imread("lucas_01.bmp", CV_LOAD_IMAGE_GRAYSCALE));
labels.push_back(0);
images.push_back(imread("lucas_02.bmp", CV_LOAD_IMAGE_GRAYSCALE));
labels.push_back(0);
images.push_back(imread("lucas_03.bmp", CV_LOAD_IMAGE_GRAYSCALE));
labels.push_back(0);
images.push_back(imread("lailton_01.bmp", CV_LOAD_IMAGE_GRAYSCALE));
labels.push_back(1);
images.push_back(imread("lailton_02.bmp", CV_LOAD_IMAGE_GRAYSCALE));
labels.push_back(1);
images.push_back(imread("lailton_03.bmp", CV_LOAD_IMAGE_GRAYSCALE));
labels.push_back(1);
images.push_back(imread("lailton_04.bmp", CV_LOAD_IMAGE_GRAYSCALE));
labels.push_back(1);
images.pop_back();
labels.pop_back();

... here I load the files to vectors

int im_width = images[0].cols;
img_width = images[0].cols; // 197
int im_height = images[0].rows;
Ptr<BasicFaceRecognizer> img_height = images[0].rows; // 197
CascadeClassifier face_cascade;
string classifier = "haarcascade_frontalface_alt.xml";
face_cascade.load(classifier);

I am not sure if createEigenFaceRecognizer is better to use, but I am using it

Ptr<FaceRecognizer> model = createFisherFaceRecognizer(); // createFisherFaceRecognizer();
createEigenFaceRecognizer();
model->train(images, labels);

model->setLabelInfo(0,"Lucas");
model->setLabelInfo(1,"Lailton");
VideoCapture cap(0);

Here is vector to store faces if exist

vector<Rect> faces;
Mat frame;
 for(;;) {
Mat cropImg;
cap >> frame;
 Mat original = frame.clone();
Mat gray;
cvtColor(original, gray, COLOR_BGR2GRAY);
vector< Rect_<int> > faces;
haar_cascade.detectMultiScale(gray, faces);
for(size_t 

Here I call the face_cascade to check if exist faces

face_cascade.detectMultiScale(frame, faces, 1.1, 3, 0, cv::Size(190, 190), cv::Size(200, 200));

cv::Rect roi;
for (int i = 0; i < faces.size(); i++) i++)
{
 Rect face_i = faces[i];
Mat 

Well, now I crop the face = gray(face_i); Mat face_resized; rectangle(original, face_i, Scalar(0, 255,0), 1); cv::resize(face, face_resized, Size(im_width, im_height), like the faces stored, that I am using to try reconizer

     roi.x = faces[i].x; roi.width = faces[i].width;
     roi.y = faces[i].y; roi.height = faces[i].height;
     cropImg = frame(roi);
     cvtColor(cropImg, cropImg, CV_BGR2GRAY);

And finally now I try to identify

     int label = model->predict(cropImg);
     string text;
     if(label>=0 ){
        text = format("Label = %d", label);
     }else{
        text = "Unknow";
     }
     putText(frame, text, Point(faces[i].x, faces[i].y), FONT_HERSHEY_COMPLEX_SMALL, 1.0, 1.0, INTER_CUBIC);
int prediction = model->predict(face_resized);
if(prediction>=0){
string box_text = format("Prediction = %d, Label = %s", prediction, model->getLabelInfo(prediction));
int pos_x = std::max(face_i.tl().x - 10, 0);
int pos_y = std::max(face_i.tl().y - 10, 0);
putText(original, box_text, Point(pos_x, pos_y), FONT_HERSHEY_PLAIN, 1.0, Scalar(0,255,0), 2);
CV_RGB(0, 255, 0), 1.0);
}
 }
imshow("face_recognizer", original);
char key = (char) waitKey(20);
if(key == 27)
break;
}
return 0;
}

I not know why it not work, it is exactly how is samples, maybe I am losing something, if a person that is not in my database go to camera, it identify the face with a wrong entrie.

Someone can tell me the way to adjust the check reconize, all faces is detected with 0help me?