Real Time AI Face Detection With Python for Beginners

Krishnadeva Sathursan
The Startup
Published in
4 min readAug 15, 2020

In this article, we’ll look at an easy method to get started with Face Detection using Python and OpenCV. OpenCV is the most popular library for computer vision. Originally written in C/C++, it provides bindings for Python. Machine Learning Algorithm is used in OpenCV for searching faces within pictures. Because faces are all seen as complex, they cannot be determined by a simple test. The algorithms break the task of identifying the face into thousands of smaller, bite-sized tasks, each of which is easy to solve. These tasks are also called Classifiers.

First we need to install openCV in our computer. So, we need to write that below code in command line

pip install python-opencv

Then we need to import that(openCV) library in our code. That library is called that cv2. it means openCV version 2.

import cv2
Trained face datasets

Then we need to download pre-trained data. openCV provides bunch of data and they actually trained bunch of face images. These data is actually trained on bunch of faces.

we need to download below marked(haarcascade_frontalface_default.xml) file. Put the downloaded file in the same working directory.

Front face data set

Then we can go and import it in our code. I assign the downloaded file to a variable called face_cascade. Here we call the opencv library(cv2) and call the function called CascadeClassifier() to classify the faces with the help of Cascade algorithm.

face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + ‘haarcascade_frontalface_default.xml’)

Then we need to pass the input image. Here we using the image read function called imread() from openCV. Image is just an array like a big matrix of a bunch of numbers, here pixels are just numbers. So, here we reading the image into big two dimensional array.

img = cv2.imread(‘img.jpg’)

Then we need to turn the picture into black and white. The only way that algorithm knows, how to look at a face is in black and white. Because, It is easier to work with one number in each pixels, on a range from black and white. If it is a coloured picture(RGB) then it has 3 numbers in each pixels. so it may take some some time and may be less accurate to process.

grayscaled_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

Next, we want to detect faces in the input image. Before, we created a classifier called face_cascade. From there, we’re gonna call a function called detectMultiScale(), this means, no matter the scale of the face whether it is small or big it’s just looking for the overall composition like the relations of eyes to the nose to the mouth whether it’s smaller up close or if there’s multiple of them then it just wants to detect all of them. Detected faces are returned as a list of rectangles. So it just returned the coordinates of those green rectangles

face_coordinates= face_cascade.detectMultiScale(grayscaled_img)
Detected face with coordinates

Once we have those coordinates, we are able to draw those rectangles on our image. If a lot of faces are found in our image, the coordinates of each face will be calculated using the for loop. The rectangle() function has 5 parameters. First one is the input of the image and the second parameter is the top left coordinate of the rectangle and the next parameter describes about the bottom right coordinate of the rectangle. The 4th parameter gives the colour of the rectangle and the final parameter describes the thickness of the circle.

for (x,y,w,h) in face_coordinates:
cv2.rectangle(img, (x,y),(x+w,y+h),(0,255,0),2)

Finally we need to open the detected photo.

cv2.imshow(‘Face detecter’,img)
cv2.waitKey()
#ENTIRE CODE
import cv2
#Load some pre-trained data on face frontals from opencv (haar cascade algorithm)face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')#choose an image to detect faces inimg = cv2.imread('av.jpg')#Must convert to grayscalegrayscaled_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#Detect facesface_coordinates= face_cascade.detectMultiScale(grayscaled_img)#Draw rectangles around the facefor (x,y,w,h) in face_coordinates: cv2.rectangle(img, (x,y),(x+w,y+h),(0,255,0),2)cv2.imshow('Face detecter',img)cv2.waitKey()
Multiple face detection using openCV

Just 10 lines of code…

--

--