Abhishek Jaiswal — April 1, 2022
Advanced Computer Vision Image Image Analysis Object Detection Python

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

Introduction

This article will discuss how to implement a haar cascade for object detection in OpenCV. In the last article, we discussed real-time object classification. If you haven’t read it yet, the link is here.

Object Detection using Haar Cascade

Source: Link

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.

Haar cascades were first introduced in 2001, and it was one of the most popular object detection algorithms in OpenCV.

What are Haar Cascades?

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
                                                                                   Source: Link

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.

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

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

About the Author

Our Top Authors

Download Analytics Vidhya App for the Latest blog/Article

Leave a Reply Your email address will not be published. Required fields are marked *