Real time face and eyes detection with OpenCV

Before preceding into the face detection we need to know what Haar-like features and cascade classifiers are. Traditionally working with image intensities(using RGB) made the task of feature calculation computationally expensive. Then people started to talk about feature calculation based on Haar wavelets to reduce the cost of computation. Paul Voila and Michel Jones in 2001 used this concept and developed the Haar-like features. A Haar-like feature considers adjacent rectangular regions at a specific location in a detection window, sums up the pixel intensities in each region and calculates the difference between these sums. This difference is used to categorize the subsections of an image. For e.g in human face images, the region of eyes is darker than cheeks.

So when detecting an object in an image, the window of target size is moved over the input image and for each subsection of the image the Haar-like feature is calculated. This difference is then compared to the learned threshold that separates the object from non-objects.

The Haar-like features are weak learners or classifiers so large number of Haar-like features are necessary to classify images with higher accuracy. This is because one simple image can contain 180, 000+ Haar-like minute features. That’s why in Haar-like features are organized into something called classifier cascade to form a strong learner or classifier. Due to the use of the integral image, the Haar-like features can be calculated in very higher speed than the traditional approach.

Now let’s start the code. The first thing we need is to install OpenCV on our computer. The installation can be quite challenging if depending on your operating system but let’s look at the simpler way to do this. If you have installed anaconda already simply execute the following command to install OpenCV and its dependencies:

conda install -c menpo opencv3

Test if it is correctly installed:

$ python3
>> import cv2

If this command in the python shell does not give any error then congratulations everything is set up. OpenCV comes with a detector and trainer. Now for detecting eyes and face, we need two XML files that have predefined Haar-like features or classifiers. You can, of course, build your own classifier to detect objects too. But we are detecting face and eyes here and openCV already comes with the classifier so why to reinvent the wheel. It will save us a lot of time in developing the system. We can download the following two files from the OpenCV’s Github repo:

  1. haarcascade_frontalface_default.xml
  2. haarcascade_eye.xml

Now create another file called in the same directory as of the above XML files. Then inside the python file, we first import the OpenCV library and load the two cascades.

Since Haar-like features classifier or cascades only works with greyscale image, we need to convert our image to greyscale. This is done in following section.

import cv2
# Load the Haar cascades
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

Now we need a function that will do the detection. We first get the coordinates that detect the face and the eyes. Then we will iterate faces that are detected in the video and for each faces we draw rectangle indicating that face. And inside each faces we will detect the eyes.

Now you need to capture video from your webcam and pick a frame from those videos. After you get the frame you need to convert to the greyscale then pass both color and greyscale image frame to the above function.

Now if you run the above code you will see that your eyes and face is detected.

If you are lost during the post you can copy the code from my repo directly and run into your machine.

If you like this post, don’t forget to clap the post and follow me on medium and on twitter. I also write about recommendation systems and other widely used deep learning and machine learning techniques. Do not forget to check my profile you are interested in those.

Connect with the Raven team on Telegram

Source: Deep Learning on Medium