Python is one of the most popular programming languages for Machine Learning, and Image processing, and with the help OpenCV library we can process images and videos using Python.
Detecting Faces in an image and blurring it is one of the most common applications of image and video processing with Machine Learning. And with Python and OpenCV we can detect faces and blur them within few lines of code.
In this Python tutorial, I will walk you through the Python code on How to blur Faces in an Image using Python OpenCV. And at the end of this tutorial, we will also write a Python program to blur faces for live webcam video.
But before jumping to the code let’s first install and download all the dependencies.
Install Python OpenCV Library
From the title of this article, you know that we will be using OpenCV with Python, so I am assuming the latest version of Python is installed in your system. If not, don’t worry, the older Python version will work the same, but just make sure that your Python version is greater than Python 3.4, and pip is installed for your Python.
Now for this tutorial, you need to install Python OpenCV, and you can simply install both the packages using the Python pip terminal command.
pip install opencv-python
Download the Harr Cascade
Blurring Faces in an Image is divided into two steps, in the first step we detect the coordinates for the faces, and in the second step, we blur those coordinates.
Detecting faces in an image is a Machine Learning task, it can be done with the help of Classifiers. Luckily OpenCV supports the most common Haar Cascade classifiers to detect faces in an image.
A classifier needs to be trained on thousands of data sets, and for this tutorial, you can copy and paste the trained
haarcascade_frontalface_default.xmlclassifier and save it as
I have also written a tutorial on How to detect faces with OpenCV, and I will be using that source code to detect faces in the images. I will suggest you go through that article first if You want to learn how to detect faces in OpenCV, although I have covered all of that and blurring in this tutorial too.
For this tutorial on Blur Faces In an Image, I will be using the following “Father-Daughter.jpg”.
We are all set, Now its time to open your best Python IDE and Text editor and Start coding.
How to Blur Faces in Images using OpenCV in Python?
We will start by importing the Python OpenCV module and load the Father-Daughter.jp image.
import cv2 as cv #load image image = cv.imread("Father-Daughter.jpg")
cv.imread()function load the image and return a Numpy ndarray of 3 channels representing the BGR matrix. By default, the OpenCV reads the image in BGR(Blue Green Red) format.
After reading or loading the image, convert it into a GrayScale image, because it’s always a good practice to convert the BGR image to a grayScale to reduce the color intensity noises. And the Haar Cascade Face detection classifier does not care about face color intensity it simply detects the faces, so converting the BRG image to GrayScale would not affect the classifier.
#convert image to grayscale image gray_image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
cv.cvtColor()function accepts two parameters, the
image and the color conversion code. Here we want to convert our BGR image to GrayScale Image that’s why we specify the
Now we need to initialize the object for the trained Haar Cascade classifier
haarcascade_frontalface_default.xml, that we have copied, pasted, and saved in our local system as
#read the harr_face_detect_classifier.xml harr_cascade = cv.CascadeClassifier("harr_face_detect_classifier.xml")
cv.CascadeClassifier()method will initialize the Trained Harr Cascade classifier.
And now we can detect faces in the GrayScale image using the Harr Cascade
face_cords = harr_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=1)
detectMultiScale()method returns an array of detected face coordinates. And we can loop through the
face_cords coordinates and blur that area in the image.
for x, y, w, h in face_cords: blur_face = image[y:y+h, x:x+w] blur_face = cv.GaussianBlur(blur_face,(23, 23), 30) image[y:y+blur_face.shape, x:x+blur_face.shape] = blur_face
First, we get a specific area of the face by
image[y:y+h, x:x+w], then blur that face area, and put that blurred area to the real image with
image[y:y+blur_face.shape, x:x+blur_face.shape] = blur_facestatement.
Now show the image with
#show image cv.imshow("Blur Faces", image) cv.waitKey(0)
Now put all the code together and execute.
#python program to blur faces in an Image using OpenCV
import cv2 as cv #load image image = cv.imread("Father-Daughter.jpg") #convert image to grayscale image gray_image = cv.cvtColor(image, cv.COLOR_BGR2GRAY) #read the harr_face_detect_classifier.xml harr_cascade = cv.CascadeClassifier("harr_face_detect_classifier.xml") face_cords = harr_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=1 ) print(face_cords) for x, y, w, h in face_cords: blur_face = image[y:y+h, x:x+w] blur_face = cv.GaussianBlur(blur_face,(23, 23), 30) image[y:y+blur_face.shape, x:x+blur_face.shape] = blur_face #show image cv.imshow("Blur Faces", image) cv.waitKey(0)
You will see a similar image.
Blur Faces in Videos with OpenCV in Python
Now you know how to blur faces in Image, let’s write a Python script that can blur faces in videos or live webcam. Blurring faces in the video are similar to blurring faces in the Image. We can treat a video as a continuous frame of images and blur faces by detecting faces in every frame.
Now let’s code to blur faces in Video with OpenCV in Python
import cv2 as cv #start web cam capture = cv.VideoCapture(0) # 0 for web-cam #read the harr_face_detect_classifier.xml harr_cascade = cv.CascadeClassifier("harr_face_detect_classifier.xml") while True: #read video frame by frame isTrue, frame= capture.read() gray_frame = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) face_cords = harr_cascade.detectMultiScale(gray_frame, scaleFactor=1.1, minNeighbors=1) #blur over faces for x, y, w, h in face_cords: blur_face = frame[y:y+h, x:x+w] blur_face = cv.GaussianBlur(blur_face,(23, 23), 30) frame[y:y+blur_face.shape, x:x+blur_face.shape] = blur_face #show blur face Video cv.imshow("Blur Faces", frame) #press e to exit if cv.waitKey(20) ==ord("e"): break capture.release() capture.destroyAllWindows()
In this Python tutorial, we learned how to blur faces in images and videos using Python and OpenCV. For this tutorial, we have used the OpenCV GaussanBlur() method to blur the faces, apart from it OpenCV also support other blurring methods such as Averaging.
The blurring of faces in Images and videos is divided into two steps, face detecting and blurring faces. For this tutorial, we have used the straightforward and basic face detecting classifier the Harr Cascade Classifier.
People are also reading:
- Python Slice Lists/Arrays and Tuples
- Print without Newline in Python
- Invalid Syntax Python
- Round Whole Numbers in Python
- Python Absolute Value
- Exponent in Python
- How to Sort a Dictionary by Value in Python?
- Math in Python 3 with Operators
- Check Python Version
- How to Use sorted() and sort() in Python?