All projects

Technical Report · ML & Computer Vision

OpenCV Auto Face Blur

Batch video anonymizer that detects faces via OpenCV Haar cascade, blurs detected regions, and writes output through configurable FFmpeg encoding.

AUTHOR  Gaurav Verma CATEGORY  ML & Computer Vision SOURCE  https://github.com/gaurav-321/Opencv-Auto-Face-Blur DATE  Feb 2021 STATUS  published
View Source Python OpenCV FFmpeg Haar Cascade Video Processing VidGear

Abstract

A batch video-processing pipeline that reads frames from a source video, detects faces with an OpenCV Haar cascade, blurs the detected regions, and writes the anonymized output through configurable FFmpeg encoding. The interesting part is the clean separation between detection logic and media I/O, which lets you swap codecs without touching the anonymization code.

1. What This Is

I built this to automate face anonymization in prerecorded video. Rather than a live-preview demo, the tool processes an entire video file end to end: open the source, detect and blur faces frame by frame, and write a complete output file. VidGear and FFmpeg handle the video I/O, so the output codec and encoding parameters are configurable rather than locked to OpenCV's default writer.

2. How It Works

The pipeline is a straightforward sequential loop over frames. Each frame passes through detection, region blur, and encoding in order.

# Stage Input Tool Output
01 Initialize Source video path VidGear / OpenCV Opened capture, Haar cascade loaded
02 Detect Current frame (BGR) OpenCV Haar cascade List of face rectangles
03 Blur Frame + rectangles OpenCV blur Anonymized frame
04 Encode & Write Anonymized frame FFmpeg / VidGear Output video file

3. Implementation Notes

3.1 Detection / encoding separation

The anonymization logic (detect + blur) is decoupled from the media I/O layer. Changing output codec, bitrate, or container format only touches the FFmpeg/VidGear configuration, not the per-frame processing code. This made it straightforward to expose encoding parameters as user-configurable options.

3.2 Haar cascade as the detector

I chose the classical Haar cascade because it runs on CPU with no model download and is trivially fast per frame. The trade-off is well known: it degrades with unusual poses, low lighting, or partial occlusion. For a demo and a lightweight batch tool it is adequate; for production anonymization a deep-learning detector would be more reliable.

4. Constraints

  • Haar cascade recall

    Faces at steep angles, in shadow, or partially occluded will be missed. There is no tracking or multi-scale refinement beyond what the cascade provides out of the box.

  • No audio or metadata handling

    The pipeline processes video frames only. Audio tracks, embedded metadata, and sidecar files in the source are not carried through to the output.

  • Single-pass, no tracking

    Each frame is detected independently. A face that flickers in and out of detection between frames will produce visible blur artifacts rather than a stable region.

  • No test suite or CI

    The project is a working script without automated tests, so regressions in detection thresholds or encoding parameters would only surface manually.

5. Next

  1. a. Swap the Haar cascade for a lightweight deep-learning detector (e.g. MTCNN or a YOLO face model) to improve recall on difficult poses.
  2. b. Add simple inter-frame tracking so blur regions persist across consecutive frames and avoid flicker.
  3. c. Wrap the pipeline in a small CLI with input/output arguments and a basic test harness using a short sample clip.

— end of report —