Face Detection: Algorithms, Mathematics & Code

🔍 Introduction

Face detection is the task of identifying human faces in digital images and video. It serves as the first step in many facial analysis systems such as recognition, expression detection, and security.

📐 Mathematical Foundation

Face detection involves computing various features across an image:

  • Integral Images
  • Haar-like Feature Extraction
  • Sliding Window Techniques
  • Neural Network Filters
H = Σ(white region pixels) - Σ(black region pixels)

🧠 Algorithm: Viola–Jones

  1. Convert image to grayscale.
  2. Generate integral image.
  3. Apply Haar-like features.
  4. Use AdaBoost for classification.
  5. Apply a cascade of classifiers for speed.

💻 Python Code: OpenCV

import cv2

face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
img = cv2.imread("person.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 5)

for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)

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

📊 Applications

  • Surveillance Cameras
  • Face Unlock
  • Driver Monitoring
  • Photo Tagging

📌 Conclusion

Face detection has evolved from handcrafted Haar features to modern deep CNN-based detectors. It's a critical tool for biometric systems, security, and smart interfaces.

Authored by Amitesh Maurya | © 2025 Amitesh Maurya