A simple and effective Python tool to automatically blur faces in images, videos, or real-time via webcam, using MediaPipe for face detection.
- Image Mode: Blur faces in a static image
- Video Mode: Process a complete video and blur all detected faces
- Webcam Mode: Blur faces in real-time from your webcam
- Python 3.7+
- OpenCV
- MediaPipe
-
Clone or download this repository
-
Install the required dependencies:
pip install opencv-python mediapipepython face_blur.py --mode webcamPress ESC to quit.
python face_blur.py --mode image --filePath path/to/image.jpgThe processed image will be saved to ./output/output.png
python face_blur.py --mode video --filePath path/to/video.mp4The processed video will be saved to ./output/output.mp4
| Argument | Type | Default | Description |
|---|---|---|---|
--mode |
str | webcam |
Execution mode: webcam, image, or video |
--filePath |
str | None |
Path to image or video file (required for image/video modes) |
.
├── face_blur.py # Main script
├── README.md # Documentation
└── output/ # Output folder (created automatically)
├── output.png # Processed image
└── output.mp4 # Processed video
The script uses MediaPipe Face Detection with the following parameters:
- model_selection: 0 (model optimized for short distance, < 2 meters)
- min_detection_confidence: 0.5 (minimum confidence threshold for detection)
You can adjust these parameters in the code according to your needs.
# Blur faces in a group photo
python face_blur.py --mode image --filePath photos/group.jpg
# Blur faces in a surveillance video
python face_blur.py --mode video --filePath videos/surveillance.mp4
# Real-time usage
python face_blur.py --mode webcam- Face Detection: MediaPipe detects faces in the image/frame
- Coordinate Extraction: Bounding boxes of faces are retrieved
- Blur Application: A gaussian blur (30x30) is applied to each detected face
- Save: The result is displayed (webcam) or saved (image/video)
In the process_img() function, change the (30, 30) values:
img[y1:y1 + h, x1:x1 + w, :] = cv2.blur(img[y1:y1 + h, x1:x1 + w, :], (50, 50))# model_selection=0 : short distance (< 2m)
# model_selection=1 : long distance (> 2m)
with mp_face_detection.FaceDetection(model_selection=1, min_detection_confidence=0.5) as face_detection:# Value between 0.0 and 1.0
# Higher = fewer false positives, but may miss faces
min_detection_confidence=0.7- Default blur is 30x30 pixels
- Output videos use MP4V codec with 25 FPS
- The
output/folder is created automatically - Output files overwrite previous files
- Check that your webcam is connected
- Try changing
cv2.VideoCapture(0)tocv2.VideoCapture(1)
- Check that the file path is correct
- Ensure the video format is supported (mp4, avi, mov, etc.)
- Reduce video/webcam resolution
- Use
model_selection=0for better performance
Contributions are welcome! Feel free to:
- Report bugs
- Suggest new features
- Improve documentation
This project is free to use for educational and personal purposes.
Privacy Note: This tool is designed to protect privacy by blurring faces. Make sure you have the necessary permissions before processing images/videos of people.