How to Use MediaPipe Face Landmarks in Python?

Estimated read time 3 min read

MediaPipe is a popular framework for building cross-platform, real-time computer vision applications. It provides a wide range of pre-built modules, including face landmark detection, which can be used to detect and track facial landmarks in real-time.

To use MediaPipe face landmarks in Python, you can follow these steps:

  1. Install the MediaPipe library: MediaPipe can be installed using pip. Open your terminal and type the following command:
pip install mediapipe
  1. Import the necessary modules: To use the face landmark detection module, you need to import the cv2 module for image processing and the mediapipe module for face landmark detection. You can import them using the following code:
import cv2
import mediapipe as mp
  1. Initialize the face landmark detector: You can use the mp.solutions.face_mesh module to initialize the face landmark detector. This module provides a pre-trained machine learning model that can detect 468 facial landmarks in real-time. You can initialize the face landmark detector using the following code:
mp_face_mesh = mp.solutions.face_mesh
face_mesh = mp_face_mesh.FaceMesh()
  1. Process the input image: You can use the cv2.imread() function to read the input image and the cv2.cvtColor() function to convert the color space of the image to RGB, which is the format expected by the face landmark detector. You can also resize the image to a smaller size to improve the processing speed. Here’s an example code:
image = cv2.imread('input_image.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.resize(image, (640, 480))
  1. Detect the face landmarks: You can use the face_mesh.process() method to detect the face landmarks in the input image. This method returns a FaceMesh object that contains the landmarks as a list of 468 tuples. Here’s an example code:
results = face_mesh.process(image)
landmarks = results.multi_face_landmarks[0].landmark
  1. Draw the landmarks on the image: You can use the cv2.circle() function to draw a circle at each landmark position on the input image. Here’s an example code:
for landmark in landmarks:
    x = int(landmark.x * image.shape[1])
    y = int(landmark.y * image.shape[0])
    cv2.circle(image, (x, y), 2, (0, 255, 0), -1)
  1. Display the output: You can use the cv2.imshow() function to display the output image with the landmarks. Here’s an example code:
cv2.imshow('Output', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Here’s the full code that combines all of these steps:

import cv2
import mediapipe as mp

mp_face_mesh = mp.solutions.face_mesh
face_mesh = mp_face_mesh.FaceMesh()

image = cv2.imread('input_image.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.resize(image, (640, 480))

results = face_mesh.process(image)
landmarks = results.multi_face_landmarks[0].landmark

for landmark in landmarks:
    x = int(landmark.x * image.shape[1])
    y = int(landmark.y * image.shape[0])
    cv2.circle(image, (x, y), 2, (0, 255, 0), -1)

cv2.imshow('Output', image)
cv2.waitKey(0)

Note that this is a simple example and there are many ways to modify and optimize this code for different use cases. Additionally, MediaPipe also provides other useful modules for face detection, face recognition, and other computer vision tasks.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply