This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

Whether you’ve recently started exploring OpenCV or have been using it for a long time, in either case, you’re bound to come across the term “face detection.” As machines get smarter, their ability to mimic human behavior seems to increase, and face detection is one of the advances in AI.

So today, we’re going to take a quick look at what face detection is, why it’s useful, and how with just 15 lines of code you can actually implement face detection on your system!

Let’s start by understanding face detection.

What is face detection?

Face detection is a computer technology based on artificial intelligence that can recognize and locate the presence of human faces in digital photos and videos. In short, the machine’s ability to detect faces in an image or video.

Thanks to major advances in artificial intelligence, it is now possible to detect faces in images or videos, regardless of lighting conditions, skin color, head posture and background.

Face detection is the starting point for several face-related applications, such as face recognition or face verification. Cameras in most digital devices today use face detection technology to detect where faces are and adjust their focus accordingly.

So how does face detection work? I’m glad you asked! The backbone of any face-detection application is an algorithm (a simple step-by-step guide that the machine follows) that helps determine whether an image is positive (with a face) or negative (without a face).

To do this accurately, the algorithm was trained on massive datasets containing hundreds of thousands of images of faces and non-faces. The trained machine learning algorithm detects if there is a face in the image, and if so, places a bounding box.

Face detection using OpenCV

Computer vision is one of the most exciting and challenging tasks in artificial intelligence, and several software packages are available to solve problems related to computer vision. OpenCV is by far the most popular open source library for solving computer vision-based problems.

The OpenCV library has been downloaded more than 18 million times and has an active user community of 47,000 members. It has 2,500 optimization algorithms, including a complete set of classic and state-of-the-art computer vision and machine learning algorithms, making it one of the most important libraries in the field of machine learning.

Face detection in images is a simple 3-step process:

Step 1: Install and import open-CV module:

pip install opencv-python
Copy the code
import cv2
import matplotlib.pyplot as plt # For drawing images
Copy the code

Step 2: Load the XML file into the system

Download the HAar-Cascade Classifier XML file and load it into the system:

Haar-cascade Classifier is a machine learning algorithm. We train the cascade function with a large number of images. There are different types of cascade classifiers according to different target objects. Here we will use the classifier considering face to recognize it as the target object.

You can click here to find the trained classifier XML file for face detection

Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
Copy the code

Step 3: Detect the face and draw a bounding box around it

Use the detectMultiScale() function in the Haar-Cascade classifier to detect faces and draw bounding boxes around them:

# Read the input image
img = cv2.imread('test.png')

# Face detection
faces = face_cascade.detectMultiScale(image = img, scaleFactor = 1.1, minNeighbors = 5)

# Draw a bounding box around the face
for (x, y, w, h) in faces:
      cv2.rectangle(img, (x, y), (x+w, y+h), (255.0.0), 2)

# Displays the number of faces detected in the image
print(len(faces),"faces detected!")

# Draw an image of a face detected
finalimg = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
plt.figure(figsize=(12.12))
plt.imshow(finalimg) 
plt.axis("off")
plt.show()
Copy the code

DetectMultiScale () parameters:

  • Image: MATRIX of type CV_8U that contains the image of the detected object.
  • ScaleFactor: Parameter that specifies how much the image size decreases at each image scale.
  • MinNeighbors: Parameter specifies how many neighbors should be reserved for each candidate rectangle.

You may need to adjust these values to get the best results.

Just like this, you can implement one of the most unique applications of computer vision. A detailed code template for the entire face detection implementation can be found at GitHub below.

Github.com/wanghao221/…

Note: This tutorial only applies to face detection in image files, not live camera sources or video.

Doesn’t it feel great? You just learned how to implement one of the most interesting applications of ARTIFICIAL intelligence and machine learning. Hope you enjoy my blog. Thanks for reading!

I’ve been writing a technical blog for a long time, mostly through nuggets, and this is my Python implementation of face detection. I like to share technology and happiness through articles. You can visit my blog at juejin.cn/user/204034… For more information. Hope you like it! 😊

💌 welcomes your comments and suggestions in the comments section! 💌

The nuggets will be drawing 100 nuggets in the comments section after project Digital. see the event article for details