Install & Compatibility
Where this runs
tested against v? · pip install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
build_error
glibcpy 3.10–3.95 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
dlib
✓ import dlib
The primary import for accessing all dlib functionalities.
get_frontal_face_detector
✓ detector = dlib.get_frontal_face_detector()
shape_predictor
✓ predictor = dlib.shape_predictor('model.dat')
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.")
Upgrade
Version history
20.0.1latest on PyPI · released Mar 29, 2026
Audit
Dependencies
setuptoolsrequiredRequired for building the package, typically handled by pip.