Function Signature
Overview
Extracts 2D facial landmarks using OpenCV’s Haar Cascade detector and FacemarkLBF algorithm. This function detects faces in an image and returns 68 normalized landmark points as a flat list of coordinates. The function is located inutils.py:59 and uses the following models:
- Haar Cascade for face detection
- FacemarkLBF for extracting 68 facial landmark points
Parameters
Input image in BGR color format (as read by OpenCV). Must be a 3-channel image with shape
(height, width, 3).If
True, draws green circles on the detected landmark points directly on the input image. Useful for visualization during development.Included for API compatibility. Currently not used in the implementation but reserved for future enhancements.
Return Value
Returns a flat list of normalized coordinates in the format:
- Length: 136 elements (68 points × 2 coordinates)
- Normalization: Coordinates are normalized relative to the face bounding box, ranging from 0.0 to 1.0
- Empty list: Returns
[]if no face is detected or if the input image is invalid
How It Works
- Validation: Checks if the input image is valid (3-channel BGR image)
- Grayscale conversion: Converts the image to grayscale for detection
- Face detection: Uses Haar Cascade to detect faces
- Landmark extraction: Applies FacemarkLBF to extract 68 landmark points
- Normalization: Normalizes coordinates relative to the face bounding box
- Optional drawing: If
draw=True, visualizes landmarks on the image
Code Examples
Basic Usage
With Visualization
Processing Video Frames
Model Auto-Download
The function automatically downloads required models on first use:haarcascade_frontalface_default.xml(Haar Cascade)lbfmodel.yaml(FacemarkLBF model)
utils.py.
Error Handling
Invalid Image
No Face Detected
Missing OpenCV Contrib Module
Dependencies
opencv-contrib-python(required forcv2.facemodule)numpy(for array operations)
Performance Notes
- First call: Slower due to model loading and potential downloads
- Subsequent calls: Fast, models are cached in memory
- Single face: Only the first detected face is processed
- Detection parameters:
scaleFactor=1.1,minNeighbors=5for Haar Cascade

