Face detection software based on OpenCV (including Python source code + UI interface + graphic detailed explanation)

Software Function Demonstration

Please add a picture description

Abstract: 人脸检测The goal is to find the corresponding positions of all the faces in the image. The output of the algorithm is the coordinates of the circumscribed rectangle of the face in the image, and may also include information such as postures such as tilt angles. This article introduces the technical principle of its implementation in detail, and at the same time gives a complete Pythonimplementation code, and PyQTrealizes the UI interface, and displays the face detection function of pictures, videos and cameras. This article provides complete Python code and tutorials for reference and study by interested partners. See the end of the article for how to obtain complete code resource files.

Click to jump to the end of the article "Complete related documents and source code" to obtain

---

1 Introduction

Face Detection (Face Detection) is to give an image and find out the positions of all faces in the image, usually framed by a rectangular frame, the input is an image img, and the output is several rectangular frame positions containing faces. It is the basis of face recognition. The main difference between face detection and face recognition is that face detection only needs to detect the position of the face in the picture, while face recognition is after detecting the position of the face, it also needs to be compared with the position of the face in the database. Match the face data of the detected face to identify which specific person the detected face belongs to. This paper implements face detection and interface display based on OpenCV, and will further update the function development related to face recognition in the future.

The following is a simple software interface designed by the blogger, which can realize the face detection function of pictures, videos and cameras, and also provides the function of saving the detection results of pictures and videos. Interested friends can try it themselves.

觉得不错的小伙伴,感谢点赞、关注加收藏!如果大家有任何建议或意见,欢迎在评论区留言交流!

The initial interface of the software is shown in the figure below:
insert image description here

2. Software effect demonstration

First, let's take a look at the actual effect of face detection through the animation. The main function of this software is to detect the position of faces in pictures, videos and camera images. The recognition results are visually displayed in the interface and images. The demonstration effect is as follows .

2.1 Image face detection

This software can perform face detection by selecting a picture file by yourself. After clicking the picture selection button icon to select a picture, the result of face detection will be displayed directly: including the number of faces and location information, and you can check the individual face through the drop-down box The detection result, and you can click the save button to save the detection result picture. The interface display of this function is shown in the figure below:

Please add a picture description

2.2 Video face detection

This software can select a video for face detection, click the video button to select the video to be detected, the software will detect the face frame by frame, and record the detection results in the table below, and you can click the save button to save The result of the video detection. The interface display of this function is shown in the figure below:
insert image description here

2.3 Camera face detection

In actual use scenarios, we often use the device camera to obtain real-time images, and at the same time need to detect faces in the images, so this article also implements the face detection function of the camera. Click the camera button to turn on the camera, the software displays the real-time picture and starts to detect the face position in the picture. The interface of this function is shown in the figure below:
insert image description here

3. Introduction to the principle of face detection

3.1 Basic Principles

There are two main types of algorithms for face detection: knowledge-based and statistics-based.
The knowledge-based method mainly uses prior knowledge to regard the human face as a combination of organ features, and detects human faces according to the characteristics of eyes, eyebrows, mouth, nose and other organs and the geometric positional relationship between them. Commonly used methods: template matching, face features, shapes and edges
, texture features, color features.
The method based on statistics regards the face as a whole pattern—a two-dimensional pixel matrix. From a statistical point of view, a face pattern space is constructed through a large number of face image samples, and whether a face exists is judged according to the similarity measure. Commonly used methods: principal component analysis and eigenfaces, neural network methods, support vector machines, hidden Markov models, Adaboost algorithm.

This article mainly uses OpenCV's Haar classifier to detect faces. The Haar classifier is actually an application of the Boosting algorithm. The Haar classifier uses the AdaBoost algorithm in the Boosting algorithm. It just cascades the strong classifiers trained by the AdaBoost algorithm, and uses high-efficiency in the underlying feature extraction. Rectangular features and integral map methods.

The main points of the Haar classifier algorithm are as follows:
① Use Haar-like features for detection.
② Use the integral image (Integral Image) to accelerate the evaluation of Haar-like features.
③ Use the AdaBoost algorithm to train a strong classifier that distinguishes between faces and non-faces.
④ Use screening cascade to cascade strong classifiers together to improve accuracy.

3.2 Use the Haar cascade detector to perform face detection on pictures

# coding:utf-8
import cv2

# 绘图展示
def cv_show(name,img):
	cv2.imshow(name, img)
	cv2.waitKey(0)
	cv2.destroyAllWindows()

# 读取图片
img = cv2.imread('images/test1.jpg')
# cv_show('img',img)

# 转为灰度图
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# cv_show('gray', img_gray)

# 构造harr检测器
face_detector = cv2.CascadeClassifier('models/haarcascade_frontalface_default.xml')

# 对图像中的人脸进行检测
detections = face_detector.detectMultiScale(img_gray,scaleFactor=1.3)

# 解析并画出人脸方框
for x,y,w,h in detections:
	cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)

insert image description here

detectMultiScale(image[, scaleFactor[, minNeighbors[, flags[, minSize[, maxSize]]]]])
parameter description:
image: image to be detected, usually a grayscale image to speed up detection
scaleFactor: set a reduction ratio, for The image is gradually reduced to detect. The larger the parameter setting, the faster the calculation speed, but it may miss a certain size of the face. The default is 1.1. In fact, this parameter can be set according to the pixel value of the image. The speed of pixel reduction can be faster, usually between 1 and 1.5.
minNeighbors: Determine that a face frame must have at least n candidate values, the higher the quality, the better (default is 3)
minSize and maxSize are used to limit the size of the obtained target area. That is, the maximum and minimum size of the face frame, such as minSize=(40,40), means that the minimum size of the face frame should not be less than 40*40.

According to the actual situation and needs, the above parameters can be adjusted to achieve the best detection effect.

Based on the above principles, in order to facilitate display and learning. The blogger finally developed a complete UI interface that can detect faces in pictures, videos, and cameras, and tested the entire software in detail. The complete UI interface, test pictures and videos, and code files have all been packaged and uploaded, and interested friends can obtain them through the download link.
insert image description here

【method of obtaining】

All the complete program files involved in this article: including python source code, UI files, etc. (see the picture below), see the end of the article for how to obtain them:
insert image description here

Note: The code is developed using Pycharm+Python3.8, the main program of the running interface is MainProgram.py, the test picture script can run img_test.py; the test camera script can run cameraTest.py; the test video script can run videoTest.py. To ensure that the program runs smoothly, please configure the version of the Python dependency package according to requirements.txt.

Follow the business card below [Axu Algorithm and Machine Learning], and reply [Face Detection] to get the download method


conclusion

The above is the entire content of the face detection software developed by the blogger. Due to the limited ability of the blogger, it is inevitable that there are omissions. I hope that friends can criticize and correct.
If you have any suggestions or comments about this article, please leave a message in the comment area for exchange!

Friends who feel good, thank you for your likes, attention and collection! More exciting content will continue to be updated...

Guess you like

Origin blog.csdn.net/qq_42589613/article/details/130706051