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