Face recognition based on self-made training set

Face recognition based on self-made training set

Explain

This article references from https://www.cnblogs.com/fpzs/p/10550450.html.
As before, due to the different versions of Opencv, you need to add the header file "include < Opencv2 / imgproc / types" and modify the fifth parameter CV "Haar" do "root" search in detectMultiScale to "cascade" scale "image", and then it's OK.

With the foundation of the previous three articles Face recognition based on self-made training set.Face recognition based on self-made training set.[Opencv comprehensive application] face recognition of self-made training set 1: taking 10 face pictures Finally, it's the last step of the project. Now we can use the trained model to compare the detected face with the one in the face model, and find out whose face it is.

1, Main steps

Using Haar feature classifier in opencv, Harr Cascades
Face recognition in video
1. Turn on the camera.
2. Load face detector and face model.
3. Image preprocessing, such as resize cvtColor, face detection.
4. Compare the detected face with the one in the face model to find out whose face it is.
5. If the face is the one you take photos of, display your name "Safaearth", if not, display "Error"





Some of the codes are explained here

switch (Predict(pImage_roi[i])) //Recognize every face
			{
			//Modify the value after case according to your own needs
			case 41:str = "Safaearth"; break;
			default: str = "Error"; break;
			}

The switch statement is used to compare the detected face with the face model. The case is followed by the group where you want to detect the face. Here, change according to your own needs. My own face picture is in the forty-first group, so write 41 after the case. When the control panel outputs 41 and marks my face name, it indicates that the prediction is correct.

2, All codes

/******************************
1.Turn on the camera.
2.Load face detector, load face model.
3.Image preprocessing to be recognized, such as resize cvtColor, face detection
4.Compare the detected face with the face model to find out whose face it is.
5.If the face is the one you take photos of, display your name "Safaearth".
******************************/
#include<opencv2\opencv.hpp>  
#include<opencv2\face.hpp>
#include<opencv2\core\core.hpp>
#include<opencv2\face\facerec.hpp>
#include <fstream>  
#include <sstream> 
#include <math.h>
#include <opencv2/imgproc/types_c.h>

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

RNG g_rng(12345);
Ptr<FaceRecognizer> model;
//Identify pictures
int Predict(Mat src_image)
{
	Mat face_test;
	int predict = 0;
	//The size adjustment of the intercepted ROI face
	if (src_image.rows >= 120)
	{
		//Change image size, use bilinear difference
		resize(src_image, face_test, Size(92, 112));

	}
	//Determine whether ROI is correctly detected
	if (!face_test.empty())
	{

		predict = model->predict(face_test);
	}
	cout << predict << endl;
	return predict;
}
int main()
{
	VideoCapture cap(0);
	if (!cap.isOpened())
	{
		return -1;
	}
	Mat frame;
	Mat gray;
	//This classifier is used for face detection
	CascadeClassifier cascade;
	bool stop = false;
	//The trained harr classifier of opencv is placed in the same directory as the executable  
	cascade.load("haarcascade_frontalface_alt2.xml");

	model = FisherFaceRecognizer::create();
	//1. Load our own trained classifier
	model->read("MyFaceModel.xml");
	//3. Use camera to collect face and recognize it
	while (1)
	{
		cap >> frame;
		vector<Rect> faces(0);              //Vector container for face storage        
		cvtColor(frame, gray, CV_RGB2GRAY); //Grayscale
		equalizeHist(gray, gray);           //Histogram averaging
		//Face detection
		cascade.detectMultiScale(gray, faces, 1.1, 4, 0 
			                                           //| CASCADE_FIND_BIGGEST_OBJECT,
			                                           //| CASCADE_DO_ROUGH_SEARCH,
                                                         | CASCADE_SCALE_IMAGE,
			                                             Size(30, 30), Size(500, 500));
		Mat* pImage_roi = new Mat[faces.size()];
		Mat face;
		Point text_lb;//Where text is written
		//Frame face
		string str;
		for (int i = 0; i < faces.size(); i++)
		{
			pImage_roi[i] = gray(faces[i]); //Save all faces
			text_lb = Point(faces[i].x, faces[i].y);
			if (pImage_roi[i].empty())
				continue;
			switch (Predict(pImage_roi[i])) //Recognize every face
			{
			//Here is the switch statement to select the face group number to be detected
            //I want to check myself. My image is in group 41, so I write 41 after the case
            //When 41 is output at the console and the face name is marked correctly, the prediction is correct
			case 41:str = "Safaearth"; break;
			default: str = "Error"; break;
			}
			Scalar color = Scalar(g_rng.uniform(0, 255), g_rng.uniform(0, 255), g_rng.uniform(0, 255));//Any value of the selected color
			rectangle(frame, Point(faces[i].x, faces[i].y), Point(faces[i].x + faces[i].width, faces[i].y + faces[i].height), color, 3, 8);//Put in cache
			putText(frame, str, text_lb, FONT_HERSHEY_TRIPLEX, 1, Scalar(255, 255, 0));//Add text
		}

		delete[]pImage_roi;
		imshow("face", frame);
		waitKey(200);
	}

	return 0;
}

3, Results

After running the program, you can see that my face can be successfully identified, and the console also shows that No. 41 is correct

Here is a picture of Solvay meeting and I will recognize it together to see the detection effect of many faces at the same time

You can see that I can still be identified from the faces of many big men



Well, on the whole, the recognition effect is still very good. If you want to continue to increase the recognition accuracy, you can add face photos and training sets to achieve this. So far, the face recognition project of self-made training set has been completed

Tags: OpenCV xml

Posted on Tue, 05 May 2020 01:50:07 -0700 by funsutton