Face Blur Privacy

Automatically detect and blur faces for privacy protection.

View Source Code


Overview

Detect faces in images or video and apply blur for privacy protection. Useful for social media, journalism, and security applications.

Key Techniques:

  • Haar cascade face detection
  • Gaussian blur
  • Pixelation effect
  • Region of interest processing

How It Works

Frame → Face Detection → Extract ROI → Apply Blur → Replace ROI
   ↓          ↓              ↓            ↓            ↓
[Image]  [Bounding      [Face        [Blurred     [Final
          boxes]         region]      face]        image]

Key OpenCV Functions

# Load cascade classifier
face_cascade = cv2.CascadeClassifier(
    cv2.data.haarcascades + 'haarcascade_frontalface_default.xml'
)

# Detect faces
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)

# Blur each face
for (x, y, w, h) in faces:
    roi = frame[y:y+h, x:x+w]

    # Gaussian blur
    blurred = cv2.GaussianBlur(roi, (99, 99), 30)

    # Or pixelation
    small = cv2.resize(roi, (10, 10))
    pixelated = cv2.resize(small, (w, h), interpolation=cv2.INTER_NEAREST)

    frame[y:y+h, x:x+w] = blurred  # or pixelated

Blur Types

Type Method Use Case
Gaussian GaussianBlur Natural soft blur
Pixelation Resize down then up Mosaic/censored look
Box blur Simple average
Median medianBlur Preserves edges

Detection Parameters

faces = face_cascade.detectMultiScale(
    gray,
    scaleFactor=1.1,    # Image pyramid scale (smaller = more accurate, slower)
    minNeighbors=5,     # Higher = fewer false positives
    minSize=(30, 30)    # Minimum face size to detect
)

Controls

Key Action
g Gaussian blur
p Pixelation
+/- Adjust blur strength
s Save screenshot
q Quit

Running the Application

python curriculum/applications/04_face_blur.py

Official Documentation