Python: Measuring the speed of cars on a highway using neural networks (YOLOv8)
Introduction
In the field of artificial neural networks and computer vision, object identification and tracking are extremely important technologies, which can be applied to countless projects, many of which may not be very obvious, such as the use of these algorithms to measure distances or the speed of objects. . Therefore, I present to you a Python project that aims to measure the speed of cars on a highway with YOLOv8 with the aim of giving you an idea of how these algorithms can be used in everyday solutions.
The code
from ultralytics import YOLO
from ultralytics.solutions import speed_estimation
import cv2
model = YOLO("yolov8s.pt")
names = model.model.names
cap = cv2.VideoCapture("/pathFreeway_night.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
video_writer = cv2.VideoWriter("/path/speed_estimation.avi",
cv2.VideoWriter_fourcc(*'mp4v'),
fps,
(w, h))
line_pts = [(0, 400), (1280, 400)]
speed_obj = speed_estimation.SpeedEstimator()
speed_obj.set_args(reg_pts=line_pts,
names=names,
view_img=True)
while cap.isOpened():
success, im0 = cap.read()
if not success:
print("Video frame is empty or video processing has been successfully completed.")
break
tracks = model.track(im0, persist=True, show=False)
im0 = speed_obj.estimate_speed(im0, tracks)
video_writer.write(im0)
cap.release()
video_writer.release()
cv2.destroyAllWindows()
Quick explanation of the code
Video source
A good start to start a project is to obtain a video on YouTube that shows a static camera, that is, one that does not move on its axis and always shows the same portion of the street or road (I indicate what I used in the example (‘https: //www.youtube.com/watch?v=-59Mr4UTgNg'), this is important to avoid errors in accuracy. Another important point is the choice of a library capable of processing the video providing frame by frame the images for inference to be carried out by the YOLO model, which is the next step, for this I recommend the Opencv library.
Identifying the cars
To identify the objects I used the pre-trained YOLO algorithm that can be obtained from the ultralytics library. This algorithm allows real-time identification of objects of interest with good accuracy.
model = YOLO(“yolov8s.pt”)
In this case, the ‘small’ model (letter s) proved to be more than sufficient.
Applying tracking
Still in the YOLO model, a tracking algorithm is incorporated that aims to continuously monitor and follow the movement of a specific object through successive frames. Its implementation is simple as we see below:
tracks = model.track(im0, persist=True, show=False)
Here we incorporate a function directly into the object identification model.
Defining an area
An important step for the project is to define the area in which the object must cross so that the crossing time of this point can be measured, from this reference point we calculate the speed.
To do this, we define coordinates (x and y points) where this region should begin and end, as follows:
line_pts = [(0, 400), (1280, 400)] #plot of a line that crosses the screen from one side to the other
This region is passed as an argument to the function that will be responsible for calculating the speed:
speed_obj.set_args(reg_pts=line_pts, names=names, view_img=True)
How does ‘SpeedEstimator’ calculate speed?
The function processes the frames by storing the tracking positions over time, so the speed of each detected object is calculated by comparing the current position in relation to the previous position within the defined region, allowing the speed of the object to be estimated through the time that it takes to move in this area, following a very well-known term in physics:
v = Δs/Δt
Where, v is the speed, Δs is the displacement (distance) and Δt is the time interval.
Result
Please, don’t forget to tip! =)