Guide to Haar Cascade Algorithm with Object Detection Example

Abhishek Jaiswal Last Updated : 14 Oct, 2024
6 min read

Introduction

Discover object detection with the Haar Cascade algorithm using OpenCV. Learn how to employ this classic method for detecting objects in images and videos. Explore the underlying principles, step-by-step implementation, and real-world applications. From facial recognition to vehicle detection, grasp the essence of Haar Cascade and OpenCV’s role in revolutionizing computer vision. Whether you’re a novice or an expert, this article will equip you with the skills to harness the potential of object detection in your projects.

Object Detection using Haar Cascade

This article was published as a part of the Data Science Blogathon.

Why Use Haar Cascade Algorithm for Object Detection?

Identifying a custom object in an image is known as object detection. This task can be done using several techniques, but we will use the haar cascade, the simplest method to perform object detection in this article.

What is Haar Cascade Algorithm?

Haar cascade is an algorithm that can detect objects in images, irrespective of their scale in image and location.

This algorithm is not so complex and can run in real-time. We can train a haar-cascade detector to detect various objects like cars, bikes, buildings, fruits, etc.

Haar cascade uses the cascading window, and it tries to compute features in every window and classify whether it could be an object. For more details on its working, refer to this link.

haar Cascades

Sample haar features traverse in window-sized across the picture to compute and match features.

Haar cascade works as a classifier. It classifies positive data points → that are part of our detected object and negative data points → that don’t contain our object.

  • Haar cascades are fast and can work well in real-time.
  • Haar cascade is not as accurate as modern object detection techniques are.
  • Haar cascade has a downside. It predicts many false positives.
  • Simple to implement, less computing power required.

Pre-trained Haar Cascades

The OpenCV library manages a repository containing all popular haar cascades that can be used for:

  • Human face detection
  • Eye detection
  • Nose / Mouth detection
  • Vehicle detection

Haar cascades are XML files that can be used in OpenCV to detect specified objects.

Implementing Haar-cascades in OpenCV

If you find your target object haar-cascade available in the pre-trained repository provided by OpenCV, you need to download the pre-trained XML file.

Installing OpenCV in Python

Installing OpenCV is easy using the pip installer.

!pip install opencv-python
#---OR ---
!pip install opencv-contrib-python

Loading Haar Cascade in OpenCV

We can load any haar-cascade XML file using cv2.CascadeClassifier function.

face_detector=cv2.CascadeClassifier(‘haarcascade_frontalface_default.xml’)
eye_dectector = cv2.CascadeClassifier(‘haarcascade_eye.xml’)

Once cascade is loaded in OpenCV, we can call the detector function.

results = face_detector.detectMultiScale(gray_img, scaleFactor=1.05,minNeighbors=5,minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE)

results It lists coordinates (x, y, w,h) of bounding boxes around the detected object.

Hit Run to see the output

Parameters in detectMultiScale

  • scaleFactor – This tells how much the object’s size is reduced in each image.
  • minNeighbors – This parameter tells how many neighbours each rectangle candidate should consider.
  • minSize — This signifies the minimum possible size of an object to be detected. An object smaller than minSize would be ignored.

Note : For detection, we must pass a gray_image , scaleFactor, and minNeighbors. Other parameters are optional.

Simple Detection of the Human Face

This is the first example of object detection using a haar cascade, where we will detect human faces from a picture using a pre-trained haar cascade.

Before starting, first, download the pre-trained haar cascade file for frontal face detection using this link.

import numpy as np
import cv2

