A Python-based privacy tool that detects and blurs faces in real-time video streams, IP camera feeds, and static images. It utilizes the MTCNN (Multi-task Cascaded Convolutional Networks) framework for high-accuracy face detection and OpenCV for smooth elliptical Gaussian blurring.
- Real-Time Detection: Processes live video with configurable frame-skipping to maintain high FPS.
- Elliptical Masking: Instead of a blocky square, it applies a smooth elliptical blur that fits the natural shape of a face.
- GPU Acceleration: Pre-configured to leverage
GPU:0for faster inference via TensorFlow. - Versatile Inputs: Supports local webcams, remote IP cameras (RTSP/HTTP), and static image files.
- Robust Error Handling: Gracefully handles "out of frame" detections or empty frames without crashing the stream.
- MTCNN: Face detection.
- OpenCV: Image processing and video I/O.
- TensorFlow: Backend for MTCNN.
- NumPy: Matrix operations for masking.
-
Clone the repository:
git clone https://github.com/yourusername/face-blur-mtcnn.git cd face-blur-mtcnn -
Install dependencies:
pip install mtcnn tensorflow opencv-python numpy matplotlib
Run the script as-is to use your default system camera:
python main.pyTo use an IP camera or a remote stream, modify the cv2.VideoCapture line in main.py:
# Replace 0 with your IP camera RTSP/HTTP URL
video = cv2.VideoCapture("rtsp://admin:password@192.168.1.100:554/stream")You can adapt the detect_face and blur_face functions to process local files:
image = cv2.imread("input.jpg")
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = detect_face(image_rgb)
blurred_image = blur_face(results, image_rgb)The project follows a 3-step pipeline to ensure privacy while maintaining visual quality:
- Detection: MTCNN scans the frame for facial landmarks.
- Padding & Scaling: The "box" is expanded by 25% to ensure the entire forehead and chin are covered.
- Elliptical Blurring: * A black mask is created with a white ellipse in the center.
- A heavy Gaussian Blur (101x101 kernel) is applied to the Region of Interest (ROI).
- The blurred pixels are merged back into the original frame only where the mask is white.
You can tweak the performance in main.py:
| Variable | Description |
|---|---|
skip_frames |
Increase this (e.g., 20) to boost FPS on slower CPUs; decrease for smoother tracking. |
threshold_onet |
The confidence threshold (0-1). Higher means fewer false positives. |
padding |
Adjusts how far the blur extends beyond the detected face box. |
device |
Change to "CPU" if you do not have a dedicated NVIDIA GPU. |
This project is licensed under the MIT License - see the LICENSE file for details.