Reference for ultralytics/solutions/speed_estimation.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/solutions/speed_estimation.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.solutions.speed_estimation.SpeedEstimator
SpeedEstimator(**kwargs)
Bases: BaseSolution
A class to estimate the speed of objects in a real-time video stream based on their tracks.
This class extends the BaseSolution class and provides functionality for estimating object speeds using tracking data in video streams. Speed is calculated based on pixel displacement over time and converted to real-world units using a configurable meters-per-pixel scale factor.
Attributes:
Name | Type | Description |
---|---|---|
fps |
float
|
Video frame rate for time calculations. |
frame_count |
int
|
Global frame counter for tracking temporal information. |
trk_frame_ids |
dict
|
Maps track IDs to their first frame index. |
spd |
dict
|
Final speed per object in km/h once locked. |
trk_hist |
dict
|
Maps track IDs to deque of position history. |
locked_ids |
set
|
Track IDs whose speed has been finalized. |
max_hist |
int
|
Required frame history before computing speed. |
meter_per_pixel |
float
|
Real-world meters represented by one pixel for scene scale conversion. |
max_speed |
int
|
Maximum allowed object speed; values above this will be capped. |
Methods:
Name | Description |
---|---|
process |
Process input frames to estimate object speeds based on tracking data. |
store_tracking_history |
Store the tracking history for an object. |
extract_tracks |
Extract tracks from the current frame. |
display_output |
Display the output with annotations. |
Examples:
Initialize speed estimator and process a frame
>>> estimator = SpeedEstimator(meter_per_pixel=0.04, max_speed=120)
>>> frame = cv2.imread("frame.jpg")
>>> results = estimator.process(frame)
>>> cv2.imshow("Speed Estimation", results.plot_im)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs
|
Any
|
Additional keyword arguments passed to the parent class. |
{}
|
Source code in ultralytics/solutions/speed_estimation.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
|
process
process(im0)
Process an input frame to estimate object speeds based on tracking data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
im0
|
ndarray
|
Input image for processing with shape (H, W, C) for RGB images. |
required |
Returns:
Type | Description |
---|---|
SolutionResults
|
Contains processed image |
Examples:
Process a frame for speed estimation
>>> estimator = SpeedEstimator()
>>> image = np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8)
>>> results = estimator.process(image)
Source code in ultralytics/solutions/speed_estimation.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
|