Image acquisition and establishment of face database, one of face recognition

The first step in face recognition is the establishment of a face database.


In the official document, a download of the face library is provided, and you can download it back to see what format it is in.

We choose the download of AT&T face database download : http://www.cl.cam.ac.uk/research/dtg/attarchive/facedatabase.html

AT&T Facedatabase , also known as ORL face database, has 40 people, each with 10 photos. Photos were collected at different times, under different lighting conditions, with different expressions (eyes open and closed, smiling or not), and with different facial details (with or without glasses). All images were taken against a dark uniform background, with frontal vertical faces (some with slight rotation).

The downloaded file is the compressed package. First unzip it to get the orl_faces folder. There are 40 folders under the folder, named from "s1" to "s40". Each folder contains photos of the same person, and there are 10 people in it. face photo.

But they are all  92 x 112 pictures in .pgm  format , which cannot be opened under windows, but can be opened under linux, as follows:



Take a look at the official face library, and you probably know how to store and process pictures. You can refer to the official method to create your own pictures later.

Stop talking nonsense and act now.


1. Implementation steps and methods

The idea is very clear, as follows:

1. Turn on the camera and capture images;


2. Load the face classifier;


3. Face detection, frame the face part and display the image;


4. When a face is detected, take a photo with one key;


5. For the face part, adjust the size and write it as an image file in the specified directory;


2. Implementation code

You can write your own code to implement this function, or you can use the COPY code directly. This is just an auxiliary function.

code show as below:

#include "opencv2/objdetect.hpp"
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
#include <stdio.h>

using namespace cv;
using namespace std;

int resize_save(Mat& faceIn, char *path, int FaceSeq);
int get_face(char *path);

int main(int argc, char *argv[])
{
	if(argc != 2)
	{
		printf("usage: %s <path>\n", argv[0]);
		return -1;
	}
	
	get_face(argv[1]);

    return 0;
}

int get_face(char *path)
{
	CascadeClassifier face_cascade;  
	VideoCapture camera;
	char key = 0;
	Mat	frame;
	int ret = 0;
	int faceNum = 1;
	vector<Rect> faces;  
	Mat img_gray;  
	Mat faceImg;

	camera.open(0); // open the camera
	if(!camera.isOpened())
	{
	  cout << "open camera failed. " << endl;
	  return -1;
	}
	cout << "open camera succeed. " << endl;

	// load face classifier
	ret = face_cascade.load("/root/library/opencv/opencv-3.2.0/data/haarcascades/haarcascade_frontalface_alt2.xml");
	if (! ret)
	{
		printf("load xml failed.\n");
		return -1;
	}
	cout << "load xml succeed. " << endl;

	while (1)  
	{
		camera >> frame;  
		if(frame.empty())
		{
			continue;
		}
		
		cvtColor(frame, img_gray, COLOR_BGR2GRAY);  
		equalizeHist(img_gray, img_gray);  
		
		// detect target
		face_cascade.detectMultiScale(img_gray, faces, 1.1, 3, 0, Size(50, 50));

		for(size_t i =0; i<faces.size(); i++)  
		{
			 /* Draw a rectangle to outline the target */
			rectangle(frame, Point(faces[0].x, faces[0].y), Point(faces[0].x + faces[0].width, faces[0].y + faces[0].height),	
							Scalar(0, 255, 0), 1, 8);	 
		}
		imshow("camera", frame); // show
		key = waitKey(1); // add a delay after display
		
		switch (key)  
		{
			case 'p': // Press P to take a face
				// Only detect one face
				if(faces.size() == 1)
				{
					faceImg = frame(faces[0]);
					ret = resize_save(faceImg, path, faceNum); // resize and save
					if (ret == 0)
					{
						printf("resize_save success.\n");
						faceNum ++;
					}
				}
				break;	

			case 27: // Press Esc to exit
				cout << "Esc..." << endl;
				return 0;
				
			default:  
				break;	
		}  
	}  
}

int resize_save(Mat& faceIn, char *path, int FaceSeq)
{
	string strName;
	Mat image;
	Mat faceOut;  
	int ret;

	if(faceIn.empty())
	{  
    	printf("faceIn is empty.\n");
      	return -1;  
	}  

	if (faceIn.cols > 100)  
	{  
		resize(faceIn, faceOut, Size(92, 112)); // Resize, here the selection is compatible with the official face library image size
		strName = format("%s/%d.jpg", path, FaceSeq); // first create a folder
		ret = imwrite(strName, faceOut); // file name suffix should be correct.jpg .bmp ...
		if(ret == false) // An error occurs, please check whether the file name suffix and file path exist
		{
			printf("imwrite failed!\n");
			printf("please check filename[%s] is legal ?!\n", strName.c_str());
			return -1;
		}
		imshow(strName, faceOut);  
	}  
	waitKey(20);  

    return 0;
}

Instructions:

After the compilation is successful, the executable file must be executed with a parameter, which is the directory where the face image is stored, and must be an existing directory.

Press the "P" key to take a photo and take a face and save it, and press the "Esc" key to exit.

If the imwrite() function fails to execute, please check whether the parameter directory exists and whether the suffix of the file name to be saved is legal.

This program is saved in .jpg format, and is named in increments of 1 using the numbering method.


3. Running example

$ ./get_face abc




Fourth, build a face database

According to the above steps, multiple face photos of a single person can be obtained. The face database consists of face photos of multiple people (>=2), as follows:


The face database must be greater than or equal to 2 people, and photos of 5 people are temporarily collected here.

Referring to the official face database, named from "s1" ~ "s4", each folder contains multiple face photos of the same person.




The ability is limited, welcome to point out the shortcomings, thank you very much!





The first step in face recognition is the establishment of a face database.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325447112&siteId=291194637