Blur and anonymize faces with OpenCV and Python
In this article, we are going to see how to Blur and anonymize faces with OpenCV and Python.
For this, we will be using Cascade Classifier to detect the faces. Make sure to download the same, from this link: haarcascade_frontalface_default.xml
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
- Firstly, we use a built face detection algorithm, to detect the face from a real-time video or from an image. Here, we will be using the cascade classifier method to detect a face from real-time video(using a webcam).
- Then, the frames from the real-time video are read. The latest frame is stored and is converted into grayscale, to understand the features in a better manner.
- Now, to make the output, aesthetically pleasing, we will make a color-bordered rectangle around the detected face. But, we want the detected face to be blurred, so we use the medianBlur function to do the same, and mention the area, up to which the face should be blurred.
- And, now we want to show the blurred face, the frame which was read using imshow function, and we want it to be shown, till we press a key.
Step 1: Importing the Face Detecting Algorithm, called Cascade Classifier.
Step 2: Capturing the frames from the video, in order to detect the face from the frame
Step 3: The captured frame being changed to grayscale.
Step 4: Drawing a colored rectangle around the detected face.
Step 5: Blur the portion within the rectangle(containing the detected face).
Step 6: Show the final output, i.e. the detected face(within the rectangle) is blurred.
Below is the complete implementation: