Skip to content

Face Blur

Blurs every face in the webcam image.

Run it

python examples/face_blur.py

Code

examples/face_blur.py
"""Blur every face in the webcam image.

Press q to quit.
"""

import cv2 as cv

from detectors_world import create_detector

cap = cv.VideoCapture(0)
with create_detector("face") as face:
    while cap.isOpened():
        ok, frame = cap.read()
        if not ok:
            break
        for found in face.detect(frame, draw=False):
            # Boxes can start outside the image when a face is near the edge.
            x, y = max(found.x, 0), max(found.y, 0)
            region = frame[y : found.y + found.height, x : found.x + found.width]
            if region.size:
                region[:] = cv.GaussianBlur(region, (51, 51), 0)
        cv.imshow("Face Blur", frame)
        if cv.waitKey(1) & 0xFF == ord("q"):
            break
cap.release()
cv.destroyAllWindows()

How it works

  • draw=False keeps the boxes off the image, and the program uses the positions itself.
  • A box can start outside the image near an edge, so max() limits it before slicing the frame.
  • cv.GaussianBlur blurs just that region, and the result is written back into the frame.

Try next

  • Increase the (51, 51) blur size for a stronger effect. Both numbers must be odd.
  • Replace the blur with a solid box using cv.rectangle.