Experiment No 6

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Aim: To study Detecting and Recognizing Faces

Objective: To Conceptualizing Haar Cascades Getting Haar cascade data Using


Open CV to Perform face detections performing face detection on still images

Theory:
Conceptualizing Haar Cascades:
Haar cascades are a machine learning-based object detection method used to identify
objects in images or video. They were introduced by Viola and Jones in their 2001
paper. The fundamental idea behind Haar cascades is to use a series of progressively
more complex classifiers to quickly eliminate areas in an image that are unlikely to
contain the target object, thus reducing the computational load.

The key components of Haar cascades include:


Haar-like Features: These are simple rectangular filters that are applied to the image
to capture variations in brightness. Haar features are computed at multiple scales and
positions within the image.

Integral Images: To speed up the calculation of Haar-like features, integral images


are used. Integral images allow for the rapid computation of Haar-like feature values
for any image region.

Adaboost: Haar cascades use the Adaboost algorithm to select and weight a subset
of Haar-like features. Adaboost combines weak classifiers (the Haar-like features)
into a strong classifier.

Cascade Classifier: The cascade classifier is composed of multiple stages, each


containing a subset of the Adaboost-trained weak classifiers. The stages are
organized in a way that allows for quick rejection of non-object regions. If a region
passes all stages, it is considered a positive detection.
Getting Haar Cascade Data:
To use Haar cascades for face detection or any other object detection task, you need
pre-trained Haar cascade classifiers. These classifiers are trained on large datasets
and are available for various objects, including faces. You can obtain Haar cascade
data from online sources or use the ones provided by OpenCV. These XML file
contain the information needed for the cascade classifier, including feature
definitions and weights.

Using OpenCV to Perform Face Detection: Performing Face Detection on a Still


Image:
OpenCV (Open Source Computer Vision Library) is a powerful open-source
computer vision and machine learning software library. It provides tools and
functions for various computer vision tasks, including face detection.

To perform face detection on a still image using OpenCV, you typically follow these
steps:
• Load the Image: Read the still image from a file or capture it using a camera.

• Load the Haar Cascade Classifier: Load the pre-trained Haar cascade classifier
for face detection using the cv2.CascadeClassifier function.

• Convert to Grayscale: Convert the input image to grayscale, which simplifies


the face detection process.

• Detect Faces: Use the detectMultiScale method of the cascade classifier to


identify faces in the grayscale image. This method returns the coordinates of
the detected faces.

• Draw Rectangles: Iterate through the detected faces and draw rectangles
around them on the original image.

• Display the Result: Display or save the image with the detected faces marked.
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.

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
haarcascade 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.
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.
Code:
import cv2
from google.colab.patches import cv2_imshow

# Load the pre-trained Haar Cascade for face detection


face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

# Load the image you want to perform face detection on


image_path = ‘/content/s2.jpg’
image = cv2.imread(image_path)

# Convert the image to grayscale (Haar cascades work on grayscale images) gray =
cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Perform face detection


faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5,
minSize=(30, 30))

# Draw rectangles around detected faces


for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)

# Display the result


cv2_imshow(image)
Input:

Output:
Conclusion:

Using Python and the OpenCV library, we successfully demonstrated face


detection in Google Colab. Face detection is a fundamental computer vision
task, and we used a pre-trained Haar cascade classifier in this tutorial to
identify and highlight faces in an image.

Loading the image, importing the necessary libraries, loading the pre-trained
face detection cascade, applying the cascade to the image to detect faces,
drawing rectangles around the detected faces, and finally displaying or saving
the output image are the key steps in this process.

This example lays the groundwork for more advanced computer vision tasks
like facial recognition, emotion detection, and others.

You might also like