Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
What is a Haar-Cascade Classifier?
Feature Detection using Haar Cascades
Step 1: Importing Necessary Libraries
Step 2: Loading pre-trained haar-cascade object classifiers
Step 3: Reading the image
Step 4: Object Detection
Step 5: Plotting results of the detection
Key Takeaways
Last Updated: Mar 27, 2024

Feature Detection with Haar-Cascade

What is a Haar-Cascade Classifier?

We use Haar-Cascades in Machine Learning for Object Detection. Alfred Haar first proposed Haar-Cascade in 1909.

In a grayscale image, each pixel has a value from 0 to 255, where 0 represents total black, and 255 represents a whole white pixel

To detect features from a person’s face, we first obtain the Haar Features using convolutional kernels.


The edge-features kernel can effectively detect the vertical & horizontal edges in an image. The line-features kernel is adequate in determining the lines in an image; a line is basically where light pixels are between dark pixels or vice-versa. Similarly, the four-rectangle kernel is also used to detect some essential features in an image.

After applying the convolutional kernels, the features that we get are the values received by subtracting the sum of pixels under the white rectangles from the pixels under the black rectangles.

Feature Detection using Haar Cascades

This section will detect features like faces, smiles, and eyes in a group picture using Haar Classifiers in OpenCV.

Original Image

Using the CascadeClassifer() method, we can use the already pre-trained models and find features on the person’s face. All the haar-cascades are available at this github-link.

Step 1: Importing Necessary Libraries

import numpy as np
import cv2
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

Step 2: Loading pre-trained haar-cascade object classifiers

smile_cascade = cv2.CascadeClassifier( + 'haarcascade_smile.xml')
face_cascade = cv2.CascadeClassifier( + 'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier( + 'haarcascade_eye.xml')

Step 3: Reading the image

img = cv2.imread('group-photo.jpg')

# Using cvtColor we'll convert the image from RGB colorspace to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

Step 4: Object Detection

# Detecting all the faces in the image
faces = face_cascade.detectMultiScale(gray, 1.1, 2)

# Looping through the faces
for(x, y, w, h) in faces:
    img = cv2.rectangle(img, (x,y), (x+w,y+h), (255,0,0), 3)
    roi_gray = gray[y:y+h, x:x+w]
    roi_color = img[y:y+h, x:x+w]

    # Detecting smile on the face
    smiles = smile_cascade.detectMultiScale(roi_gray, minNeighbors=10)
    for(sx, sy, sw, sh) in smiles:
        cv2.rectangle(roi_color, (sx,sy), (sx+sw,sy+sh), (0,0,255), 3)
    # Detecting eyes on the face
    eyes = eye_cascade.detectMultiScale(roi_gray)
    for(ex, ey, ew, eh) in eyes:
        cv2.rectangle(roi_color, (ex,ey), (ex+ew,ey+eh), (0,255,255), 3)

Step 5: Plotting results of the detection


face_patch = mpatches.Patch(color='blue', label='Faces')
smile_patch = mpatches.Patch(color='red', label='Smiles')
eye_patch = mpatches.Patch(color='yellow', label='Eyes')

plt.legend(handles=[face_patch, smile_patch, eye_patch], loc='lower right', fontsize=10)

imgplot = plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))

Also read, Sampling and Quantization

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job


  1. What are the applications of the OpenCV library?
    The OpenCV library has many use-cases like
    Face Detection
    Object Detection
    Image Processing
    Face Recognition
  2. What are Haar-Cascades?
    We use Harr-Cascades to detect facial expressions from a person’s face. These are classifiers in XML files and store the pre-defined patterns over face segments.
  3. What are the different types of haar-cascades available in OpenCV?
    OpenCV has many haar-cascades corresponding to different parts:

Key Takeaways

Congratulations on finishing the blog!! Below, I have some blog suggestions for you. Go ahead and take a look at these informative articles.

In today’s scenario, more & more industries are adapting to AutoML applications in their products; with this rise, it has become clear that AutoML can be the next boon in the technology. Check this article to learn more about AutoML applications.

Check out this link if you are a Machine Learning enthusiast or want to brush up your knowledge with ML blogs.

If you are preparing for the upcoming Campus Placements, don't worry. Coding Ninjas has your back. Visit this link for a carefully crafted and designed course on-campus placements and interview preparation.

Previous article
Face Detection Using OpenCV with Python
Next article
Eye Ball Tracking with OpenCV
Live masterclass