This article was published as a part of the Data Science Blogathon.
In this article, we will learn to detect the faces in the image using the Mediapipe library we might see different algorithms and models that could perform the same task. Here we will discuss the detection pipeline with the help of the Mediapipe library and undergo code by code explanation. Before moving forward please don’t confuse yourself between face detection and facial landmarks detection as in face detection it detects your overall face and draws the bounding box but in facial landmarks, it detects the features of the face i.e. eyes, nose, and mouth though we will also try to detect some landmarks here as well this is not the optimal way we will do that only because the Mediapipes face detection algorithm offers us the same.
So let’s build our face detection system this time with the help of the Media pipe library.
Import the Libraries
The very first step will be to import all the necessary libraries.
import cv2 import numpy as np import mediapipe as mp import matplotlib.pyplot as plt
So this time we will be performing the face detection functionality with Mediapipe’s face detection model when we try to get into the depth of this model we can find out that it is completely based on BlazeFace which is one of the face detection algorithms and the main reason that it is used is because of its lightweight and very accurate predictions when it comes to face detection even that algorithm is derived from the MobileNetV1/V2 state of the art model. The frame per second of this model is 200-1000 depending on the specification of the devices.
Image Source: CronJ
Before using Mediapipe’s face detection model we have to first initialize the model for that we will be using the simple syntax as mp.solution.face_detection and after initializing the model we will call the face detection function with some arguments. Now we will discuss those arguments:
model_selection
: This argument takes the real integer value only in the range of 0-1 i.e. this model will take the integer value as either 1 or 0. Let us discuss these two types of models.
min_detection_confidence
: This argument also takes the integer value but in the range of [0.0,1.0] and the default value for the same is 0.5 which is 50% confidence i.e. when our model will be detecting the faces it should be at least 50% sure that the face is there otherwise it won’t detect anything.Though we have initialized our model and it is ready to detect the faces but having said that we need to visualize the results too for that we will use the drawing_utils
function to see the result in the images/frames.
mp_face_detection = mp.solutions.face_detection face_detection = mp_face_detection.FaceDetection(model_selection=0, min_detection_confidence=0.5) mp_drawing = mp.solutions.drawing_utils
Code breakdown
So after the initialization part, we will read a particular image on which we will apply our face detection model for that we will be using the cv2.imread
a function that will help us to read the image but during this step it will convert the image format from RGB to BGR.
sample_img = cv2.imread('media/sample.jpg') plt.figure(figsize = [10, 10]) plt.title("Sample Image");plt.axis('off');plt.imshow(sample_img[:,:,::-1]);plt.show()
Output:
Code breakdown
show
function we will plot the image before that we will also use the figure size with the help of figure
function of matplotlib.
So we have read the image as well now comes to the main task where we will use our face detection model and implement its functionality on our sample image for that we will be using the process()
function from FaceDetection
class. This function will return us the major six-coordinate for every face it will detect in the image/frame. Those six coordinates are as follows:
So after getting these 6 points we will be able to plot the bounding box on the image/frame but we will only plot two major key points so that the output is clear and there should be no extra points over the image which could make it congested.
face_detection_results = face_detection.process(sample_img[:,:,::-1]) if face_detection_results.detections: for face_no, face in enumerate(face_detection_results.detections): print(f'FACE NUMBER: {face_no+1}') print('==============================') print(f'FACE CONFIDENCE: {round(face.score[0], 2)}') face_data = face.location_data print(f'nFACE BOUNDING BOX:n{face_data.relative_bounding_box}') for i in range(2): print(f'{mp_face_detection.FaceKeyPoint(i).name}:') print(f'{face_data.relative_keypoints[mp_face_detection.FaceKeyPoint(i).value]}')
Output:
Code breakdown
detection
attribute of face_detection.enumerate
function and for loop, we will iterate over the faces found in the image.
img_copy = sample_img[:,:,::-1].copy() if face_detection_results.detections: for face_no, face in enumerate(face_detection_results.detections): mp_drawing.draw_detection(image=img_copy, detection=face, keypoint_drawing_spec=mp_drawing.DrawingSpec(color=(255, 0, 0), thickness=2, circle_radius=2)) fig = plt.figure(figsize = [10, 10]) plt.title("Resultant Image");plt.axis('off');plt.imshow(img_copy);plt.show()
Output:
Code breakdown
copy
the method so that we won’t lose the original preprocessed part of the image.draw_detection
function with relevant parameters as discussed.
Here’s the repo link to this article. Hope you liked my article on Face detection using the Mediapipe library. If you have any opinions or questions, then comment below.
Read our blogs on various predictions using Machine Learning.
Greeting to everyone, I’m currently working in TCS and previously, I worked as a Data Science Analyst in Zorba Consulting India. Along with full-time work, I’ve got an immense interest in the same field, i.e. Data Science, along with its other subsets of Artificial Intelligence such as Computer Vision, Machine Learning, and Deep learning; feel free to collaborate with me on any project on the domains mentioned above (LinkedIn).
The media shown in this article is not owned by Analytics Vidhya and are used at the Author’s discretion.