Face Detection Systems are one of the Artificial Intelligences most commonly uses.

Meanwhile, security and robotics implement it in an inconspicuous way, we use Face Detection every time we take a photo or upload content to social media.

It has become part of our lives and mostly of the people don’t even notice what’s behind it.

Related course: Python Machine Learning Course

Face Detection can seem simple, but it’s not. Is a technology capable to identify and verify people from images or video frames. Is similar somehow to fingerprint or eye iris recognition systems.

Python Face Detection

Introduction

So, what we want to say with all of this? Face Detection is possible for everyone that know how to code. Developers and programmer can reach it.

They will only going to need a library, from example Open CV.

They also will need a programming language, from example Python.

And, they have to be a little patient if they didn’t do it before.

You can’t skip the all steps and go to action without some errors.

Why Open CV?

Open CV means Open Source Computer Vision and is a library originally written in C++ and later written for Python, that’s the programming language we are going to use.

This library has a design for computational efficiency and a strong focus on real-time applications.

That can sound accurate to Face Detection and it is. Open CV can search for faces within a picture using machine learning algorithms.

But the process is tricky because faces are complicated. There’s thousands and thousands of small patterns and features that must match.

opencv python

Machine Learning

Machine learning algorithms have tasks called classifiers. Classifiers identify the face into thousands of smaller, bite-sized tasks and that way is easier to do it.

Imagine this: a face can have 6,000 or more classifiers and all must match for a face to be detected.

Algorithm starts at the top left of a picture and moves down across small blocks of data. Those 6,000 classifiers have to test it and there’s a millions of calculations to do.

It’s pretty obvious your computer will stop. If you have to do the work yourself you would lose your mind.

face detection with python opencv

Cascades

Open CV uses cascades to solve the problem of detecting faces into multiple stages.

Cascades do a very rough and quick test for each block. If that block passes, does a more detailed test and so on.

The algorithm can have 30 to 50 cascades and detect a face if all stages pass.

This makes possible face recognition to be done in real time.

Cascades are XML files that contain Open CV data, used to detect objects.

Example

Once Open CV is installed and you understand it, it’s time to check the result of Face Detection with Python.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import cv2
import sys

imagePath = sys.argv[1]
cascPath = sys.argv[2]

faceCascade = cv2.CascadeClassifier(cascPath)

# read and convert image
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# detect faces in the image
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
# flags = cv2.cv.CV_HAAR_SCALE_IMAGE
)
print("Found {0} faces!".format(len(faces)))

# show face detections
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2)

cv2.imshow("Faces found", image)
cv2.waitKey(0)

Run the program with the command:

1
python3 face.py workplace-1245776_960_720.jpg haarcascade_frontalface_default.xml

You can download the cascade here

There are two things that can result:

When the picture is taken with a high quality camera and close to the face, is more probably to facial recognition to be accurate.

When the picture doesn’t have a decent resolution and is far from the face, is possible to false positives to appear.

Download examples and exercises