A modern C++ toolkit containing machine learning algorithms and tools for creating complex software to solve real world problems
—
Comprehensive face detection, landmark prediction, and face recognition capabilities using state-of-the-art deep learning models and traditional computer vision approaches.
Built-in face detection using HOG-based and CNN-based approaches for robust face localization in images.
def get_frontal_face_detector():
"""
Get default HOG-based frontal face detector.
Returns:
Face detector object for finding frontal faces
"""
class cnn_face_detection_model_v1:
"""CNN-based face detector for improved accuracy."""
def __init__(self, model_filename: str):
"""
Initialize CNN face detector.
Args:
model_filename: Path to CNN face detection model file
"""
def __call__(self, img, upsample_num_times: int = 0):
"""
Detect faces in image using CNN.
Args:
img: Input image
upsample_num_times: Number of times to upsample image for detection
Returns:
List of face detections with confidence scores
"""
class mmod_rectangle:
"""Face detection result with confidence score."""
@property
def rect(self) -> rectangle:
"""Bounding rectangle of detected face."""
@property
def confidence(self) -> float:
"""Detection confidence score."""Usage Example:
import dlib
import cv2
# Load image
img = cv2.imread("group_photo.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# HOG-based face detection
detector = dlib.get_frontal_face_detector()
faces = detector(gray)
print(f"Found {len(faces)} faces")
for i, face in enumerate(faces):
print(f"Face {i}: {face}")
# CNN-based face detection (requires model file)
cnn_detector = dlib.cnn_face_detection_model_v1("mmod_human_face_detector.dat")
cnn_faces = cnn_detector(img, upsample_num_times=1)
for detection in cnn_faces:
face_rect = detection.rect
confidence = detection.confidence
print(f"CNN face: {face_rect}, confidence: {confidence}")Precise facial landmark localization for face alignment and feature extraction.
class shape_predictor:
"""Facial landmark detector."""
def __init__(self, predictor_path: str):
"""
Initialize shape predictor.
Args:
predictor_path: Path to trained shape predictor model
"""
def __call__(self, img, face_rect: rectangle):
"""
Predict facial landmarks for detected face.
Args:
img: Input image
face_rect: Rectangle containing detected face
Returns:
Shape object containing facial landmark points
"""
class full_object_detection:
"""Facial landmarks representation."""
def num_parts(self) -> int:
"""Get number of landmark points."""
def part(self, index: int) -> point:
"""Get specific landmark point by index."""
@property
def rect(self) -> rectangle:
"""Bounding rectangle of the detection."""
class full_object_detections:
"""Array of full_object_detection objects."""
def clear(self):
"""Remove all detections."""
def resize(self, size: int):
"""Resize container."""
def extend(self, detection_list: list):
"""Add detections from list."""Usage Example:
import dlib
import cv2
# Load image and detect faces
img = cv2.imread("face.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
faces = detector(gray)
for face in faces:
# Get facial landmarks
landmarks = predictor(gray, face)
print(f"Found {landmarks.num_parts()} landmarks")
# Access specific landmarks
nose_tip = landmarks.part(30) # Nose tip
left_eye = landmarks.part(36) # Left eye corner
right_eye = landmarks.part(45) # Right eye corner
print(f"Nose tip: {nose_tip}")
print(f"Eyes: {left_eye}, {right_eye}")
# Draw landmarks
for i in range(landmarks.num_parts()):
point = landmarks.part(i)
cv2.circle(img, (point.x, point.y), 2, (0, 255, 0), -1)
cv2.imshow("Landmarks", img)
cv2.waitKey(0)Deep learning-based face recognition for generating face encodings and computing face similarity.
class face_recognition_model_v1:
"""Face recognition neural network."""
def __init__(self, model_filename: str):
"""
Initialize face recognition model.
Args:
model_filename: Path to face recognition model file
"""
def compute_face_descriptor(
self,
img,
face: full_object_detection,
num_jitters: int = 0,
padding: float = 0.25
):
"""
Compute face descriptor from aligned face.
Args:
img: Input image
face: Facial landmarks from shape predictor
num_jitters: Number of random jitters for robust encoding
padding: Padding around face for extraction
Returns:
128-dimensional face descriptor vector
"""
def compute_face_descriptor_from_aligned_image(
self,
aligned_img,
num_jitters: int = 0
):
"""
Compute face descriptor from pre-aligned 150x150 face image.
Args:
aligned_img: Pre-aligned face image (150x150 pixels)
num_jitters: Number of random jitters for robust encoding
Returns:
128-dimensional face descriptor vector
"""
def compute_face_descriptors(
self,
img,
faces: full_object_detections,
num_jitters: int = 0,
padding: float = 0.25
):
"""
Compute face descriptors for multiple faces in single image.
Args:
img: Input image
faces: Multiple facial landmarks
num_jitters: Number of random jitters
padding: Padding around faces
Returns:
List of 128-dimensional face descriptor vectors
"""
def batch_compute_face_descriptors(
self,
batch_imgs: list,
batch_faces: list,
num_jitters: int = 0,
padding: float = 0.25
):
"""
Batch processing of face descriptors for multiple images.
Args:
batch_imgs: List of input images
batch_faces: List of lists of facial landmarks (one list per image)
num_jitters: Number of random jitters
padding: Padding around faces
Returns:
List of lists of face descriptor vectors (one list per image)
"""
def batch_compute_face_descriptors_from_aligned_images(
self,
batch_aligned_imgs: list,
num_jitters: int = 0
):
"""
Batch processing of face descriptors from pre-aligned images.
Args:
batch_aligned_imgs: List of pre-aligned face images (150x150 each)
num_jitters: Number of random jitters
Returns:
List of 128-dimensional face descriptor vectors
"""Usage Example:
import dlib
import cv2
import numpy as np
# Initialize models
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
face_rec = dlib.face_recognition_model_v1("dlib_face_recognition_resnet_model_v1.dat")
# Load reference image
ref_img = cv2.imread("reference_face.jpg")
ref_gray = cv2.cvtColor(ref_img, cv2.COLOR_BGR2GRAY)
# Get reference face encoding
ref_faces = detector(ref_gray)
if len(ref_faces) > 0:
ref_landmarks = predictor(ref_gray, ref_faces[0])
ref_encoding = face_rec.compute_face_descriptor(ref_img, ref_landmarks)
# Load test image
test_img = cv2.imread("test_face.jpg")
test_gray = cv2.cvtColor(test_img, cv2.COLOR_BGR2GRAY)
# Get test face encoding
test_faces = detector(test_gray)
if len(test_faces) > 0:
test_landmarks = predictor(test_gray, test_faces[0])
test_encoding = face_rec.compute_face_descriptor(test_img, test_landmarks)
# Compute similarity (Euclidean distance)
distance = np.linalg.norm(np.array(ref_encoding) - np.array(test_encoding))
# Typically, distance < 0.6 indicates same person
if distance < 0.6:
print(f"Same person (distance: {distance:.3f})")
else:
print(f"Different person (distance: {distance:.3f})")Algorithms for grouping faces by identity using unsupervised clustering approaches.
def chinese_whispers_clustering(descriptors: list, threshold: float) -> list:
"""
Cluster face descriptors using Chinese Whispers algorithm.
Args:
descriptors: List of face descriptor vectors
threshold: Distance threshold for clustering
Returns:
List of cluster labels for each descriptor
"""
def chinese_whispers(edges: list) -> list:
"""
Direct Chinese Whispers clustering on graph edges.
Args:
edges: List of graph edges (node pairs)
Returns:
List of cluster labels
"""
def bottom_up_clustering(
descriptors: list,
min_num_clusters: int = 1,
max_dist: float = 0.6
) -> list:
"""
Hierarchical bottom-up clustering of face descriptors.
Args:
descriptors: List of face descriptor vectors
min_num_clusters: Minimum number of clusters
max_dist: Maximum distance for merging clusters
Returns:
List of cluster labels for each descriptor
"""Usage Example:
import dlib
import cv2
import numpy as np
from glob import glob
# Initialize models
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
face_rec = dlib.face_recognition_model_v1("dlib_face_recognition_resnet_model_v1.dat")
# Process multiple images to get face descriptors
descriptors = []
image_files = glob("faces/*.jpg")
for img_path in image_files:
img = cv2.imread(img_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = detector(gray)
for face in faces:
landmarks = predictor(gray, face)
descriptor = face_rec.compute_face_descriptor(img, landmarks)
descriptors.append(descriptor)
# Cluster faces
print(f"Clustering {len(descriptors)} face descriptors...")
# Chinese Whispers clustering
cw_labels = dlib.chinese_whispers_clustering(descriptors, threshold=0.5)
print(f"Chinese Whispers found {len(set(cw_labels))} clusters")
# Bottom-up clustering
bu_labels = dlib.bottom_up_clustering(descriptors, min_num_clusters=2, max_dist=0.6)
print(f"Bottom-up clustering found {len(set(bu_labels))} clusters")
# Group images by cluster
for cluster_id in set(cw_labels):
cluster_indices = [i for i, label in enumerate(cw_labels) if label == cluster_id]
print(f"Cluster {cluster_id}: {len(cluster_indices)} faces")Utilities for extracting and saving aligned face chips for analysis and recognition.
def save_face_chip(
img,
face: full_object_detection,
filename: str,
size: int = 150,
padding: float = 0.25
):
"""
Save aligned face chip to file.
Args:
img: Input image
face: Facial landmarks
filename: Output filename
size: Chip size in pixels
padding: Padding around face
"""
def save_face_chips(
img,
faces: full_object_detections,
filename_prefix: str,
size: int = 150,
padding: float = 0.25
):
"""
Save multiple aligned face chips to files.
Args:
img: Input image
faces: Multiple facial landmarks
filename_prefix: Prefix for output filenames
size: Chip size in pixels
padding: Padding around faces
"""
def get_face_chip(
img,
face: full_object_detection,
size: int = 150,
padding: float = 0.25
):
"""
Extract aligned face chip as image array.
Args:
img: Input image
face: Facial landmarks
size: Chip size in pixels
padding: Padding around face
Returns:
Aligned face chip image
"""
def get_face_chips(
img,
faces: full_object_detections,
size: int = 150,
padding: float = 0.25
) -> list:
"""
Extract multiple aligned face chips.
Args:
img: Input image
faces: Multiple facial landmarks
size: Chip size in pixels
padding: Padding around faces
Returns:
List of aligned face chip images
"""Usage Example:
import dlib
import cv2
# Initialize models
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
# Load image
img = cv2.imread("group_photo.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect faces and landmarks
faces = detector(gray)
landmarks_list = dlib.full_object_detections()
for face in faces:
landmarks = predictor(gray, face)
landmarks_list.extend([landmarks])
# Save individual face chips
for i, landmarks in enumerate(landmarks_list):
dlib.save_face_chip(img, landmarks, f"face_chip_{i}.jpg", size=200, padding=0.3)
# Extract face chips for processing
face_chips = dlib.get_face_chips(img, landmarks_list, size=150)
print(f"Extracted {len(face_chips)} face chips")
for i, chip in enumerate(face_chips):
cv2.imshow(f"Face Chip {i}", chip)
cv2.waitKey(0)
cv2.destroyAllWindows()Training custom facial landmark detectors for specialized applications.
class shape_predictor_training_options:
"""Training configuration for shape predictors."""
tree_depth: int # Depth of regression trees
num_trees_per_cascade_level: int # Trees per cascade level
nu: float # Regularization parameter
oversampling_amount: int # Data augmentation factor
feature_pool_size: int # Feature pool size
lambda_param: float # Lambda regularization
num_test_splits: int # Test splits per node
feature_pool_region_padding: float # Padding for feature region
verbose: bool # Verbose training output
def train_shape_predictor(
dataset_filename: str,
predictor_output_filename: str,
options: shape_predictor_training_options
):
"""
Train custom shape predictor from XML dataset.
Args:
dataset_filename: Path to XML training dataset
predictor_output_filename: Output path for trained model
options: Training configuration options
"""Complete Face Recognition Pipeline Example:
import dlib
import cv2
import numpy as np
def recognize_faces_in_image(image_path, known_encodings, known_names, tolerance=0.6):
"""Complete face recognition pipeline."""
# Initialize models
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
face_rec = dlib.face_recognition_model_v1("dlib_face_recognition_resnet_model_v1.dat")
# Load and process image
img = cv2.imread(image_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect faces
faces = detector(gray)
results = []
for face in faces:
# Get landmarks and encoding
landmarks = predictor(gray, face)
encoding = face_rec.compute_face_descriptor(img, landmarks)
# Compare with known faces
distances = [np.linalg.norm(np.array(encoding) - np.array(known_enc))
for known_enc in known_encodings]
if min(distances) < tolerance:
name = known_names[distances.index(min(distances))]
confidence = 1 - min(distances)
else:
name = "Unknown"
confidence = 0
results.append({
'face_rect': face,
'landmarks': landmarks,
'name': name,
'confidence': confidence
})
return results
# Usage
known_encodings = [] # Load your known face encodings
known_names = [] # Corresponding names
results = recognize_faces_in_image("test.jpg", known_encodings, known_names)
for result in results:
print(f"Found {result['name']} with confidence {result['confidence']:.3f}")Efficient processing of multiple images and faces using batch operations for improved performance.
Batch Face Descriptor Computation:
import dlib
import cv2
import numpy as np
def batch_face_processing_demo():
"""Demonstrate efficient batch processing of face descriptors."""
# Initialize models
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
face_rec = dlib.face_recognition_model_v1("dlib_face_recognition_resnet_model_v1.dat")
# Simulate multiple images with faces
image_paths = ["photo1.jpg", "photo2.jpg", "photo3.jpg"]
batch_images = []
batch_faces_list = []
# Process each image to detect faces and landmarks
for img_path in image_paths:
img = dlib.load_rgb_image(img_path)
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
# Detect faces
faces = detector(gray)
if len(faces) > 0:
# Get landmarks for all faces in this image
landmarks_list = dlib.full_object_detections()
for face in faces:
landmarks = predictor(gray, face)
landmarks_list.extend([landmarks])
batch_images.append(img)
batch_faces_list.append(landmarks_list)
# Batch compute face descriptors (much more efficient than individual calls)
print(f"Computing descriptors for {len(batch_images)} images...")
batch_descriptors = face_rec.batch_compute_face_descriptors(
batch_images,
batch_faces_list,
num_jitters=1,
padding=0.25
)
# Process results - batch_descriptors[i] contains descriptors for all faces in image i
for i, (img_path, descriptors) in enumerate(zip(image_paths, batch_descriptors)):
print(f"{img_path}: {len(descriptors)} faces, {len(descriptors[0])}D descriptors")
return batch_descriptors
# Usage
descriptors = batch_face_processing_demo()Batch Processing with Pre-aligned Face Chips:
import dlib
import numpy as np
def process_aligned_face_chips():
"""Process batch of pre-aligned 150x150 face chips."""
face_rec = dlib.face_recognition_model_v1("dlib_face_recognition_resnet_model_v1.dat")
# Load pre-aligned face chips (must be 150x150 pixels)
aligned_faces = []
for i in range(10):
try:
face_chip = dlib.load_rgb_image(f"aligned_faces/face_{i:03d}.jpg")
# Ensure proper size for aligned processing
if face_chip.shape[:2] == (150, 150):
aligned_faces.append(face_chip)
except:
continue
# Batch process aligned images (fastest method for pre-aligned faces)
descriptors = face_rec.batch_compute_face_descriptors_from_aligned_images(
aligned_faces,
num_jitters=2 # Higher jitters for more robust descriptors
)
print(f"Processed {len(aligned_faces)} pre-aligned faces")
print(f"Generated {len(descriptors)} descriptors of {len(descriptors[0])} dimensions each")
# Example: Compute pairwise similarities
similarities = []
for i in range(len(descriptors)):
for j in range(i+1, len(descriptors)):
distance = np.linalg.norm(np.array(descriptors[i]) - np.array(descriptors[j]))
similarity = 1.0 / (1.0 + distance)
similarities.append((i, j, similarity))
# Find most similar pair
most_similar = max(similarities, key=lambda x: x[2])
print(f"Most similar faces: {most_similar[0]} and {most_similar[1]} (similarity: {most_similar[2]:.3f})")
return descriptors
# Usage
aligned_descriptors = process_aligned_face_chips()Install with Tessl CLI
npx tessl i tessl/pypi-dlib