Artificial Intelligence 4 min read

Extracting 3D Face Mesh Points with MediaPipe in Python

This article demonstrates how to use MediaPipe's Python API to extract 468 3D facial landmark points, configure detection parameters, and visualize the results with OpenCV, providing code examples and references to related research papers.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Extracting 3D Face Mesh Points with MediaPipe in Python

This article introduces a method for extracting 3D facial point cloud data using MediaPipe, yielding 468 facial landmarks whose coordinates range from 0 to 1, with the z‑axis representing depth.

Relevant research papers can be found at https://arxiv.org/pdf/1907.06724.pdf and https://arxiv.org/pdf/2006.10962.pdf .

The overall workflow is illustrated by three diagrams:

The extracted 468 points are 3D coordinates with values between 0 and 1; the z value indicates depth. The Python function supports the following parameters:

max_num_faces (default 1) – maximum number of faces to detect.

min_detection_confidence (default 0.5) – minimum detection confidence.

min_tracking_confidence (default 0.5) – minimum tracking confidence.

Below is a modified Python script based on the official MediaPipe tutorial that captures video, processes each frame, and draws the face mesh:

<code>import cv2
import mediapipe as mp
mp_drawing = mp.solutions.drawing_utils
mp_face_mesh = mp.solutions.face_mesh

drawing_spec = mp_drawing.DrawingSpec(thickness=1, circle_radius=1)
cap = cv2.VideoCapture("D:/images/video/face_mesh.mp4")
with mp_face_mesh.FaceMesh(
    max_num_faces=4,
    min_detection_confidence=0.5,
    min_tracking_confidence=0.5) as face_mesh:
  while cap.isOpened():
    success, frame = cap.read()
    if not success:
      print("Ignoring empty camera frame.")
      break
    h, w, c = frame.shape
    image = cv2.resize(frame, (w // 2, h // 2))
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    results = face_mesh.process(image)
    image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
    if results.multi_face_landmarks:
      for face_landmarks in results.multi_face_landmarks:
        mp_drawing.draw_landmarks(image=image, landmark_list=face_landmarks)
    cv2.imshow('MediaPipe Face Mesh', image)
    if cv2.waitKey(5) & 0xFF == 27:
      break
cap.release()</code>

The execution result is shown in the following image:

*Disclaimer: This article is compiled from online sources; copyright belongs to the original authors. Please contact us for removal or licensing if any rights are infringed.

Computer VisionMediaPipe3D point cloudFaceMesh
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.