Face Detection from Image and Video

In this blog post, we will see how to detect faces and eyes from videos & images using Haar feature-based cascade classifiers, OpenCV library with C++ and Python programming languages.

Let's first quickly see how these two applications (face detection from image and video) look in practice. Then we dive into Haar feature-based cascade classifiers and the C++ and Python code.

Outline

Applications: Face detection from image and video

Face detection from image

In this first application, we start with loading Haar feature-based cascade classifiers. Then we find the rectangle areas which contain faces using the face cascade classifier and save them as faces region of interest sub-images.

After that, we find the areas which contain eyes inside the face sub-images using the eyes cascade classifier.

Finally, we draw blue and green rectangles around the faces and eyes that we found. More details can be found in the code section, the code is heavily commented and mostly readable.

Face detection from video:

In this second application, we use the same technique and tools with a video file. The logic and the code are almost the same.

The main difference is that we use frames from the video file instead of images. These frames can be thought of as images. More details can be found in the code section.

Haar feature-based cascade classifiers

Haar feature-based cascade classifiers are effective object detection methods proposed by Paul Viola and Michael Jones in their paper, “Rapid Object Detection using a Boosted Cascade of Simple Features,” published in 2001.

It 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.

For those who want to know more, let’s dive a little deeper:

Each stage of the cascade consists of a group of Haar-like features selected by the AdaBoost learning algorithm. For the first several stages, the classifiers are trained to reject most of the negative subwindows while detecting almost all face-like candidates.

This architecture can speed up the detection process dramatically because most of the negative images can be discarded during the first two or three stages.So most of the computation effort is focused on face-like subwindows.

These subwindows are evaluated in sequence by stage classifiers and the result of each Haar-like feature in a stage is accumulated. When all the features in a stage are computed, the accumulated value is compared with a stage threshold value to determine if the current subwindow is a face-like candidate.

The subsequent stage is activated only if the previous stage turns out to be a positive result. If a candidate passes all stages in the cascade, it is determined that the current subwindow contains a face.

Source

Further reading on OpenCV Haar feature-based cascade classifiers:

Code (C++ and Python)

In this section, we will look at four programs; face and eyes detection from image and video implemented in both C++ and Python with the same logic.

The code is heavily commented and mostly readable. So, let's look at the code now which explains itself step by step in the comments - see the lines with // for C++ and # for Python code files:

C++ – Face detection from image

Python – Face detection from image

C++ – Face detection from video

Python – Face detection from video


Source Code:

https://github.com/simsekgokhan/computer-vision/tree/master/Face-Detect-cpp https://github.com/simsekgokhan/computer-vision/tree/master/Face-Detect-python


Thanks to our friend and face model Anton Tsapliuk for submitting his video for this example 🙏

And thank you for reading. This is the second in our series of Computer Vision posts. Stay tuned for more!

You might also like
Earn it, Build it, or Buy it — Three Ways to Grow a Community around your Product

Earn it, Build it, or Buy it — Three Ways to Grow a Community around your Product