Viola Jones Face Detector - iffatAGheyas/computer-vision-handbook GitHub Wiki

😊 Viola–Jones Face Detection Algorithm

The Viola–Jones algorithm is a real-time object detection framework, best known for face detection. Introduced by Paul Viola and Michael Jones in 2001, it was the first practical face detector for consumer devices (webcams, digital cameras).


🔑 Key Features

Feature Description
Real-time Extremely fast, even on CPU-only systems
Face-specific Trained primarily on frontal human faces
Lightweight Doesn’t require GPUs or deep learning
Haar-like Features Uses simple rectangle-based features (light/dark region differences)
Cascade Classifier Boosting-based cascade of weak classifiers to rapidly reject non-face windows

🛠 How It Works (4 Steps)

Step Description
1. Haar Features Detect light/dark patterns such as eye sockets and cheeks
2. Integral Image Speeds up rectangle-sum computations across the window
3. AdaBoost Selection Selects the most discriminatory features from thousands
4. Cascade Classification Applies successive stages of classifiers to quickly eliminate non-faces

🐍 Step-by-Step Code: Haar Cascade Face Detection

import cv2
import matplotlib.pyplot as plt

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

# Load OpenCV’s pre-trained face detector
face_cascade = cv2.CascadeClassifier(
    cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
)

# Detect faces
faces = face_cascade.detectMultiScale(
    gray,
    scaleFactor=1.1,
    minNeighbors=5,
    minSize=(30, 30),
    flags=cv2.CASCADE_SCALE_IMAGE
)

# Draw rectangles around detected faces
for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)

# Show result
plt.figure(figsize=(8, 8))
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.title("Viola–Jones Face Detection")
plt.axis("off")
plt.show()

image

🔍 detectMultiScale Parameters

Parameter Meaning
scaleFactor How much the image size is reduced at each image scale (e.g. 1.1 = 10% smaller per layer)
minNeighbors Minimum number of overlapping rectangles required to retain a detection (higher → fewer false positives)
minSize Minimum possible object size (smaller → detects smaller faces but increases noise)
flags Detection mode (typically cv2.CASCADE_SCALE_IMAGE, OpenCV’s internal resizing strategy)

Summary

Feature Value
Uses Haar-like features Detects structural patterns (e.g. eyes, cheeks)
Performance Extremely fast; suitable for real-time applications
Generalizability Not limited to faces—OpenCV provides cascades for eyes, smiles, full bodies, etc.
Common Applications Webcams, surveillance, facial region extraction

⚠️ Limitations

Issue Description
Lighting Sensitivity Performs poorly in low light or under strong shadows
Pose Variation Struggles with side views or rotated faces
Lower Accuracy vs Deep Models May miss small, occluded or non-standard face appearances