Face Detection, Face Blurring in Live Stream Video and Images using OpenCV and Python

Dipendra Pant
3 min readOct 23, 2020

--

Before Blur and After Blur

I wanted to anonymize the people’s identity by blurring their faces so for that I used the deadly combination of the old but highly esteemed technology, which are OpenCV with Python 3.Hence I used the Haar Cascade file to detect the faces and then implemented the preexisting blurring method of OpenCV to blur those detected faces.

The steps followed for the blurring and detection of faces are:

1- Read input webcam video frame by frame

video_cam = cv2.VideoCapture(0)

while True:

ret, image_frame = video_cam.read()

2- Use the Haar cascade classifier to detect the faces in Images

face_cascade = cv2.CascadeClassifier(Resources/haarcascade_frontalface_alt.xml’)

faces = face_cascade.detectMultiScale(image_frame,1.3,5)

3- Enclose the detected face within the rectangle

for (x,y,w,h) in faces:

cv2.rectangle(image_frame,(x,y),(x+w,y+h),(255.0,0),3)

4- Take only the face area and blur it with different available blur techniques like Gaussian blur, simple blur, median blur.

face_color = image_frame[y:y + h, x:x + w]

blur = cv2.GaussianBlur(face_color, (51, 51), 0)

image_frame[y:y + h, x:x + w] = blur

5- Done! Now display the output image.

cv2.imshow(‘Face Detected’,image_frame)

if cv2.waitKey(1) & 0xFF == ord(‘q’):

break

Complete Code for Live Webcam Video

'''
Simple Face Blur on a live webcam video streams using Gausian Blur
'''

import cv2
video_cam = cv2.VideoCapture(0)
# Haar Cascade to detect frontal faces
face_cascade = cv2.CascadeClassifier('Resources/haarcascade_frontalface_alt.xml')
while True:
ret, image_frame = video_cam.read()
# Detect faces in the Webcam Video Stream
faces = face_cascade.detectMultiScale(image_frame,1.3,5)
for (x,y,w,h) in faces:
# Enclose inside a blue rectangular box
cv2.rectangle(image_frame,(x,y),(x+w,y+h),(255,0,0),3)
# Select only detected face portion for Blur
face_color = image_frame[y:y + h, x:x + w]
# Blur the Face with Gaussian Blur of Kernel Size 51*51
blur = cv2.GaussianBlur(face_color, (51, 51), 0)
image_frame[y:y + h, x:x + w] = blur
# Display the Blurred Faces
cv2.imshow('Detected Face',image_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_cam.release()
cv2.destroyAllWindows()

Obtained Output:

Obtained Output without blur and with blur effects

Complete Code for Images

import cv2
# Read Input Image
image = cv2.imread('Resources/multiplefaces.jpeg')
# Haar Cascade for Face Detection
face_cascade = cv2.CascadeClassifier('Resources/haarcascade_frontalface_alt.xml')
# Detect the faces in Images
faces = face_cascade.detectMultiScale(image,1.3,5)

for (x,y,w,h) in faces:
# Enclose the detected faces inside a rectangular box
cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),3)
# Select the detected face area
face_color = image[y:y + h, x:x + w]
# Blur the detected face
blur = cv2.GaussianBlur(face_color, (51, 51), 0)
image[y:y + h, x:x + w] = blur

# Display the Blurred Faces Image
cv2.imshow('Face Detected Blurred Image',image)
# Hold output for 20 sec
cv2.waitKey(20000)
cv2.destroyAllWindows()

Obtained Output:

Obtained Output in Images without blur and with blur

Besides this, we can simply fill the detected faces with the color combination of RGB using the simple technique shown by the code below.

import cv2
image = cv2.imread('Resources/multiplefaces.jpeg')
face_cascade = cv2.CascadeClassifier('Resources/haarcascade_frontalface_alt.xml')
faces = face_cascade.detectMultiScale(image,1.3,5)
for (x,y,w,h) in faces:
#Enclose the detected faces inside a rectangular filling the total face
cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),cv2.FILLED)

cv2.imshow('Detected Faces in Image',image)
cv2.waitKey(20000)
cv2.destroyAllWindows()

--

--

Dipendra Pant
Dipendra Pant

Written by Dipendra Pant

Python, OpenCV, Evidence Based Software Engineering, Artificial Intelligent Technologies

No responses yet