A Python script is designed to count vehicles in a video using object detection and tracking. It begins by loading the YOLOv3 deep learning model, which is trained to recognize various objects, including vehicles. The model configuration and weights are loaded from external files, and the class names it can detect are read from a file called coco.names. . The script also sets up the video input, reading frames from a file named traffic.mp4, and calculates the frame dimensions to help with object positioning and drawing.
A horizontal line is drawn across the video frame to act as a counting line. Vehicles that cross this line will be counted. The script initializes several data structures to track objects across frames, including dictionaries for storing tracker positions and histories, a set to keep track of already-counted vehicle IDs, and a counter to assign new IDs to detected vehicles. Only specific vehicle types like cars, buses, and motorcycles are considered for counting, which helps filter out irrelevant objects.
For each frame in the video, the script processes the image using YOLOv3 to detect objects. It converts the frame into a blob, feeds it into the neural network, and retrieves the output layers. It then filters the detections based on confidence scores and whether the detected object is a vehicle. For each valid detection, it calculates the bounding box and center coordinates, which are used for tracking and drawing purposes. Non-maximum suppression is applied to remove overlapping boxes and keep the most confident ones.
The script then attempts to match each detected vehicle with an existing tracker by comparing the current position with previously stored positions. If a match is found within a certain distance, it reuses the tracker ID; otherwise, it assigns a new one. It draws bounding boxes and labels on the frame for each detected vehicle, including the object type and its unique ID. It also updates the tracking history for each vehicle to determine if it has crossed the counting line.
The GitHub link is attached below where we can view the code
GitHub – afsal-jobinandjismi/Vehile-count-detection