How to Auto Blur Faces in Photos and Videos Using OpenCV (with Face Recognition Toggle)

How to Auto Blur Faces in Photos and Videos Using OpenCV (with Face Recognition Toggle)

Whether you're working on a security project, anonymizing people in public footage, or building a social app with privacy in mind—face blurring is essential. In this post, we’ll walk you through building a face detection and blurring tool using OpenCV. Bonus? You can also toggle face recognition using the face_recognition library to exclude known faces from being blurred.

What You’ll Learn

  • Detect faces using Haar Cascades (or optionally MTCNN for better accuracy)

  • Blur detected faces for privacy using OpenCV

  • Bonus: Add a face recognition toggle to protect known faces

Libraries You’ll Need

pip install opencv-python face_recognition

For MTCNN:

pip install mtcnn

Step 1: Detect and Blur Faces Using Haar Cascades

import cv2

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

def blur_faces(image):
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    for (x, y, w, h) in faces:
        face = image[y:y+h, x:x+w]
        face = cv2.GaussianBlur(face, (99, 99), 30)
        image[y:y+h, x:x+w] = face

    return image

Exclude Known Faces from Blurring

import face_recognition
import numpy as np

# Load known face
known_image = face_recognition.load_image_file("your_face.jpg")
known_encoding = face_recognition.face_encodings(known_image)[0]

def blur_faces_with_recognition(image, skip_recognition=False):
    rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    face_locations = face_recognition.face_locations(rgb_image)
    face_encodings = face_recognition.face_encodings(rgb_image, face_locations)

    for (top, right, bottom, left), encoding in zip(face_locations, face_encodings):
        if skip_recognition or not face_recognition.compare_faces([known_encoding], encoding)[0]:
            face = image[top:bottom, left:right]
            face = cv2.GaussianBlur(face, (99, 99), 30)
            image[top:bottom, left:right] = face

    return image

 

Testing the Code

# Load and blur
image = cv2.imread("group_photo.jpg")

# True = skip known face check, False = blur everyone
output = blur_faces_with_recognition(image, skip_recognition=False)

cv2.imshow('Blurred', output)
cv2.waitKey(0)
cv2.destroyAllWindows()

Using MTCNN Instead of Haar

from mtcnn.mtcnn import MTCNN

detector = MTCNN()

def blur_faces_mtcnn(image):
    rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    faces = detector.detect_faces(rgb)

    for face in faces:
        x, y, width, height = face['box']
        x, y = abs(x), abs(y)
        face_crop = image[y:y+height, x:x+width]
        face_crop = cv2.GaussianBlur(face_crop, (99, 99), 30)
        image[y:y+height, x:x+width] = face_crop

    return image

Use Cases

  • Surveillance systems — blur passerby faces

  • Public datasets — anonymize sensitive data

  • Social apps — let users choose what gets shown

  • Healthcare & Education — protect identities in media

Final Thoughts

Face detection and blurring is a powerful way to build privacy-first systems. Whether you're scrubbing video for compliance or just blurring photo-bombers in vacation photos, you now have a working solution.

Next step? Wrap it into a web or mobile UI, or integrate into your video pipeline with cv2.VideoCapture.