Design and implementation of face recognition system based on python

Design and Implementation of Face Recognition System Based on Python for Case Sharing

Face recognition means that the program judges whether there is a face in the input image, and recognizes the person corresponding to the image with a face. That is, the face recognition we often say generally includes two parts: face detection and face recognition. The corresponding modules in opencv are introduced separately below.

On the opencv official website, there are many recommended face online datasets: http://face-rec.org/databases/, you can download it yourself if you need it.

One: face detection

In face detection, its main task is to construct a classifier that can distinguish between instances that contain faces and instances that do not contain faces.

Two: basic principles

Three trained cascaded classifiers are provided in opencv. As the name implies, the cascade classifier screens through different features step by step, and finally obtains the classification it belongs to. It disassembles a complex classification problem into simple classification problems. With the judgment of the cascade conditions, it can step by step A large number of negative samples are screened out, which greatly improves the speed of subsequent classification. The following is a brief introduction:

2.1: Haar cascade classifier

The development history of the haar cascade classifier will not be introduced. It mainly divides pixels into many modules, and then calculates the difference between adjacent modules to reflect the grayscale change of the image. In the end, the relevant personnel divided the haar features into: 4 edge features, 8 line features, 2 center point features and 1 diagonal feature.
insert image description here
In addition to haar, opencv also provides a cascade classifier of the Hog feature and the LBP algorithm. The Hog cascade classifier is mainly used for pedestrian detection. The LBP algorithm will be introduced in the LBPH face recognition section later.

The use of cascade classifiers: opencv_createsamples.exe and opencv_traincascade.exe in the root directory of opencv can be used to train cascade classifiers, but self-training is time-consuming. Opencv provides trained cascade classifiers for users to use. Haar, HOG, and LBP cascade classifiers are stored in the corresponding haarcascades, hogcascades, and lbpcascades folders. They are stored in opencv in the form of .xml files. In the source file of , different .xml files can detect different types, such as: eyes, glasses, front face, nose, etc.

The syntax for loading a cascaded classifier is:

object=cv2.CascadeClassifier(filename)
其中filename为分类器的路径和名称


在使用级联分类器的时候可以通过下面的一些方法去获取需要的级联分类器.xml文件:

```has-numbering
1:在安装opencv的目录下查找xml文件
2:直接在网络上找到相应的.xml文件,下载并使用。

2.1.1: Function introduction

In opencv, face detection uses the cv2.CascadeClassifier.detectMultiScale() function, which can detect all faces in the picture. This function is called by the classifier object:

objects=cv2.CascadeClassifier.detectMultiScale(image,scaleFactor,minNeighbors,flags,minSize,maxSize)
image:待检测图片,通常为灰度图。
scaleFactor:表示在前后两次的扫描中,搜索窗口的缩放比例。
minNeighbors:表示构成检测目标的相邻矩形的最小个数,默认情况下,该值为3,即有3个以上的检测标记存在时才认为人脸存在。该值越大,检测的准确率就越高,但同时无法被检测到的人脸就越多。
flags:该参数通常被省略,在使用低版本的opencv(1.0X版本)可能被设置为CV_HAAR_DO_CANNY_PRUNING,表示使用canny边缘检测器来拒绝一些区域。
minSize:目标的最小尺寸,小于这个尺寸的目标被忽略。
maxSize:返回值,目标对象的矩形框向量组。

########The specific implementation of face detection in opencv###########

import cv2
dir=r"F:\my_project\opencv\face_recognize"

image=cv2.imread(r"F:\my_project\opencv\img\30.jpg")
#加载分类器模型
faceCascade=cv2.CascadeClassifier(r"F:\my_project\opencv\haarcascade_frontalface_default.xml")
gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

faces=faceCascade.detectMultiScale(
                                   gray,
                                   scaleFactor=1.15,
                                   minNeighbors=5,
                                   minSize=(5,5)
                                   )
 #逐个标注人脸
for (x,y,w,h) in faces:
    cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),2)
cv2.imshow("dect",image)
cv2.waitKey()
cv2.destroyAllWindows()	
=>人脸向量组: [[417  89  31  31]
			  [343  93  28  28]
			  [262  95  29  29]
			  [135  98  28  28]
		  	  [499  98  27  27]
			  [199 100  29  29]]
=>发现6个人脸!

insert image description here

2.2: LBPH face recognition

Face recognition is to find a model that can reflect features in a brief and differentiated way. It first extracts features from the current face using the same method as detection, and then finds the nearest neighbor sample of the current feature from the existing feature set, so that Get the label of the current sample.

There are three face recognition methods in opencv, namely LBPH, EigenFishface, and Fisherfaces methods. The following is only a brief introduction to LBPH.

The LBPH (Local Binary Pattern Histogram) model is based on the LBP (Local Binary Pattern) algorithm. LBP was first used as texture description, and it has been widely used because of its excellent expression effect of local texture features of images.

Since more applications are introduced here, if you need to know more about the principle, you can check: https://blog.csdn.net/heli200482128/article/details/79204008

2.2.1: LBPH function introduction

In opencv, you can use the cv2.face.LBPHFaceRecognizer_create() function to generate the LBPH instance model, use the cv2.face_FaceRecognizer.train() function to complete the training, and use the cv2.face_FaceRecognizer.predict() function to complete face recognition.

1: cv2.face.LBPHFaceRecognizer_create() function

retval=recognizer=cv2.face.LBPHFaceRecognizer_create(radius, neighbors, grid_x, grid_y, threshold)
radius:半径值,默认为1
neighbors:邻域点的个数,默认采用8邻域
grid_x:将LBP特征图划分为一个个单元格时,每个单元格在水平方向上的像素个数,该参数值默认为8,即将LBP特征图像在行方向上以8个像素为单位分组。
grid_y:将LBP特征图划分为一个个单元格时,每个单元格在垂直方向上的像素值,该参数值默认为8,即将LBP特征图像在列方向上以8个像素为单位分组。
threshold:在预测时使用,如果大于该阈值,就认为没有识别到任何目标对象。
以上全部参数均为可选参数

2: cv2.face.LBPHFaceRecognizer.train() function

None=cv2.face.LBPHFaceRecognizer.train(src,labels)
src:训练图像,用来学习的人脸图像
labels:标签,人脸图像所对应的标签
该函数没有返回值

3: cv2.face_FaceRecognizer.predict() function

This function judges a face image to be tested, finds the face image with the closest distance to the current image, and marks the current image to be tested as its corresponding label if it is the closest to that face image. Of course, if the distance between the image to be tested and all face images is greater than the specified threshold in the cv2.face.LBPHFaceRecognizer_create() function, no corresponding result is found, that is, the current face cannot be recognized.

label,confidence=recognizer.predict(src)
 src:需要识别的人脸图片
 label:返回的识别结果标签
 confidence:返回的置信度评分,置信度评分用来衡量识别结果与原有模型之间的距离,0表示完全匹配,通常小于50的值可以接受,大于80的则认为差别较大。

######################Face recognition case##########################

System function description

Project function demo
insert image description here
insert image description here
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/qq_42135426/article/details/128472565