In this chapter, you learned how to apply OpenCV’s pre-trained Haar cascades to detect the location of faces in images.
Haar cascades are all called Viola-Jones detectors, named after the researchers who first introduced the method in their 2001 paper, Rapid Object Detection using a Boosted Cascade of Simple Features.
However, Haar cascades, while easy to use once they are trained, come with a number of drawbacks.
In the remainder of this supplementary material, I’ve detailed some of these drawbacks, provided suggestions for alternatives to Haar cascades, and included examples of other pre-trained Haar detectors that you might find useful.
My face(s) are not being detected?
If you apply the code detailed in this chapter to your own images, you might run into circumstances where faces are not detected in your images — even though the faces are clearly visible.
So, what gives?
While Haar cascades are quite fast and can obtain decent accuracy, they have two prominent shortcomings.
The first is parameter tuning. You’ll (likely) need to tweak the parameters of the detectMultiScale
function on a per-image basis. This can be a real pain, especially if you are looking to bulk process a dataset of images and cannot manually inspect the output of each face detection.
The second shortcoming is that Haar cascades can be highly prone to false positives, meaning that faces are detected when there really aren’t any there!
Again, this problem can be fixed by tuning the parameters of detectMultiScale
on a case-by-case basis.
Alternatives to Haar cascades
If you find yourself in a situation where either:
- Haar cascades are not giving you your desired level of accuracy.
- You need to train your own custom object detector.
I would suggest using the HOG + Linear SVM framework, utilizing an image pyramid and sliding windows, you can detect objects in images at various scales and locations.
Inside the PyImageSearch Gurus course, I demonstrate how to implement the HOG + Linear SVM framework to detect cars, stop signs, and faces in images:
https://www.pyimagesearch.com/pyimagesearch-gurus/
This framework can also be easily extended to train your own detectors from scratch.
Other popular pre-trained detectors
The OpenCV repository contains a number of pre-trained Haar cascades in the data/haarcascades
directory on GitHub:
https://github.com/opencv/opencv/tree/master/data/haarcascades
Of particular interest, you might want to learn how to detect cats in images:
Finally, OpenCV also ships out-of-the-box with a pre-trained HOG detector to detect pedestrians in images: