In one session around computer vision, someone ask the question about which approach is better
Haar Cascades or DNN?
And the answer can be show using the video below
As you can see Haar Cascades works great for faces looking directly to the camera, with good lights and in optimal conditions. However, once my face stop looking at the camera, HC stop detecting faces; and the custom DNN model still working.
But, and this is important, the DNN model will rely only on the set of faces that has been used for training. If the model doesn’t include any demographics, in example Asian friends, the model won’t work with Asian subjects.
At the end, DNN models usually works great than Haar Cascades, however it’s really important to know the limitations of each model.
I’m writing a series of posts about how to control a drone with Python and 20 lines of code, and once I reach to the point to read the camera feed, I’ve added a face detection sample. However this time I didn’t use the face_recognition python package I’ve used in this series, I performed the face detection using OpenCV and Haar Cascades. So, let’s explain a little what’s this.
Let me start quoting an amazing article “Face Detection using Haar Cascades” (see references)
Object Detection using Haar feature-based cascade classifiers is an effective object detection method proposed by Paul Viola and Michael Jones in their paper, “Rapid Object Detection using a Boosted Cascade of Simple Features” 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.
OpenCV comes with a trainer as well as detector. If you want to train your own classifier for any object like car, planes etc. you can use OpenCV to create one. Its full details are given here: Cascade Classifier Training.
And here we come to the cool part, OpenCV already contains many pre-trained classifiers for face, eyes, smile etc. Those XML files are stored in opencv/data/haarcascades/ folder (see references).
Let’s take a look at a really [20 lines] sample code for face detection:
Line 6, we use OpenCV to load the haar cascade classifier to detect faces
Lines 9-20, main app
Lines 10-12, open a frame from the camera, transform the frame to a gray color scaled image and use the face cascade detector to find faces
Lines 14-15, iterate thought detected faces and draw a frame
Lines 17-20, display the webcam image with the detected faces, and stop the app when ESC key is pressed
And a live sample using a drone camera instead of an USB Camera
Bonus. Viola Jones Face Detection and tracking explained video