0
0
Computer Visionml~5 mins

Face landmark detection in Computer Vision

Choose your learning style9 modes available
Introduction
Face landmark detection finds key points on a face, like eyes, nose, and mouth. It helps computers understand facial features easily.
To add fun filters on faces in a photo app, like sunglasses or hats.
To help a robot recognize emotions by looking at facial expressions.
To guide makeup apps that show how lipstick or blush would look.
To improve face recognition by focusing on important face parts.
To track face movements in video calls for better animations.
Syntax
Computer Vision
model = FaceLandmarkDetector()
landmarks = model.detect(image)
FaceLandmarkDetector is a placeholder for any face landmark detection model or library.
The detect method takes an image and returns points marking facial features.
Examples
Detect landmarks on a single image and print the points.
Computer Vision
landmarks = model.detect(image)
print(landmarks)
Detect landmarks on multiple faces and draw them on the image.
Computer Vision
for face in faces:
    landmarks = model.detect(face)
    draw_points(image, landmarks)
Sample Model
This code uses MediaPipe to detect face landmarks on a single image. It prints how many landmarks were found and shows the first five points with their normalized coordinates.
Computer Vision
import cv2
import mediapipe as mp

# Initialize MediaPipe Face Mesh
mp_face_mesh = mp.solutions.face_mesh
face_mesh = mp_face_mesh.FaceMesh(static_image_mode=True, max_num_faces=1)

# Load image
image = cv2.imread('face.jpg')

# Convert BGR to RGB
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Process image to find face landmarks
results = face_mesh.process(rgb_image)

if results.multi_face_landmarks:
    for face_landmarks in results.multi_face_landmarks:
        print(f'Number of landmarks detected: {len(face_landmarks.landmark)}')
        # Print first 5 landmark points (x, y normalized)
        for i, landmark in enumerate(face_landmarks.landmark[:5]):
            print(f'Landmark {i}: x={landmark.x:.3f}, y={landmark.y:.3f}')
else:
    print('No face landmarks detected.')
OutputSuccess
Important Notes
Face landmarks are usually given as points with x and y coordinates normalized between 0 and 1.
Different models detect different numbers of landmarks; MediaPipe Face Mesh detects 468 points.
Lighting and face angle can affect detection accuracy.
Summary
Face landmark detection finds key points on faces to understand facial features.
It is useful for apps like filters, emotion detection, and makeup simulation.
Popular tools like MediaPipe provide easy-to-use models with many landmarks.