Skip to content

sea-rod/face-blur

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Real-Time Face Blurring with MTCNN

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.

🚀 Features

  • 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:0 for 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.

🛠️ Tech Stack

  • MTCNN: Face detection.
  • OpenCV: Image processing and video I/O.
  • TensorFlow: Backend for MTCNN.
  • NumPy: Matrix operations for masking.

📋 Installation

  1. Clone the repository:

    git clone https://github.com/yourusername/face-blur-mtcnn.git
    cd face-blur-mtcnn
  2. Install dependencies:

    pip install mtcnn tensorflow opencv-python numpy matplotlib

💻 Usage

1. Real-Time Webcam

Run the script as-is to use your default system camera:

python main.py

2. IP Camera Integration

To 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")

3. Processing Static Images

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)

⚙️ How it Works

The project follows a 3-step pipeline to ensure privacy while maintaining visual quality:

  1. Detection: MTCNN scans the frame for facial landmarks.
  2. Padding & Scaling: The "box" is expanded by 25% to ensure the entire forehead and chin are covered.
  3. 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.

🔧 Configuration

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.

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

About

An application that blurs face in real-time

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages