dlib - Machine Learning Toolkit

20.0.1 · active · verified Mon Apr 13

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

Install

Imports

Quickstart

Demonstrates how to use dlib for frontal face detection and facial landmark prediction. Note that for meaningful results, you must provide a real image and download a pre-trained shape predictor model file separately, as these are not bundled with the pip package. The example includes error handling for missing files.

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.")

view raw JSON →