Real-time Face Detection and Blurring using OpenCV

Abhinav Shreyash
2 min readAug 3, 2023

What is OpenCV?

OpenCV is a powerful open-source library used for image processing and computer vision tasks. It enables us to perform various operations like face detection, object tracking, and more.

Installation of OpenCV -

To get started, you can easily install the OpenCV library on your system using the following command:

pip install opencv-python

and we will also need of Haar cascade classifier file to detect the face of the video. Now we will read about the Haar cascade classifier.

What is the Haar Cascade Classifier?

Haar Cascade Classifier is an Object Detection Algorithm used to identify faces in an image or a real-time video. It employs a machine learning approach for visual object detection which is able to process images very quickly and achieve high detection rates.

Face Detection with Haar Cascade Classifier -

OpenCV uses Haar Feature-based Cascade Classifiers and comes with pre-trained XML files of various Haar Cascades. and Face detection with Haar Cascades algorithm needs a lot of positive images and negative images to train the classifier. The model created from this training is available in the OpenCV GitHub repository.

https://github.com/opencv/opencv/tree/master/data/haarcascades.

Now, we will start writing the code.

Firstly, import the library

import cv2

Sets the video source to the default webcam, which OpenCV can easily capture.

cap = cv2.VideoCapture(0)

we capture the video. The read() function reads one frame from the video source, which in this example is the webcam. This returns:

  1. The actual video frame read (one frame on each loop)
  2. A return code
while True:
ret, img = cap.read()

In this code, we will use the detectMultiScale() function to detect the
faces of the video.

face  = model.detectMultiScale(img)

Now, we will find the coordinates in our video. We use these coordinates to blur the face in our video.

x1 = face[0][0]
y1 = face[0][1]
x2 = face[0][2] + x1
y2 = face[0][3] + y1
cimg = img[y1:y2 , x1:x2]
blur_img = cv2.blur(cimg, (50,50))
img[y1:y2 , x1:x2] = blur_img

Now, To view the video using the imshow() function and cv2 wait key () allows you to wait for a specific time in milliseconds until you press “enter” on the keyword and destroyAllWindows() simply destroys all the windows we created.

cv2.imshow(“Blurred Face”,img)
if cv2.waitKey(100) == 13:
break
cv2.destroyAllWindows()

Conclusion

By utilizing OpenCV and Haar Cascade Classifier, it is possible to easily detect and blur faces in a live video stream. This approach is particularly valuable for preserving privacy and anonymity when handling video footage that includes human faces. OpenCV’s versatility and user-friendly nature make it an ideal choice for tackling a wide range of computer vision and image processing challenges. Don’t hesitate to experiment further and apply this method to other exciting projects. Best of luck with your coding endeavors!

--

--