How To Master Face Detection with OpenCV

Written by Santiago Castro
Technology

Written by Adrian, Software Engineer

OpenCV has become one of the most popular and powerful computer vision libraries out there today. Using machine learning, this library was built to allow developers a more hands-on implementation of computer vision. Being an open-source and cross-platform library has made this a vital tool for exploring academic and commercial Computer Vision capabilities.

Python power

OpenCV (written in C++) offers interfaces and APIs in C++, Matlab/Octave, Java, Python and wrappers for more languages like Javascript. When we talk about AI and Machine Learning applications, one of the most widely used and powerful languages is Python. With its clear syntax, lower learning curve, and the existence of many AI, data analysis and math packages, Python is one of the best places to start when working with OpenCV. Here you can find some great official documentation for using OpenCV with Python.

Detect faces on live video feed!

1. Installation

As a prerequisite, you need to have Python installed and running on your machine. It’s generally better if you use Python 3.x version.

To install OpenCV for Python, use pip (or pip3) as follows:

foo@bar:~$ pip install opencv-python

or

foo@bar:~$ pip install opencv-contrib-python

which allows the use of additional contributor developer modules.

2. Import Library

In order to use the library in your .py file, you need to import the cv2 package, which is the name they gave to their Python module.

import cv2


3. Cascade Classifiers

Cascade classifiers are a great way to dive into OpenCV’s amazing features. They abstract the whole Machine Learning process for a specific process (i.e. face recognition, object identification, etc.) behind a simple xml file that includes the trained model specifications.

Official OpenCV documentation tells us that: “Object Detection using Haar feature-based cascade classifiers is a machine learning based approach where a cascade function is trained from a lot of positive and negative images. It is then used to detect objects in other images.”

To explore this approach, OpenCV offers a simple but really useful catalog of pre-trained cascade classifiers on their GitHub repository. You can find them at OpenCV repository.

To import a cascade classifier, we just need to download the xml file to our file system and import it into our script.

cascade_faces = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')


4. Evaluate images

Usually, we start detecting objects using openCV on individual images using cv2:

# Load image from file system
image = cv2.imread('image.jpg')

Note that the method we are using in this tutorial for face detection works only on grayscale images. But don’t worry too much as cv2 offers a way to convert images when necessary.

# Convert to grayscale image to allow using detection method
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

We can use our cascade classifier to detect the objects - or in this case, faces. You can play with the second and third parameters (scaleFactor and minNeighbors) to improve the quality of object detection.

# 2nd and 3rd params are scaleFactor and minNeighbors
faces = cascade_faces.detectMultiScale(gray, 1.5, 4)

According to OpenCV documentation:

scaleFactor:    Parameter specifying how much the image size is reduced at each image scale.

minNeighbors:    Parameter specifying how many neighbors each candidate rectangle should have to retain it.


5. Draw mark

The detectMultiScale method returns a list of rectangles that represent each detection, so we need to draw all those rectangles on the image now.

CV2 offers a method to draw those rectangles having the position (x,y), width (w) and height (h) of the rectangle.

Using a for loop, we can add each rectangle to the image specifying the color (blue,green,red) and line weight (number)

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

and finally, we can show the image.

cv2.imshow('Faces', img)
How To Master Face Detection with OpenCV


6. Detection on video

If you want to go further and perform this detection on a live video feed, you first need to get a video captured.

OpenCV library offers a method to capture a video feed from files or directly from a webcam as follows:

# Live video from webcam
cam = cv2.VideoCapture(0)

# From video file
video = cv2.VideoCapture('filename.mp4')

To implement “live” analysis and face detection on a video feed, you need to execute all of these steps: load image, detect faces, draw markers for the faces, show markers on a loop, frame by frame and add a closing condition (press ESC key).

while True:
    _, img = cam.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = cascade_faces.detectMultiScale(gray, 1.5, 4)

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

    cv2.imshow('Faces', img)

    # close loop and video window when ESC key is pressed
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break


This is what the final script looks like:

import cv2

cascade_faces = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

cam = cv2.VideoCapture(0)

while True:
    _, img = cam.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = cascade_faces.detectMultiScale(gray, 1.5, 4)

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

    cv2.imshow('Faces', img)

    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break


Up for a challenge?

Now that you know a bit more, how about a challenge to test your detection skills? Try modifying the script to also detect eyes in the video feed. It’s not too difficult, but a great way to practice what you’ve learned! If you find yourself struggling, you can find some help for this here.

What is next?

OpenCV is an amazing open-source library for anyone who wants to explore the world of Machine Learning, Computer Vision, and general Artificial Intelligence with a hands-on approach. Go check the complete documentation at OpenCV and as always… keep learning!

--

If you want to stay up to date with all the new content we publish on our blog, share your email and hit the subscribe button.

Also, feel free to browse through the other sections of the blog where you can find many other amazing articles on: Programming, IT, Outsourcing, and even Management.

Share
linkedIn icon
Written by Santiago Castro
LinkedIn

With over +16 years of experience in the technology and software industry and +12 of those years at Jobsity, Santi has performed a variety of roles including UX/UI web designer, senior front-end developer, technical project manager, and account manager. Wearing all of these hats has provided him with a wide range of expertise and the ability to manage teams, create solutions, and understand industry needs. At present, he runs the Operations Department at Jobsity, creating a high-level strategy for the company's success and leading a team of more than 400 professionals in their work on major projects.