OpenCV - Object Detection
Face Detection
HARR Cascade Classifiers
We can extract features from an image and use those features to classify objects.
What is it?
HARR Cascade Classifiers is an object detection method that inputs Haar features into a series of classifiers (cascade) to identify objects in an image. They are trained to identify one type of object, however, we can use several of them in parallel e.g. detecting eyes and faces together.
Advantage and Disadvantage
- Can only identify one type of object
- Can be used in parallel
HAAR Classifiers Explained
HAAR Classifiers are trained using lots of positive images and negative images.
positive images = images with the object present
negative images = images without the object present
We then extract features using sliding windows of rectangular blocks. These features are single valued and are calculated by subtracting the sum of pixel intensities under the white rectangles from the black rectangles. However, this is a ridiculous number of calculations, even for a base window of 24 x 24 pixels (180,000 features generated). So the researchers devised a method called Integral Images that computed this with four array references.
However, they still had 180,000 features and the majority of them added no real value.
Boosting was then used to determine the most informative features, with Freund & Schapire’s AdaBoost the algorithm of choice due to its ease of implementation. Boosting is the process by which we use weak classifiers to build strong classifiers, simply by assigning heavier weighted penalties on incorrect classifications. Reducing the 180,000 features to 6000, which is still quite a bit features.
Think about this intuitively, if of those 6000 features, some will be more informative than others. What if we used the most informative features to first check whether the region can potentially have a face (false positives will be no big deal). Doing so eliminates the need for calculating all 6000 features at once.
This concept is called the Cascade of Classifiers - for face detection, the Viola Jones method used 38 stages.
How to use
There are pre-trained classifier can be found here
They are stored as .xml files.
A small sample program to use haarcascade_frontalface_alt.xml
.
1 | import io |
Lets detect eye as well, then extract it.
1 | import cv2 |
Other Objects Detection
Where HAAR Classifiers, they also provide different xml files for other objects.
Car Detection
!empty() in function detectMultiScale
Note: You might need to include the full path to use the xml file.
1 | import cv2 |
Pedistrian Detection
!empty() in function detectMultiScale
Note: You might need to include the full path to use the xml file.
1 | import cv2 |
Face Analysis and Filters
Let say we want to make a face-swap app. Just the the one on snapchat.
Although HARR Cascade Classifiers provide excellent results in Face Detection, for face-swap, We need other solution.
We need to detect Facial landmarks.
But it is still hard to make a face-swap app.
What was so hard about that?
You need to:
- Identifying Facial Features
- Warping the image to fit the new and different facial expression
- Color Matching
- Creating seamless borders on the edges of the new swapped face
Dlib
Relately easy way.
Code Implementation
With this code, you can locate the some points on a human face.
1 | import cv2 |
Your first Face-swapping App (Image Only)
Things you need to know :
Facial Landmarks Number Order
- MOUTH_POINTS = 48 to 61
- RIGHT_BROW_POINTS = 17 to 21
- LEFT_BROW_POINTS = 22 to 27
- RIGHT_EYE_POINTS = 36 to 42
- LEFT_EYE_POINTS = 42 to 48
- NOSE_POINTS = 27 to 35
- JAW_POINTS = 0 to 17
Code Implementation
1 | import cv2 |
Code Explained
Your first Face-swapping App (LIVE!)
Everyone could become Donald Trump.
1 | import cv2 |