OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning library.
It is a BSD-licensed product, free for both business and academic purposes.
The library provides more than 2500 algorithms, including:
- Machine learning tools for classification and clustering
- Image processing and vision algorithms
- Basic algorithms and drawing functions
- GUI and I/O functions for images and videos
Some common applications of OpenCV include:
- Face detection
- Object recognition
- Extracting 3D models
- Camera calibration
- Image processing
- Motion analysis
OpenCV is written in C/C++ but has interfaces for C++, C, Python, and Java. It supports Windows, Linux, Mac OS, iOS, and Android. It is optimized for computational efficiency and designed for real-time applications, leveraging multi-core processing.
Face detection has gained significant attention due to its real-time applications.
However, it is a challenging task for machines because of variations such as:
- Pose differences (front, side, angled views)
- Occlusion (partially hidden faces)
- Image orientation
- Lighting/illumination changes
- Facial expressions
OpenCV provides many pre-trained classifiers for face, eyes, and smile detection.
For face detection specifically, two classifiers are commonly used:
- Haar Cascade Classifier
- LBP Cascade Classifier
We will explore both in this tutorial.
The Haar Cascade Classifier is a machine-learning–based approach proposed by Paul Viola and Michael Jones.
It uses a cascade function trained on positive images (with faces) and negative images (without faces).
-
Haar Feature Selection
- Haar features are computed on subsections of the image.
- They calculate differences in pixel intensity between adjacent regions.
- A large number of Haar-like features are required for facial recognition.
-
Creating an Integral Image
- To speed up calculations, an integral image is used.
- This reduces computation to only four pixels instead of all.
-
Adaboost
- Not all computed features are relevant.
- Adaboost selects the most important features for classification.
-
Cascading Classifiers
- Features are grouped into stages.
- Each stage filters out non-facial regions.
- Only regions passing all stages are classified as faces.
The Local Binary Pattern (LBP) classifier is based on texture descriptors.
Since the human face is composed of micro-texture patterns, LBP features help distinguish faces from non-faces.
-
LBP Labelling
- Each pixel is assigned a binary label.
-
Feature Vector
- The image is divided into sub-regions.
- A histogram of labels is created for each sub-region.
- These histograms are concatenated into one large feature vector.
-
Adaboost Learning
- Gentle Adaboost is applied to remove redundant information.
- A strong classifier is built from useful features.
-
Cascade of Classifiers
- Classifiers are arranged in a cascade.
- Each stage filters sub-regions of the image.
- Only facial regions survive all stages.
- Load the Haar Cascade Face Algorithm
- Initialize the Camera
- Read a Frame from the Camera
- Convert the frame to Grayscale
- Obtain Face Coordinates using the classifier
- Draw a Rectangle around detected faces
- Display the Output Frame
The detected face(s) will be highlighted in the video feed.
To view the output, check the generated media file or run the script live.