Face Detect, Blur in Live Video using OpenCV and Python

Lalita Rajpoot
4 min readJan 13, 2022

What is Computer Vision?

Computer Vision is a field of study which enables computers to replicate the human visual system. It’s a subset of artificial intelligence which collects information from digital images or videos and processes them to define the attributes. The entire process involves image acquiring, screening, analysing, identifying and extracting information. This extensive processing helps computers to understand any visual content and act on it accordingly.

Computer vision projects translate digital visual content into explicit descriptions to gather multi-dimensional data. This data is then turned into a computer-readable language to aid the decision-making process.

“The main objective of this branch of artificial intelligence is to teach machines to collect information from pixels”.

How does a computer read an image?

Humans have different skills and one of the skills is to identify an object in an image , computers have machine learning models, which can be thought of as a skill, to perform the same task. As humans need to be trained to perform a particular skill, computers need to train the machine learning model as well.

A computer sees an image as 0s and 1s. Pixel is the smallest unit in an image. When we take a digital image, it is stored as a combination of pixels. Each pixel contains a different number of block. If it a grayscale image, it has only one pixel, whereas if it is a coloured image, it contains three channels: red, green and blue.

What is OpenCV?

OpenCV Originally developed by Intel, OpenCV was later supported by Willow Garage then Itseez, in turn later acquired by Intel

OpenCV is a Python library which is designed to solve computer vision problems. OpenCV supports a wide variety of programming languages such as C++, Python, Java etc. Support for multiple platforms including Windows, Linux, and MacOS.

OpenCV Python is nothing but a wrapper class for the original C++ library to be used with Python. Using this, all of the OpenCV array structures gets converted to/from NumPy arrays.

Use Cases for OpenCV library

There are many ideal use cases for OpenCV :

1. Automated inspection and surveillance

2. Tracking the number of people in a place (for instance, the foot traffic in a mall)

3. Vehicle count on highways along with their speeds

4. Street view image stitching

5. Defect detection in manufacturing processes

6. Interactive art installations

7. Video/image search and retrieval

8. Object recognition

9. TV Channels advertisement recognition

10. Robot and driver-less car navigation and control

11. Analyzing medical images

12. Figuring out 3D structure from motion captured in videos/ films

Advantages of using OpenCV

Easy of use: OpenCV is easy and simple to learn

Availability of many tutorials: The fact that there are lots of tutorials available is a big plus as one can access many learning resources.

Compatibility with leading coding languages: OpenCV works with almost all the leading programming languages today, including Python, C++ and Java.

Here, We’re going to develop a OpenCV program that will detect human face, blur it and get position where we click on image..

So let’s Start….

I have used cv2 module from python-OpenCV library and haarcascade file to detect frontal faces.

import cv2
cap = cv2.VideoCapture(0)
# Haar Cascade to detect frontal faces
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
# Setting position when click on image at anywhere
def lw(event , x , y, i,j):
if event == cv2.EVENT_LBUTTONDOWN:
print("x :", x , "y :" , y)
cv2.circle(image , (x,y) , 50 , [0,255,0], -1 )

#cv2.imshow(photo)
cv2.namedWindow(winname="mywin1")
cv2.setMouseCallback( "mywin1" , lw )
while True:
ret, image = cap.read()
# Detect faces in the live Video Stream
faces = face_cascade.detectMultiScale(image, 1.3,5)
for (x,y,w,h) in faces:
# Enclose inside a blue rectangular box
cv2.rectangle(image,(x,y),(x+w,y+h),(255,140,200),3)
# Select only detected face portion for Blur
face_color = image[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[y:y + h, x:x + w] = blur
cv2.imshow('mywin1', image)
if cv2.waitKey(100) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()

Here, This is my code which is detecting face properly and blur the image . It is also showing the cursor position where we click on image

Blurred Face after detecting My Face

Now, It’ll show position of the blur image.

Blurred Image with it’s Position

I hope this article will help you to get more knowledge about Python-OpenCV.

Thanks for your valuable time , Stay Happy :)

--

--