Real-time face detection and blur
Detect faces from your webcam in real time and automatically blur them, a privacy tool for videos.
Beginnerโฑ 4โ6 hours๐ช 7 stepsPythonOpenCV
What you'll learn
- โReading video frames with OpenCV
- โFace detection with a pretrained model
- โImage operations (blur, draw)
What you'll build
- โLive webcam face boxes
- โBlur mode for privacy
- โProcess a saved video file
- โFPS counter
Step-by-step guide
- 1
Install
pip install opencv-python.
- 2
Read the webcam
cap = cv2.VideoCapture(0); loop ret, frame = cap.read(); cv2.imshow; quit on "q".
- 3
Detect faces
Use cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml") or the DNN face detector for better accuracy.
- 4
Draw or blur
cv2.rectangle for boxes; for blur, frame[y:y+h, x:x+w] = cv2.GaussianBlur(face, (51, 51), 30).
- 5
Video files
Read a file with VideoCapture(path) and write the result with cv2.VideoWriter.
- 6
FPS
Measure time per frame and draw the FPS with cv2.putText.
- 7
Ethics
Only record people who agreed. Mention privacy in your README.
๐ Make it stand out
- +Pixelate instead of blur
- +Detect and blur number plates
- +Compare Haar vs DNN accuracy