Skip to main content

Prediction of 3d facemarks on a 3d face mesh.

Project description

3D Facial Landmarks Detection

LicenseFramework

A Python package for detecting and analyzing 3D facial landmarks (facemarks) from mesh data. This package provides a complete pipeline for importing 3D facial meshes, predicting facial landmark positions in 3D space, and visualizing results.

Features

  • Mesh Import: Import 3D facial meshes with texture support via Open3D
  • 3D Landmark Prediction: Detect 478 facial landmarks in 3D space using multi-view projection and raycasting
  • JSON Export: Save predicted facemarks with normalized coordinates and closest vertex indices
  • Visualization: Render meshes with detected landmarks overlaid
  • Robust Detection: Uses multiple camera projections for accurate 3D reconstruction

Requirements

  • Python 3.11
  • MediaPipe Face Landmarker model (in working directory)
    wget https://storage.googleapis.com/mediapipe-models/face_landmarker/face_landmarker/float16/latest/face_landmarker.task
    

Installation

Install the package via pip:

pip install facemarks

Quick Start

from facemarks import (
    import_mesh_and_setup,
    predict,
    save_facemarks_json,
    render_result
)

# Import and setup your 3D mesh
meshes = import_mesh_and_setup("path/to/mesh.obj")

# Predict 3D facial landmarks
prediction_result = predict(meshes, projections=100)

# Extract results
facemarks_3d = result["facemarks_3d"]
closest_vertex_ids = result["closest_vertex_ids"]

# or Save results directly to JSON
save_facemarks_json(
    "path/to/mesh.obj",
    prediction_result,
    "output/facemarks.json"
)

# Visualize the results
render_result(meshes["textured"], facemarks_3d)

API Reference

import_mesh_and_setup(filename)

Imports a 3D facial mesh and prepares it for landmark detection. Supports textured OBJ files.

Parameters:

  • filename (str): Path to the mesh file (.obj format)

Returns:

  • dict: Dictionary containing mesh data with keys:
    • "original": The base mesh geometry
    • "textured": Textured mesh for rendering
    • "tensor": Tensor representation for raycasting

Example:

meshes = import_mesh_and_setup("face_model.obj")

predict(meshes, projections=100)

Predicts 3D facial landmarks from the provided mesh data using multi-view projection and raycasting. Detects 478 facial landmarks in 3D space.

Parameters:

  • meshes (dict): Mesh data object returned from import_mesh_and_setup()
  • projections (int, optional): Number of camera projections to use for landmark detection. Default is 100. More projections increase accuracy but take longer.

Returns:

  • dict: Dictionary containing:
    • "facemarks_3d" (list): List of 3D coordinates for detected facial landmarks
    • "closest_vertex_ids" (list): Indices of the closest mesh vertices to each landmark

Example:

result = predict(meshes, projections=150)
landmarks = result["facemarks_3d"]
vertex_ids = result["closest_vertex_ids"]

Note: The function uses random camera rotations for robust multi-view detection. If fewer than half of the requested projections successfully detect a face, an error message is printed.


save_facemarks_json(input_path, prediction_result, json_path)

Exports predicted facial landmarks to a JSON file with normalized coordinates and vertex mapping.

Parameters:

  • input_path (str): Path to the original input mesh file
  • prediction_result (dict): Prediction data object returned from predict()
  • json_path (str): Destination path for the output JSON file

Output JSON Structure:

{
    "model": "path/to/mesh.obj",
    "normalized coordinates": [[x, y, z], ...],
    "closest vertex indexes": [idx1, idx2, ...]
}

Example:

save_facemarks_json(
    "input/face.obj",
    prediction_result,
    "output/landmarks.json"
)

render_result(mesh, facemarks)

Visualizes the mesh with predicted facial landmarks overlaid as magenta points.

Parameters:

  • mesh: The mesh geometry to be displayed (either from meshes["original"] or meshes["textured"])
  • facemarks (list): List of 3D landmark coordinates to visualize

Example:

render_result(meshes["original"], result["facemarks_3d"])

Note: This function requires a display environment. It will not work in headless environments with virtual displays.

How It Works

The package uses a sophisticated multi-view approach to detect 3D facial landmarks:

  1. Multi-View Projection: The mesh is rendered from multiple random camera angles (default: 100 views)
  2. 2D Landmark Detection: MediaPipe Face Landmark detection is applied to each rendered view
  3. Ray Casting: Rays are cast from camera positions through detected 2D landmarks onto the 3D mesh
  4. 3D Reconstruction: The intersection points are aggregated across all views to compute robust 3D landmark positions
  5. Vertex Mapping: Each landmark is mapped to the closest vertex on the original mesh

Use Cases

  • Facial animation and rigging
  • 3D face model analysis
  • Biometric applications
  • Character modeling pipelines
  • Medical and dental visualization
  • Motion capture alignment

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Support

For issues, questions, or contributions, please visit the GitHub repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

facemarks-0.1.2.tar.gz (14.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

facemarks-0.1.2-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

File details

Details for the file facemarks-0.1.2.tar.gz.

File metadata

  • Download URL: facemarks-0.1.2.tar.gz
  • Upload date:
  • Size: 14.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.11

File hashes

Hashes for facemarks-0.1.2.tar.gz
Algorithm Hash digest
SHA256 a17f5def94ff130e2adb86a28058d2277778daa18e4da168a5160b1da1b3408c
MD5 d3552d38543ce3859598c830196748be
BLAKE2b-256 cfa192d95f6c07a4f14c822b6ab3dd985002e2d9c40838c62576a09062f4b93d

See more details on using hashes here.

File details

Details for the file facemarks-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: facemarks-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 13.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.11

File hashes

Hashes for facemarks-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cc1a3dc205ff456551c68d59b84f76414a0143ee8d635bbeb0a0cb18662590c4
MD5 33a02c7f8fd3bc517698848a637d5c5c
BLAKE2b-256 2774737f92b4c22fbc83d4fad17d3ca5da9fc8945bb71206057f5719c404680d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page