Skip to content
Home » Using dlib in Python: A Full Guide to Face Detection and More

Using dlib in Python: A Full Guide to Face Detection and More

  • by

dlib is a powerful toolkit for machine learning and computer vision in C++ and Python. It’s widely known for its facial recognition, landmark detection, and object tracking capabilities.

In this tutorial, we’ll explore how to use dlib in Python for:

  • Face detection
  • Face landmark extraction (68-point model)
  • Face recognition
  • Object detection basics

We’ll also compare dlib with OpenCV and see when and why to use each.


What is dlib?

dlib is a C++ toolkit with Python bindings, used for:

  • Image processing
  • Machine learning
  • Deep learning
  • Facial analysis and biometric systems

It includes pretrained models for face detection, landmarks, and face embeddings that are highly accurate and production-ready.


Installing dlib

Step 1: Install prerequisites

For Windows:

pip install cmake

For Linux/macOS:

sudo apt install cmake

Step 2: Install dlib

pip install dlib

If this fails, try:

pip install dlib‑19.x.x‑cp39‑cp39‑win_amd64.whl  # from unofficial binaries

Also install supporting libraries:

pip install numpy opencv-python

Detecting Faces with dlib

import dlib
import cv2

# Load detector
detector = dlib.get_frontal_face_detector()

# Load image and convert to grayscale
img = cv2.imread("person.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Detect faces
faces = detector(gray)

for face in faces:
x, y, w, h = face.left(), face.top(), face.width(), face.height()
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)

cv2.imshow("Face Detection", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Facial Landmark Detection (68 points)

Download the pre-trained model:
shape_predictor_68_face_landmarks.dat

import dlib
import cv2

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

img = cv2.imread("face.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

faces = detector(gray)

for face in faces:
landmarks = predictor(gray, face)
for n in range(0, 68):
x = landmarks.part(n).x
y = landmarks.part(n).y
cv2.circle(img, (x, y), 2, (255, 0, 0), -1)

cv2.imshow("Landmarks", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

The 68 points include the jawline, eyebrows, eyes, nose, and lips.


Face Embeddings for Recognition

face_rec_model_path = "dlib_face_recognition_resnet_model_v1.dat"

face_rec = dlib.face_recognition_model_v1(face_rec_model_path)

Full recognition pipeline:

  1. Detect face → dlib.get_frontal_face_detector()
  2. Get landmarks → shape_predictor
  3. Get 128D face encoding → face_recognition_model_v1
img = cv2.imread("person.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

faces = detector(gray)
for face in faces:
landmarks = predictor(gray, face)
encoding = face_rec.compute_face_descriptor(img, landmarks)
print(list(encoding)) # This 128D vector can be used to compare faces

You can then compare encodings using Euclidean distance:

from scipy.spatial import distance

dist = distance.euclidean(encoding1, encoding2)
if dist < 0.6:
print("Same person")
else:
print("Different people")

Object Detection with HOG + SVM

dlib uses HOG (Histogram of Oriented Gradients) + SVM for face detection. You can also train it to detect custom objects using dlib.simple_object_detector_training_options() and annotated images.

But for most tasks, pretrained models are sufficient.


dlib vs. OpenCV vs. face_recognition

FeaturedlibOpenCVface_recognition
Accuracy✅ High❌ Lower for face tasks✅ High (built on dlib)
Performance⚠️ Slower⚡ Fast⚠️ Slower
Face Embeddings✅ Yes (128D)❌ No✅ Yes
Landmark Detection✅ 5, 68 points✅ 6–68 points (less accurate)
Ease of Use🟡 Medium🟢 Easy🟢 Very Easy
Custom Models✅ Trainable⚠️ Limited❌ No training support

Real-World Use Cases of dlib

  1. Facial attendance system
  2. Biometric authentication
  3. Virtual makeup/filters
  4. Emotion detection (using landmarks)
  5. Object tracking in surveillance

Common Errors & Fixes

ErrorSolution
shape_predictor_68_face_landmarks.dat not foundDownload from dlib GitHub
no module named dlibInstall with pip install dlib
Installation failure on WindowsUse pre-built wheels from PyPi unofficial binaries
SlownessResize image before detection

Conclusion

The dlib library offers everything from basic face detection to advanced facial embeddings and recognition. If you’re building a secure, accurate, and production-ready facial recognition system, dlib is a rock-solid foundation.

With just a few dozen lines of code, you’ve learned how to:

  • Detect faces and extract features
  • Recognize faces using deep learning
  • Use facial landmarks for intelligent applications

Leave a Reply

Your email address will not be published. Required fields are marked *