Face Detection Mini Project
Build a complete face detection application that processes both images and video.
15 min•By Priygop Team•Updated 2026
Face Detection Application
Face Detection Application
import cv2
import numpy as np
import os
class FaceDetector:
"""A complete face and eye detector with configurable sensitivity."""
def __init__(self, scale_factor=1.1, min_neighbors=5, min_size=(30, 30)):
self.face_cascade = cv2.CascadeClassifier(
cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
)
self.eye_cascade = cv2.CascadeClassifier(
cv2.data.haarcascades + "haarcascade_eye.xml"
)
self.scale_factor = scale_factor
self.min_neighbors = min_neighbors
self.min_size = min_size
def detect(self, frame):
"""Detect faces in a single frame. Returns list of face dicts."""
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.equalizeHist(gray) # Improve contrast
faces_raw = self.face_cascade.detectMultiScale(
gray,
scaleFactor=self.scale_factor,
minNeighbors=self.min_neighbors,
minSize=self.min_size
)
results = []
for (x, y, w, h) in faces_raw:
# Detect eyes within face
face_gray = gray[y:y+h, x:x+w]
eyes = self.eye_cascade.detectMultiScale(face_gray)
results.append({
"bbox": (x, y, w, h),
"eyes": len(eyes),
"centre": (x + w//2, y + h//2),
"area": w * h,
})
return results
def draw(self, frame, detections):
"""Draw detections on a frame."""
result = frame.copy()
for d in detections:
x, y, w, h = d["bbox"]
cv2.rectangle(result, (x,y), (x+w,y+h), (0,255,0), 2)
label = f"eyes:{d['eyes']}"
cv2.putText(result, label, (x, y-5),
cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0,255,0), 1)
count_text = f"Faces: {len(detections)}"
cv2.putText(result, count_text, (10,25),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0,255,0), 2)
return result
def process_image(self, image_path, output_path=None):
"""Process a single image."""
image = cv2.imread(image_path)
if image is None:
return 0
detections = self.detect(image)
result = self.draw(image, detections)
out = output_path or image_path.replace(".", "_detected.")
cv2.imwrite(out, result)
print(f"Detected {len(detections)} face(s). Saved: {out}")
return len(detections)
# Use the detector
detector = FaceDetector(scale_factor=1.1, min_neighbors=5)
detector.process_image("photo.jpg")Diagram
Loading diagram…
Machine Learning follows a structured pipeline from data to deployment
Key Takeaways from Module 8
- Face detection locates faces in images (bounding boxes). Face recognition identifies who the person is — a separate, harder task
- Haar cascades are classical CV detectors — fast and built into OpenCV via CascadeClassifier
- Use detectMultiScale() with tuned scaleFactor and minNeighbors for the right sensitivity
- Always convert to grayscale and equalise the histogram before detection
- For video: resize frames to a smaller resolution first for faster processing
- Detect eyes within detected face regions to filter false positives
- Face detection technology has serious privacy implications — always obtain consent and follow applicable laws
Key Takeaways
- Build a complete face detection application that processes both images and video.
- Face detection locates faces in images (bounding boxes). Face recognition identifies who the person is — a separate, harder task
- Haar cascades are classical CV detectors — fast and built into OpenCV via CascadeClassifier
- Use detectMultiScale() with tuned scaleFactor and minNeighbors for the right sensitivity