Improve speed estimation using track history and region-free calculation (#20344)

Signed-off-by: Muhammad Rizwan Munawar <muhammadrizwanmunawar123@gmail.com>
Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
Co-authored-by: Laughing <61612323+Laughing-q@users.noreply.github.com>
Co-authored-by: Laughing-q <1185102784@qq.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
This commit is contained in:
Muhammad Rizwan Munawar 2025-04-30 11:36:11 +05:00 committed by GitHub
parent fba1b3f513
commit efdafa9c45
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 51 additions and 56 deletions

View File

@ -40,7 +40,7 @@ keywords: Ultralytics YOLO11, speed estimation, object tracking, computer vision
???+ warning "Speed is an Estimate"
Speed will be an estimate and may not be completely accurate. Additionally, the estimation can vary depending on GPU speed and environmental factors.
Speed will be an estimate and may not be completely accurate. Additionally, the estimation can vary on camera specifications and related factors.
!!! example "Speed Estimation using Ultralytics YOLO"
@ -53,8 +53,8 @@ keywords: Ultralytics YOLO11, speed estimation, object tracking, computer vision
# Pass a source video
yolo solutions speed source="path/to/video.mp4"
# Pass region coordinates
yolo solutions speed region="[(20, 400), (1080, 400), (1080, 360), (20, 360)]"
# Adjust meter per pixel value based on camera configuration
yolo solutions speed meter_per_pixel=0.05
```
=== "Python"
@ -71,14 +71,14 @@ keywords: Ultralytics YOLO11, speed estimation, object tracking, computer vision
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("speed_management.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
# speed region points
speed_region = [(20, 400), (1080, 400), (1080, 360), (20, 360)]
# Initialize speed estimation object
speedestimator = solutions.SpeedEstimator(
show=True, # display the output
model="yolo11n.pt", # path to the YOLO11 model file.
region=speed_region, # pass region points
fps=fps, # adjust speed based on frame per second
# max_speed=120, # cap speed to a max value (km/h) to avoid outliers
# max_hist=5, # minimum frames object tracked before computing speed
# meter_per_pixel=0.05, # highly depends on the camera configuration
# classes=[0, 2], # estimate speed of specific classes.
# line_width=2, # adjust the line width for bounding boxes
)
@ -107,7 +107,7 @@ keywords: Ultralytics YOLO11, speed estimation, object tracking, computer vision
Here's a table with the `SpeedEstimator` arguments:
{% from "macros/solutions-args.md" import param_table %}
{{ param_table(["model", "region"]) }}
{{ param_table(["model"]) }}
The `SpeedEstimator` solution allows the use of `track` parameters:
@ -138,7 +138,6 @@ video_writer = cv2.VideoWriter("speed_estimation.avi", cv2.VideoWriter_fourcc(*"
# Initialize SpeedEstimator
speedestimator = solutions.SpeedEstimator(
region=[(0, 360), (1280, 360)],
model="yolo11n.pt",
show=True,
)

View File

@ -1,6 +1,7 @@
# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
from time import time
from collections import deque
from math import sqrt
from ultralytics.solutions.solutions import BaseSolution, SolutionAnnotator, SolutionResults
from ultralytics.utils.plotting import colors
@ -15,12 +16,10 @@ class SpeedEstimator(BaseSolution):
Attributes:
spd (Dict[int, float]): Dictionary storing speed data for tracked objects.
trkd_ids (List[int]): List of tracked object IDs that have already been speed-estimated.
trk_pt (Dict[int, float]): Dictionary storing previous timestamps for tracked objects.
trk_pp (Dict[int, Tuple[float, float]]): Dictionary storing previous positions for tracked objects.
region (List[Tuple[int, int]]): List of points defining the speed estimation region.
track_line (List[Tuple[float, float]]): List of points representing the object's track.
r_s (LineString): LineString object representing the speed estimation region.
trk_hist (Dict[int, float]): Dictionary storing the object tracking data.
max_hist (int): maximum track history before computing speed
meters_per_pixel (float): Real-world meters represented by one pixel (e.g., 0.04 for 4m over 100px).
max_speed (int): Maximum allowed object speed; values above this will be capped at 120 km/h.
Methods:
initialize_region: Initializes the speed estimation region.
@ -45,12 +44,15 @@ class SpeedEstimator(BaseSolution):
"""
super().__init__(**kwargs)
self.initialize_region() # Initialize speed region
self.spd = {} # Dictionary for speed data
self.trkd_ids = [] # List for already speed-estimated and tracked IDs
self.trk_pt = {} # Dictionary for tracks' previous timestamps
self.trk_pp = {} # Dictionary for tracks' previous positions
self.fps = kwargs.get("fps", 30) # assumed video FPS
self.frame_count = 0 # global frame count
self.trk_frame_ids = {} # Track ID → first frame index
self.spd = {} # Final speed per object (km/h), once locked
self.trk_hist = {} # Track ID → deque of (time, position)
self.locked_ids = set() # Track IDs whose speed has been finalized
self.max_hist = kwargs.get("max_hist", 5) # Required frame history before computing speed
self.meter_per_pixel = kwargs.get("meter_per_pixel", 0.05) # Scene scale, depends on camera details
self.max_speed = kwargs.get("max_speed", 120) # max_speed adjustment
def process(self, im0):
"""
@ -67,45 +69,39 @@ class SpeedEstimator(BaseSolution):
>>> image = np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8)
>>> results = estimator.process(image)
"""
self.extract_tracks(im0) # Extract tracks
annotator = SolutionAnnotator(im0, line_width=self.line_width) # Initialize annotator
self.frame_count += 1
self.extract_tracks(im0)
annotator = SolutionAnnotator(im0, line_width=self.line_width)
# Draw speed estimation region
annotator.draw_region(reg_pts=self.region, color=(104, 0, 123), thickness=self.line_width * 2)
for box, track_id, _, _ in zip(self.boxes, self.track_ids, self.clss, self.confs):
self.store_tracking_history(track_id, box)
for box, track_id, cls, conf in zip(self.boxes, self.track_ids, self.clss, self.confs):
self.store_tracking_history(track_id, box) # Store track history
if track_id not in self.trk_hist: # Initialize history if new track found
self.trk_hist[track_id] = deque(maxlen=self.max_hist)
self.trk_frame_ids[track_id] = self.frame_count
# Initialize tracking data for new objects
if track_id not in self.trk_pt:
self.trk_pt[track_id] = 0
if track_id not in self.trk_pp:
self.trk_pp[track_id] = self.track_line[-1]
if track_id not in self.locked_ids: # Update history until speed is locked
trk_hist = self.trk_hist[track_id]
trk_hist.append(self.track_line[-1])
speed_label = (
f"{int(self.spd[track_id])} km/h"
if track_id in self.spd and self.show_labels
else self.adjust_box_label(cls, conf, track_id)
)
annotator.box_label(box, label=speed_label, color=colors(track_id, True)) # Draw bounding box
# Compute and lock speed once enough history is collected
if len(trk_hist) == self.max_hist:
p0, p1 = trk_hist[0], trk_hist[-1] # First and last points of track
dt = (self.frame_count - self.trk_frame_ids[track_id]) / self.fps # Time in seconds
if dt > 0:
dx, dy = p1[0] - p0[0], p1[1] - p0[1] # pixel displacement
pixel_distance = sqrt(dx * dx + dy * dy) # get pixel distance
meters = pixel_distance * self.meter_per_pixel # convert to meters
self.spd[track_id] = int(
min((meters / dt) * 3.6, self.max_speed)
) # convert to km/h and store final speed
self.locked_ids.add(track_id) # prevent further updates
self.trk_hist.pop(track_id, None) # free memory
self.trk_frame_ids.pop(track_id, None) # optional: remove frame start too
# Determine if object is crossing the speed estimation region
if self.LineString([self.trk_pp[track_id], self.track_line[-1]]).intersects(self.r_s):
direction = "known"
else:
direction = "unknown"
# Calculate speed for objects crossing the region for the first time
if direction == "known" and track_id not in self.trkd_ids:
self.trkd_ids.append(track_id)
time_difference = time() - self.trk_pt[track_id]
if time_difference > 0:
# Calculate speed based on vertical displacement and time
self.spd[track_id] = abs(self.track_line[-1][1] - self.trk_pp[track_id][1]) / time_difference
# Update tracking data for next frame
self.trk_pt[track_id] = time()
self.trk_pp[track_id] = self.track_line[-1]
if track_id in self.spd:
speed_label = f"{self.spd[track_id]} km/h"
annotator.box_label(box, label=speed_label, color=colors(track_id, True)) # Draw bounding box
plot_im = annotator.result()
self.display_output(plot_im) # Display output with base class function