Face Landmark Detection with Mediapipe and OpenCV
Have you ever wondered how these social media apps are able to offer mind-blowing face filters, face switching, and so much more? All of these amazing filters are readily created using the face landmark detection technology. Facial landmark identification is the process of identifying key facial landmarks, tracking them, and representing prominent facial regions.
In this tutorial, we will learn how to utilize mediapipe and openCV to do face Landmark Detection. Facial landmarks have been successfully used in face alignment, head pose estimation, face swapping, blink detection, and many more applications.
So, let’s get this tutorial started….
First, make sure you have all of the required Python libraries loaded on your system, and then import all of them as shown below.
import cv2 import mediapipe as mp
First, we will use our Mediapipe library to initialize the facial landmark feature detection model. Our main function, FaceMesh(), which handles landmarks detection, is part of the face_mesh method. The Mediapipe Facial Mesh approach constructs a metric 3D space and employs the screen positions of face landmarks to estimate a face morph inside that space, all in real-time.
# Face Mesh mp_face_mesh = mp.solutions.face_mesh face_mesh = mp_face_mesh.FaceMesh()
The image will now be read using the cv2.imread method and will change the color format since OpenCV utiliszs BGR rather than RBG.
image = cv2.imread("image1.jpg") height, width,_ = image.shape print("Height, width", height, width) rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
Next, we will utilize the image’s height and width, which were previously specified, as a guide to mark the points. By sketching a circle, we will extract points with the help of a for loop.
for facial_landmarks in result.multi_face_landmarks: for i in range(0, 468): pt1 = facial_landmarks.landmark[i] x = int(pt1.x * width) y = int(pt1.y * height) cv2.circle(image, (x, y), 1, (0, 0, 0), 5) #draws cirle on image, center_coordinates, radius, color(BGR), thickness)
The cv2.imshow() function is used to draw the picture on the window.
cv2.imwrite("landmark.jpg", image) cv2_imshow(image) cv2.waitKey(0)
Hurray, it’s all finished! Let’s have a look at the results.
So, we’ve displayed the original image and the landmark image above. We hope that you have found this tutorial to be helpful.
Leave a Reply