Facial Detection With Python And OpenCV

Total nerd
5 min readMar 14, 2021

OpenCV is the Python implementation of the open source project OpenCV which is the most powerful computer vision library used in major programming languages like Java and C++ to build computer vision based applications like facial detection and facial recognition

What The Heck Is Even Computer Vision ?

Computer vision is a field in computer science that is concerned with the idea of enabling the machine (computer) to see the world like we do in some form and this field is insanely growing and has so many applications in many domains but in this article we’re interested in facial detection — the ability to detect the presence of a human’s face

How Does The Computer See The World ?

To answer this question we need to understand the idea of how the image is represented in the “eyes” of the machine, whether it’s a computer or a camera or whatever device capturing a live footage of our real world

An image is an array of pixels, pixels are units used in image processing to indicate a value that represents a shade of one of the three main colors, red, green and blue or simply RGB and this value varies from 0 to 255 and the alignment of these pixels can form an image and a video or any live capture is basically a series of these images

Getting Started With OpenCV

Firstly we need to install it using pip with this command pip install opencv-python then let’s see a very minimum program that will allow us to load an image and display it

import cv2 # importing opencvpic = cv2.imread("park.jpg")cv2.imshow("tutorial",pic)cv2.waitKey(0) # prevents the window from immediately closing

And just like that we were able to load an image we already have and display it on the screen in just 4 lines of code !

How Does Facial Detection Works In OpenCV?

It’s fairly easy for the average programmer who only wants to make use of the library without really going into details but the idea is as follows

On their github there are many “cascade classifier” files that help doing this task easily but what does that even mean?

A cascade file, if you open one, is like an xml file that defines very specific attributes that if existed they indicate the object that the classifier is written to detect, for example there is a human face classifier and a cat face classifier and many others you can find on their github

How To Use Those Files

  1. Click on the file you want to use
  2. Click on “Raw” as shown in the pic

3. Right click then “save as” and save it preferably in the same directory as your Python script

Loading Our Cascade And Detect Faces In An Image

Firstly we’re gonna load an image i download and you can choose any image but make sure it has faces in it and then we’re gonna load our cascade file to help us detect those faces

I’m gonna be using an image of RDJ from google and also downloading this file here

Another thing to notice about OpenCV it uses a color system called BGR unlike what’s common which is RGB so each time you want the cascade classifier to work more accurately you need to convert your image to from BGR to preferably gray as it’s easier to detect objects

Now let’s apply all that to our program

import cv2

# Load the cascade file we downloaded
cascade = cv2.CascadeClassifier(PATH_TO_THE_FILE)
# Load our image
img = cv2.imread('robert.jpg')
# Doing the conversion
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# This method returns us 4 coordinates
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2) # Draws a blue rectangle with a thickness of 2
cv2.imshow('facial detection', img)
cv2.waitKey(0)

Real Time Facial Detection Using Your Webcam

One of my favorite things to do with OpenCV is detecting faces in videos and also my own webcam so let’s see how easy that is to do, it’s seriously really simple

Firstly i’ll show you how to stream your webcam and then how to use a video you have saved on your computer — that’s fairly easy in both cases

import cv2

cascade = cv2.CascadeClassifier(PATH_TO_FULE)

vid = cv2.VideoCapture(0)# Selects the first video camera source which is our webcam
# To use a video file as input
# vid = cv2.VideoCapture(VIDEO_FILE)

while True:
# Read the frame
_, img = vid.read()
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect the faces
faces = face_cascade.detectMultiScale(gray, 1.8, 3)
# Draw the rectangle around each face
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
# view the frame/image
cv2.imshow('facial detecction in a video', img)
# Stop if escape key is pressed
k = cv2.waitKey(30) & 0xff
if k==27:
break
# Release the VideoCapture object
vid.release()

We used the cv2.VideoCapture() to detect and use the video source we have, now in this case we pass in the the index of that device so we passed 0 being the first video capture device we have and if we were to use an external camera we would pass in 1 or 2 depending on how many we have

As we have mentioned earlier, a video is an array of images so we need to “loop” over each frame or image in the video then the read() method will return us a list of two items, one is whether it was successful to read the frame or not and the other is a numpy array of the frame pixels

The numbers passed as arguments to the detectMultiScale() along with the grey scale frame may vary depending on the video used so please try out with different numbers until they fit your need

Thanks For Reading

Thanks if you’ve made it this far and i hope you enjoyed learning about OpenCV and how easy it is to use its basic features

Useful Links

OpenCV tutorials

Learn Python

--

--