OpenCV Pedestrian Detection Project
Machine Learning courses with 100+ Real-time projects Start Now!!
Pedestrian detection using OpenCV is a technique to identify humans in images or videos. This method is widely used in various applications, such as surveillance and self-driving cars.
OpenCV is an open-source computer vision library that provides functions to analyze images and videos. Pedestrian detection using OpenCV is based on simple rectangular features called Haar-like features. It is an easy and efficient way to detect pedestrians and has become a popular research area to improve its accuracy and efficiency.
What is meant by the Pedestrian?
Pedestrians are people who are traveling on foot, such as walking, running, or jogging, on sidewalks, crosswalks, or any other pedestrian-friendly areas. Pedestrians are an essential part of any urban or rural environment, as they provide a cost-effective and sustainable mode of transportation.
Pedestrian safety is crucial, and it is important to ensure that pedestrians have safe and accessible places to walk, as well as protection from vehicular traffic. Pedestrian detection is the process of identifying pedestrians in images or videos, which is a critical component of many computer vision applications, such as autonomous vehicles and surveillance systems.
Cascade in Pedestrian Detection
Cascade in pedestrian detection is a machine learning algorithm used to detect pedestrians in images or videos. It’s trained on positive and negative images to learn to distinguish between them. It scans the image at different scales and locations, applying a series of classifiers in a cascade fashion to quickly and accurately detect pedestrians in real time. Cascade classifiers are widely used in pedestrian detection systems for surveillance, crowd monitoring, and self-driving cars.
Prerequisites for Pedestrian Detection Using OpenCV
It is important to have a solid understanding of the Python programming language and the OpenCV library. Apart from this, you should have the following system requirements.
1. Python 3.7 and above
2. Any Python editor (VS code, Pycharm, etc.)
Download OpenCV Pedestrian Detection Project
Please download the source code of OpenCV Pedestrian Detection Project from the following link: OpenCV Pedestrian Detection Project Code.
Installation
Open windows cmd as administrator
1. To install the opencv library run the command from the cmd.
pip install opencv-python
Let’s Implement It
To implement it follow the below step.
1. First of all we are importing all the necessary libraries that are required during implementation.
import cv2
2. The line cap = cv2.VideoCapture(‘pedestrian.avi’) creates a VideoCapture object in OpenCV that reads video frames from a file called “pedestrian.avi”.
cap = cv2.VideoCapture('pedestrian.avi')
3. This loads a pre-trained cascade classifier called ‘haarcascade_fullbody.xml’. This classifier is designed to detect the full body of a human in an image or video.
cascade_human = cv2.CascadeClassifier('haarcascade_fullbody.xml')
4. Start While loop.
while True:
5. This reads a frame from the video capture object ‘cap’. It returns two values: ‘ret’ which is a boolean indicating if the frame was successfully read or not, and ‘frame’, which is the actual image frame that was read from the video.
ret, frame = cap.read()
6. This code detects human bodies in a video frame by first converting it to grayscale and then using the detectMultiScale() method of the cascade classifier cascade_human. The method returns a list of rectangles that represent the detected human bodies.
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) human_bodies = cascade_human.detectMultiScale(gray, 1.9, 1)
7. It draws a rectangle around each detected human body in the original color frame using the cv2.rectangle() function. The (x, y) coordinates and the width (w) and height (h) of each detected human body are obtained from the human_bodies variable. The rectangle is drawn using the color (255, 0, 0) and a thickness of 2 pixels.
for (x, y, w, h) in human_bodies:
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
8. This code displays the video frame with detected human bodies and waits for a key press. If the key pressed is ‘q’, the program exits and all windows are closed.
cv2.imshow('DataFlair', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
Note:- you have to include steps 5 to 8 in the while loop.
9. release the video capture and close all windows that were opened using OpenCV in the previous code.
cap.release() cv2.destroyAllWindows()
Opencv Pedestrian Detection Output
OpenCV Pedestrian Detection Video Output
Conclusion
Pedestrian detection using OpenCV is an important technology that can improve pedestrian safety in self-driving cars, surveillance systems, and city planning.
OpenCV is a flexible and powerful tool that can be customized to fit different situations. It uses advanced techniques to accurately detect pedestrians in difficult situations like low light, obstacles, and crowded backgrounds. As research continues, OpenCV will get even better at detecting pedestrians, making it safer for people to walk around in the future.
Did we exceed your expectations?
If Yes, share your valuable feedback on Google