#---loading haarcascade detector---face_detector=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
#---Loading the image-----
img = cv2.imread('team_india.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_detector.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
    cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
  
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
  • faces It contains the coordinates of bounding boxes around detected faces.
  • detectMultiScale This method only accepts grayscale pictures.
  • cv2.rectangle This method draws rectangles (bounding boxes around the detected face).
  • scaleFactor = 1.3 We can try a number between 1 and 2 and later fine-tune it.

Hierarchical Detection

Haar cascade can detect multiple objects within a single frame in a hierarchical manner.

Imagine we have to detect the faces and eyes of humans. To proceed with this problem, we need to follow the steps:

  • Detect Human faces
  • For every face, crop faces and pass it for eye detection
  • after getting the coordinates of the eyes (ex,ey,ew,eh) draw bounding boxes for the eyes on the original picture
  • draw bounding box for faces using coordinates(x,y,w,h) on the original picture.
import numpy as np
import cv2

face_detector=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_detector = cv2.CascadeClassifier('haarcascade_eye.xml')

img = cv2.imread('.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_detector.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
#-----roi_gray is the cropped detected face in grayscale
# --- roi_color is the cropped detected face in color
eyes = eye_detector.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)

cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
  • roi_gray : region of interest(cropped face) in grayscale
  • roi_color : region of interest(cut face) in colour
                                                                                                                                      Source: Link

Object Detection in Real-time

So far, we have worked on a single image. The haar-cascade algorithm is light and works in real-time with a perfect frame per second.

We will be using OpenCV video cam feed input to take images in real-time (video).

import cv2
face_detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_dectector = cv2.CascadeClassifier('haarcascade_eye.xml')
# reading the input image now
cap = cv2.VideoCapture(0)
while cap.isOpened():
    _, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_detector.detectMultiScale(gray,1.1, 4 )
    for (x,y, w, h) in faces:
    cv2.rectangle(frame, pt1 = (x,y),pt2 = (x+w, y+h), color = (255,0,0),thickness =  3)
    roi_gray = gray[y:y+h,x:x+w]
    roi_color = frame[y:y+h, x:x+w]
    eyes = eye_dectector.detectMultiScale(roi_gray)
    for (ex,ey, ew, eh) in eyes:
        cv2.rectangle(roi_color, (ex,ey), (ex+ew, ey+eh), (0,255,0), 5)
    cv2.imshow("window", frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

frame.release()

This output the face and eyes detection in real-time.

Limitation of Haar Cascade

We have seen haar cascade performing very well, but there are several limitations of haar cascade.

  • High false-positive detection
  • Less accurate than deep learning-based techniques
  • Manual tuning of parameters.
  • Training haar cascade on a custom object is not easy.

Conclusion

In this article, we have discussed the working of haar cascade and how to implement haar cascade for object detection using OpenCV in python. We downloaded a pre-trained haar cascade file for frontal face detection and eyes detection and detected faces and eyes successfully in a single video camera feed.

We also discussed the limitation of the haar cascade algorithm still widely used when we can tolerate some false-positive detection.

  • Manual tuning of the haar cascade detection parameter can solve the problem of false-positive detection.
  • Using other object detection algorithm like YOLO, SSD promise better accuracy with good FPS.
  • Training a haar cascade for a custom object is not very efficient.

The following article will discuss object detection using deep learning algorithms.

Frequently Asked Questions

Q1. What is the Haar cascade algorithm and CNN algorithm? 

A. The Haar cascade algorithm is an ML object detection method using Haar-like features. CNN is a deep learning approach for image analysis and recognition tasks.

Q2. How does Haar Cascade training work? 

A. Haar Cascade training involves creating a classifier to detect specific features by applying filters on image windows and adjusting weights during iterations to minimize errors.

Q3. Why do we use the cascade algorithm? 

A. Cascade algorithms, like the Haar cascade, provide efficient object detection by sequentially applying multiple stages of classifiers, quickly rejecting non-object regions and saving computation.

Q4. What is the cascade algorithm in machine learning?

A. A cascade algorithm is a multi-stage approach where each stage consists of a classifier. Examples include the Haar cascade for face detection and the Viola-Jones algorithm.

The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.

A data enthusiast exploring the leading technologies related to the data

Responses From Readers

Clear

Congratulations, You Did It!
Well Done on Completing Your Learning Journey. Stay curious and keep exploring!

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details