Skip to content

ankitsharma-tech/Face-Detection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Face Detection in Python Using OpenCV

What is OpenCV?

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

Applications of OpenCV

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

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:

  1. Haar Cascade Classifier
  2. LBP Cascade Classifier

We will explore both in this tutorial.


Haar Cascade Classifier

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).

Algorithm Stages

  1. 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.
  2. Creating an Integral Image

    • To speed up calculations, an integral image is used.
    • This reduces computation to only four pixels instead of all.
  3. Adaboost

    • Not all computed features are relevant.
    • Adaboost selects the most important features for classification.
  4. Cascading Classifiers

    • Features are grouped into stages.
    • Each stage filters out non-facial regions.
    • Only regions passing all stages are classified as faces.

LBP Cascade Classifier

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.

Algorithm Steps

  1. LBP Labelling

    • Each pixel is assigned a binary label.
  2. 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.
  3. Adaboost Learning

    • Gentle Adaboost is applied to remove redundant information.
    • A strong classifier is built from useful features.
  4. Cascade of Classifiers

    • Classifiers are arranged in a cascade.
    • Each stage filters sub-regions of the image.
    • Only facial regions survive all stages.

Steps for Face Detection

  1. Load the Haar Cascade Face Algorithm
  2. Initialize the Camera
  3. Read a Frame from the Camera
  4. Convert the frame to Grayscale
  5. Obtain Face Coordinates using the classifier
  6. Draw a Rectangle around detected faces
  7. Display the Output Frame

Output

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.