source/data/generate_mediapipe_face_symmetry_map.py
source-code data mediapipe augmentation face-mesh
File Path: src/data/generate_mediapipe_face_symmetry_map.py
Purpose: Generates a symmetry mapping array for MediaPipe Face Mesh landmarks to enable correct horizontal flipping.
Overview
MediaPipe’s 478 face landmarks are not perfectly symmetrical by index (e.g., left eye point index is not simply right eye point index + offset). This script calculates the corresponding “mirror” index for every landmark by analyzing a frontal face image.
Key Functions
gen_symmetry_map(face_model, image_path)
Calculates the symmetry indices.
- Detect: extract face landmarks from a reference image.
- Center: Centralize points around the mean.
- Flip: Create a horizontally flipped copy of the points.
- Match: Use linear sum assignment (Hungarian Algorithm) to find the closest matching point in the original set for each point in the flipped set.
- Validate: Perform sanity checks on known pairs (e.g., eye corners) to ensure mapping correctness.
Returns: NumPy array where arr[i] is the symmetric index of point i.
get_face_mesh_symmetry_indices(face_model, image_path)
Helper coroutine that performs the actual geometric matching logic.
Usage
Generated file face_symmetry_map.npy is used during data augmentation to properly flip face landmarks.
Related Documentation
- data_preparation.py - Uses the symmetry map for
hflip. - mediapipe_utils.py - MediaPipe constants.
File Location: src/data/generate_mediapipe_face_symmetry_map.py