Face Detection using Haar-Cascade using Python
This article was published as a part of the Data Science Blogathon.
Hey Folks !!
In this article, we will discuss implementing a face detector using Haar Cascade in OpenCV python. In the last article, we talked about real-time object classification; if you haven’t checked it yet, the link is here.
Identifying a given object in an image is known as object detection. This task can be achieved using several techniques, but in this article, we will use haar cascade with pre-trained XML files. It’s the simplest method to perform object detection.
Haar cascades have been used for object detection on low-edge devices, and it was one of the most popular object detection algorithms in OpenCV.
Haar Cascade is not much computation-heavy; hence it is popular for small devices with small computation power.
What is Haar Cascade, and how it works?
Haar Cascade is a feature-based object detection algorithm to detect objects from images. A cascade function is trained on lots of positive and negative images for detection.
The algorithm does not require extensive computation and can run in real-time. We can train our own cascade function for custom objects like animals, cars, bikes, etc.
Haar Cascade can’t be used for face recognition since it only identifies the matching shape and size.
Haar cascade uses the cascade function and cascading window. It tries to calculate features for every window and classify positive and negative. If the window could be a part of an object, then positive, else, negative. For more in-depth knowledge of its working, refer to this link.
Haar cascade can be understood as a binary classifier. It assigns positive to those cascade windows that can be a part of our object . and negative to those windows that can’t be a part of our object.
- Haar cascades can work in real-time. It blazing fast.
- Haar cascade is not much accurate compared to modern object detection algorithms.
- It detects many false positives. This can be tuned to some extent but can’t be completely removed.
- It is very simple to implement.
- The biggest downside of Haar Cascade is its false negative detection.
Pre-trained Haar Cascades
There are lots of pre-trained haar cascade files that make implementation super easy. We can also train our own haar cascade, but that needs big data to train on.
The OpenCV library manages a repository on GitHub for all popular haar cascades pre-trained files that can be used for various object detection tasks, for example:
- Human face detection
- Eye detection
- Vehicle detection
- Nose / Mouth detection
- body detection
- license plate detection
Haar cascade stores its features in an XML file; these files can be directly loaded in OpenCV for object detection using Haar cascade.
Implementing Haar-cascades in OpenCV
If you are working with any of the pre-trained object detection available in the repository provided by OpenCV, you only need to download the pre-trained XML file.
Installing OpenCV in Python
OpenCV can be installed using the pip package manager in python.
!pip install opencv-python #---OR --- !pip install opencv-contrib-python
Loading Haar-cascade in OpenCV
We can load haar-cascade XML files using
face_detector=cv2.CascadeClassifier(‘haarcascade_frontalface_default.xml’) eye_dectector = cv2.CascadeClassifier(‘haarcascade_eye.xml’)
We can call the detector function once the XML file is loaded.
results = face_detector.detectMultiScale(gray_img, scaleFactor=1.15,minNeighbors=5,minSize=(34, 35), flags=cv2.CASCADE_SCALE_IMAGE)
results It is the list of bounding box coordinates (x,y,w,h) around the detected object.
scaleFactor– This tells how much the object’s size is reduced to the original image.
minNeighbors– This parameter tells how many neighbors should contribute in a single bounding box.
minSize— This signifies the minimum possible size of the object in our image. if our object is smaller than the minSize it will be ignored.
Note : For Object detection, we must use a
scaleFactorther parameters are not necessary.
Face Detection of the Humans
Let’s take the first example of object detection using a pre-trained haar cascade, where we will detect human faces from a picture using Python.
Download the cascade file for face detection using this link.
import numpy as np import cv2
#---loading the Haar Cascade detector using CascadeClassifier---face_detector=cv2.CascadeClassifier('haarcascade_frontalface_default.xml') #---Loading the image from local ----- img = cv2.imread('team_india.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) results = face_detector.detectMultiScale(gray, 1.3, 5) for (x,y,w,h) in results: cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2) cv2.imshow('img',img) cv2.waitKey(0) cv2.destroyAllWindows()
resultsIt contains the coordinate of the Bounding boxes in the picture.
detectMultiScaleThis method only works on grayscale pictures.
cv2.rectangleThis OpenCV method allows us to draw rectangles after passing the coordinates.
scaleFactor = 1.3FineTuning parameter from 1 to 2.
Haarcascade supports Hierarchical detection, which means the Haar cascade can be able to detect multiple objects within a single frame in a hierarchical manner.
Suppose we have to detect the faces and eyes of humans. To proceed with the task, we need to follow these steps.
- Detect Faces
- For every face, crop faces and forward them for eye detection
- After finding the coordinates of the eyes (
ex,ey,ew,eh) draw a bounding box around the eyes in the original picture.
- draw a bounding box around faces using coordinates(
x,y,w,h) on the original picture.
import numpy as np import cv2
face_detector1=cv2.CascadeClassifier('haarcascade_frontalface_default.xml') eye_detector1 = cv2.CascadeClassifier('haarcascade_eye.xml')
img = cv2.imread('uman.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces_result = face_detector.detectMultiScale(gray, 1.3, 5) for (x,y,w,h) in faces_result: 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] 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()
Implementing Face Detection in Real Time
Object detection using Haar Cascade can be used in OpenCV video stream, we only need to read a video or camera feed in OpenCV, and the rest of the thing will be the same.
A Video feed is a sequence of frames so that the code will be the same as the single frame. Due to its light computation requirements, Haar Cascade runs at a good time per second.
We will read OpenCV video cam feed input to take images in real-time.
import cv2 face_detector1 = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') eye_dectector1 = 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_detector1.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_dectector1.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
The bounding boxes for the eyes and faces will be real-time and change for every frame.
Limitation of Haar Cascade Face Detection
Haar Cascade is still very popular for some objects like faces, cars, etc., where the object is easily distinguishable.
Haar Cascade can’t work for deep object detection like types of grain, etc.
There are a few Limitations of the Haar Cascade Algorithm-
- Lower Accuracy compared to modern object detectors.
- High False-positive detection.
- Manual tuning of parameters is required.
- Training haar cascade for the custom objects is not easy at all.
In this article, we have talked about the working of the haar cascade and how to implement the haar cascade for object detection using OpenCV in python. We used a pre-trained haar cascade file for face detection and eyes detection, and then we performed the same operation in real time.
We also talked about the limitation of the haar cascade algorithm, why it is still widely used, and why it is so fast.
- The false positive rate can be fixed using manual parameter tuning.
- YOLO, SSD, and other deep learning object detection algorithms promise better accuracy.
- Training customer haar cascade is time-consuming and inefficient.
Thanks for reading, If you still have any queries feel free to write me on LinkedIn.
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.