-
Run the following command line in the terminal:
pip install opencv-python numpy supervision tqdm ultralytics
-
Download the YOLOv8 model from the link - https://github.com/ultralytics/assets/releases/download/v8.2.0/yolov8x.pt
-
Copy paste the path of the model in the code
-
Download the
vehicles.mp4
file -
Copy the path of the file
vehicles.mp4
and paste it in the code -
Once that is done run the code and you will get the result video by the name of
vehicles-result.mp4
Input Video:
vehicles.mp4
Output Video:
vehicles-result.mp4
This project uses object detection and tracking techniques to detect vehicles and estimate their speed in a video. The key components include:
-
Video Processing: The code reads and processes video frames from a source video file (
vehicles.mp4
), and a target video (vehicles-result.mp4
) is generated with annotations. -
Object Detection: The YOLOv8 model is employed to detect objects (vehicles) in each video frame. Only objects with a confidence score above a threshold (0.3) and excluding class 0 (person) are considered.
-
Polygon Zone: A source polygon is defined on the video to filter detected objects that fall within this region, ensuring only relevant detections are tracked.
-
Tracking: The ByteTrack tracker is used to track vehicles across frames. The tracker updates with each new detection and stores coordinates of the tracked objects.
-
Speed Estimation: For each tracked object, the code calculates its speed based on the change in position over time (using the coordinates stored by the tracker). The speed is then converted to kilometers per hour (km/h).
-
Annotation: The frame is annotated with bounding boxes around detected vehicles, their tracker IDs, and calculated speeds. Annotations are applied using custom annotators that handle bounding boxes, labels, and traces.
-
Result Video: Finally, the annotated frames are compiled into a target video that shows the detected vehicles with their speeds, providing real-time tracking and speed estimation.
In summary, the code integrates object detection, tracking, and speed estimation to analyze vehicle movement in a video, providing an annotated output with the vehicle IDs and their speeds.