This repository contains a Jupyter notebook demonstrating various applications of Haar Cascade classifiers for object detection, primarily focusing on face, eye, and full-body detection in images and videos.
This project showcases the implementation of Haar Cascade classifiers for detecting human features in static images and video streams. It utilizes OpenCV's pre-trained Haar Cascade models to identify faces, eyes, and full bodies.
- OpenCV (cv2)
- NumPy
- Matplotlib
- Requests
- Face detection in images
- Face and eye detection in images
- Full-body detection in images
- Face detection in video streams
- Full-body detection in video streams
- Utility functions for drawing bounding boxes and text labels
The notebook is divided into several sections, each demonstrating a different aspect of object detection:
- Face detection from images
- Face and eye detection from images
- Human full-body detection from images
- Face detection from videos
- Human full-body detection from videos
Each section contains functions that can be called with appropriate parameters to perform the desired detection task.
detect_face(image_path, save_fig, filename)
: Detects faces in a given image.detect_face_and_eye(image_path, save_fig, filename)
: Detects faces and eyes in a given image.detect_human_body(image_path, scale_factor, min_neighbours, save_fig, filename)
: Detects full bodies in a given image.detect_faces_from_video(video_path, scale_factor, min_neighbors, resize_factor, save_video, filename)
: Detects faces in a video stream.detect_human_body_from_video(video_path, scale_factor, min_neighbors, resize_factor, save_video, filename)
: Detects full bodies in a video stream.
This function detects faces in a given image using Haar Cascade classifiers.
image_path
: String, path to the input image file.save_fig
: Boolean, whether to save the resulting figure (default: False).filename
: String, file name to save the figure (if save_fig is True).
- Loads the pre-trained face cascade classifier.
- Reads the input image and converts it to grayscale.
- Detects faces using the
detectMultiScale
method of the face cascade. - Draws rectangles around detected faces.
- Displays the original image and the image with detected faces side by side.
- Optionally saves the resulting figure.
- Uses
cv2.CascadeClassifier
with 'haarcascade_frontalface_default.xml'. - Implements error handling for file loading and face detection.
- Utilizes matplotlib for image display and saving.
This function detects both faces and eyes in a given image.
- Same as
detect_face
function.
- Loads both face and eye cascade classifiers.
- Detects faces in the image.
- For each detected face, it creates a region of interest (ROI) and detects eyes within this region.
- Draws rounded rectangles around faces and rectangles around eyes.
- Displays and optionally saves the result.
- Uses both face and eye Haar Cascades.
- Implements nested detection (eyes within faces).
- Uses custom
draw_rounded_rect
function for aesthetically pleasing face bounding boxes.
In some cases, it may mistakenly identify other facial features as eyes in the image. To ensure that we only detect and draw bounding boxes around the eyes, and not other facial regions such as lips, we can filter the detected eye regions based on their relative vertical positions. Specifically, if the centres of two detected eye-bounding boxes are within a specified vertical range (e.g., ±50 pixels), we can confirm that they are likely to be eyes.
This function detects full human bodies in an image.
As seen in the figure, the haar cascade is less accurate in detecting persons compared to other object detection models (e.g., YOLO, detectron2). It misses out on many persons and sometimes doesn't capture the whole human body.
image_path
: String, path to the input image (can be a URL or local path).scale_factor
: Float, parameter specifying how much the image size is reduced at each image scale.min_neighbours
: Integer, parameter specifying how many neighbors each candidate rectangle should have to retain it.save_fig
andfilename
: Same as previous functions.
- Loads the full-body Haar Cascade classifier.
- Handles both local and URL-based image inputs.
- Detects full bodies in the image.
- Draws rectangles and labels for each detected body.
- Displays and optionally saves the result.
- Uses 'haarcascade_fullbody.xml' for detection.
- Handles URL-based image inputs using the
requests
library. - Implements custom text drawing with background for clear labeling.
4. detect_faces_from_video(video_path, scale_factor, min_neighbors, resize_factor, save_video=False, filename=None)
This function detects faces in a video stream.
video_path
: String, path to the input video file or camera index.scale_factor
andmin_neighbors
: Same as indetect_human_body
.resize_factor
: Float, factor to resize the displayed video frames.save_video
: Boolean, whether to save the processed video.filename
: String, file name to save the processed video.
- Opens the video stream and prepares video writer if saving is requested.
- Processes each frame of the video:
- Detects faces
- Draws rounded rectangles around faces
- Detects eyes within face regions for additional validation
- Displays the processed frames in real-time.
- Optionally saves the processed video.
- Real-time face detection in video streams.
- Uses eye detection to validate face regions.
- Implements video saving functionality.
5. detect_human_body_from_video(video_path, scale_factor, min_neighbors, resize_factor, save_video=False, filename=None)
This function detects full human bodies in a video stream.
Here, the number associated with the body is not the tracker ID; it is only used to display the number of bodies the model detected.
- Same as
detect_faces_from_video
function.
- Opens the video stream and prepares video writer if saving is requested.
- Processes each frame of the video:
- Detects full bodies
- Draws rectangles around detected bodies
- Labels each detected body
- Displays the processed frames in real-time.
- Optionally saves the processed video.
- Real-time full-body detection in video streams.
- Implements labeling for multiple detected bodies.
- Provides options for video resizing and saving.
These functions collectively provide a comprehensive toolkit for detecting human features (faces, eyes, and full bodies) in images and videos using Haar Cascade classifiers. They offer flexibility in input handling, parameter tuning, and output options, making them suitable for various computer vision applications.