Last Updated : 12 Jul, 2025
Haar Cascade classifiers are a machine learning-based method for object detection. They use a set of positive and negative images to train a classifier, which is then used to detect objects in new images.
In this article we will learn how to perform detection using Haar Cascades.
Code ImplementationWe will go through the step-by-step procedure to implement object detection using Haar Cascades.
1. Importing required LibrariesHere, we will use Numpy, OpenCV and Matplotlib.
Python
import cv2
import numpy as np
import matplotlib.pyplot as plt
2. Loading Haar Cascade Classifiers
Next we will load the pre-trained Haar Cascade classifiers for detecting faces and eyes. You can download these classifier from this link.
Python
face_cascade = cv2.CascadeClassifier("/content/haarcascade_frontalface_default.xml")
eye_cascade = cv2.CascadeClassifier('/content/haarcascade_eye.xml')
3. Creating Function to Detect Faces
Now we’ll create a function adjusted_detect_face
()
to detect faces in an image. This function uses the face cascade classifier to identify face rectangles and draws rectangles around the detected faces.
def adjusted_detect_face(img):
face_img = img.copy()
face_rect = face_cascade.detectMultiScale(face_img, scaleFactor=1.2, minNeighbors=5)
for (x, y, w, h) in face_rect:
cv2.rectangle(face_img, (x, y), (x + w, y + h), (255, 255, 255), 10)
return face_img
4. Creating Function to Detect Eyes
Similarly we create a function detect_eyes
()
to detect eyes using the eye cascade classifier.
def detect_eyes(img):
eye_img = img.copy()
eye_rect = eye_cascade.detectMultiScale(eye_img, scaleFactor=1.2, minNeighbors=5)
for (x, y, w, h) in eye_rect:
cv2.rectangle(eye_img, (x, y), (x + w, y + h), (255, 255, 255), 10)
return eye_img
5. Loading a Image
Now let’s load an image and apply both face and eye detection on it. The image which we are using can be downloaded from this link.
Python
img = cv2.imread('/content/andrew.jpg')
img_copy1 = img.copy()
img_copy2 = img.copy()
img_copy3 = img.copy()
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
Output:
After running the code you will see three images Face Detection, Eyes Detection and Face and Eyes Detection. These images will also be saved as face.jpg
, eyes.jpg
and face+eyes.jpg
respectively.
Face Detection:
Python
face = adjusted_detect_face(img_copy1)
plt.imshow(cv2.cvtColor(face, cv2.COLOR_BGR2RGB))
plt.show()
cv2.imwrite('face.jpg', face)
Output:
face detectionEyes Detection:
Python
eyes = detect_eyes(img_copy2)
plt.imshow(cv2.cvtColor(eyes, cv2.COLOR_BGR2RGB))
plt.show()
cv2.imwrite('eyes.jpg', eyes)
Output:
eyes detectionFace and Eyes Detection:
Python
eyes_face = adjusted_detect_face(img_copy3)
plt.imshow(cv2.cvtColor(eyes_face, cv2.COLOR_BGR2RGB))
plt.show()
cv2.imwrite('face+eyes.jpg', eyes_face)
Output:
face and eyes detectionIn this article, we explored Haar Cascades for object detection. By using pre-trained XML files we can detect different objects with minimal setup. Moreover the flexibility of Haar Cascades allows us to create custom XML files tailored to detect specific objects offering a wide range of computer vision applications.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4