Getting Acquainted with Face Detection using OpenCV

Posted By Harshit Verma | 30-Nov-2018


Face detection also refers to psychological process by which the humans locate and attend to the faces in a visual scene.OpenCV is most popular library for the computer vision. Originally written in the C/C++, it now provides bindings for the Python. OpenCV uses the machine learning algorithms to search for the faces within the picture. Because faces are quite complicated, there is not one simple test that will tell you if it found a face or not able to find it. Instead, there are thousands of small patterns and features that must be matched. The algorithms that 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 the classifiers.
Like a series of the waterfalls, the OpenCV cascade breaks problem of detecting the faces into multiple stages. For each block, it does a very rough and a quick test. If that passes, it does slightly more detailed a test, and so on. This algorithm may have the 30 to 50 of these stages or cascades.


1. Python
2. OpenCV
3. Django

Steps for face detection are as follows:-

Installing OpenCV

First of all, you need to have a python install in your system, try to use the latest version:-
steps to install
amd upgrade python

1. Open the terminal via Ctrl+Alt+T or searching for the “Terminal” from the app launcher. When it opens, run the command to add  PPA:

    sudo add-apt-repository ppa:jonathonf/python-3.6

2. Then check the updates and install the  Python 3.6 via commands:

    sudo apt-get update

    sudo apt-get install python3.6

3. To make the python3 use the new installed python 3.6 instead of the default run the following two commands:

    sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 1

    sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 2

Now, understanding the Code :
Let’s break down the actual code, which you can download from repository. Grab script, the abc.png pic, and the haarcascade_frontalface_default.xml.

# Get supplied values from user:-

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

You first pass in image and cascade names as the command-line arguments. We will use the ABC image as well as default cascade for detecting the faces provided by OpenCV.

# Create the haar cascade

faceCascade = cv2.CascadeClassifier(cascPath)

Now we create cascade and initialize it with the face cascade. These loads face cascade into the memory so it’s ready for the use. Remember, the cascade is just an XML file that contains data to detect faces.

# Read the image

image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

Here we read image and convert it to the grayscale. Many operations in the OpenCV are done in grayscale.

# Detect faces in the image

faces = faceCascade.detectMultiScale(
    minSize=(30, 30),
    flags =

This function detects actual face and it is the key part of our code:

The detectMultiScale function is general function that detects the objects. Since we are calling it on face cascade, that’s what detects.

The first option is grayscale image.

The second is scale factor. Since some faces may be closer to the camera, they would appear bigger than faces in back.

The detection algorithm uses moving window to detect objects. minNeighbors defines how many objects detected near current one before it declares face found. minSize, meanwhile, gives size of each window.

print "Found {0} faces!".format(len(faces))

# Draw a rectangle around faces

for (x, y, w, h) in the faces:
 cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
This function returns four values: x and y location of rectangle, and  rectangles width and height (w , h).

cv2.imshow("Faces found", image)

In end, we display the image and wait for the user to press a key.

OpenCV-Python is the library of Python bindings designed to solve the computer vision problems. So this is the simple intuitive explanation of how the face detection works.

References :-

Request for Proposal

Recaptcha is required.

Sending message..