dlib - Machine Learning Toolkit
dlib is a modern C++ toolkit that provides a wide array of machine learning algorithms and tools, exposed via a Python API. It is widely used for computer vision tasks such as state-of-the-art facial recognition, object detection, and image processing, as well as general machine learning applications including SVMs, clustering, and regression. The library is actively maintained, with frequent patch releases and occasional minor or major version bumps, the current version being 20.0.1.
Warnings
- breaking Version 20.0 introduced several breaking changes. `dlib.fhog_object_detector` was moved to `dlib.image_processing.fhog_object_detector`. The `num_threads` argument was removed from `train_shape_predictor` and `train_object_detector`, which now use the global dlib thread pool configurable via `dlib.set_thread_pool_size()`.
- gotcha Installation via `pip install dlib` often requires system-level prerequisites for compilation (C++ compiler, CMake, Boost development libraries). Pre-built wheels are not available for all platforms/Python versions, leading to a source build.
- gotcha Pre-trained models (e.g., for facial landmarks, object detection) are not included in the pip package and must be downloaded manually from dlib's website (dlib.net/files) before use. Attempting to initialize predictors without the model file will result in runtime errors.
Install
-
pip install dlib
Imports
- dlib
import dlib
- get_frontal_face_detector
detector = dlib.get_frontal_face_detector()
- shape_predictor
predictor = dlib.shape_predictor('model.dat')
Quickstart
import dlib
import numpy as np # Used for dummy image
# --- Quickstart: Face Detection and Landmark Prediction API Usage ---
# Note: For this example to produce meaningful results, you need:
# 1. A pre-trained facial landmark predictor model file (e.g., 'shape_predictor_68_face_landmarks.dat').
# Download from: http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
# Then decompress it using 'bunzip2'.
# 2. An actual image file. Replace 'path/to/your/image.jpg' below.
# --- 1. Face Detection ---
# Initialize the frontal face detector
detector = dlib.get_frontal_face_detector()
# Load an image (replace with your actual image file path)
# For demonstration, we use a placeholder:
try:
img = dlib.load_rgb_image("path/to/your/image.jpg")
except RuntimeError:
print("Warning: Could not load dummy image 'path/to/your/image.jpg'.")
print("Using a blank NumPy array for demonstration purposes. Face detection will likely find nothing.")
img = np.zeros((400, 400, 3), dtype=np.uint8) # Create a 400x400 black image
# Detect faces in the image. The '1' argument means to upsample the image 1 time
# (make it larger) to find smaller faces.
dets = detector(img, 1)
print(f"Detected {len(dets)} faces (or regions of interest) in the image.")
# --- 2. Facial Landmark Prediction ---
# Initialize the shape predictor (requires a downloaded model file)
model_path = "shape_predictor_68_face_landmarks.dat" # Replace with actual path
try:
predictor = dlib.shape_predictor(model_path)
except dlib.set_level_error as e:
print(f"Error: Could not load shape predictor model from '{model_path}'.")
print("Please download 'shape_predictor_68_face_landmarks.dat' and provide the correct path.")
predictor = None # Prevent further errors if model loading failed
if predictor:
for i, d in enumerate(dets):
# Predict landmarks for each detected face
print(f" Processing face {i+1} at bounding box: Left: {d.left()}, Top: {d.top()}, Right: {d.right()}, Bottom: {d.bottom()}")
try:
shape = predictor(img, d)
print(f" Found {shape.num_parts} facial landmarks for face {i+1}.")
if shape.num_parts > 0:
print(f" Example: First landmark point: ({shape.part(0).x}, {shape.part(0).y})")
except Exception as e:
print(f" Could not find landmarks for face {i+1}. This is expected if the image is blank or model is incorrect. Error: {e}")
else:
print("Skipping facial landmark prediction due to missing or invalid model.")
print("\ndlib quickstart example finished.")