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
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.
Attributes:
Name | Type | Description |
---|---|---|
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. |
annotator |
Annotator
|
Annotator object for drawing on images. |
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. |
Methods:
Name | Description |
---|---|
initialize_region |
Initializes the speed estimation region. |
estimate_speed |
Estimates the speed of objects based on tracking data. |
store_tracking_history |
Stores the tracking history for an object. |
extract_tracks |
Extracts tracks from the current frame. |
display_output |
Displays the output with annotations. |
Examples:
>>> estimator = SpeedEstimator()
>>> frame = cv2.imread("frame.jpg")
>>> processed_frame = estimator.estimate_speed(frame)
>>> cv2.imshow("Speed Estimation", processed_frame)
Source code in ultralytics/solutions/speed_estimation.py
estimate_speed
Estimates the speed of objects based on tracking data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
im0
|
ndarray
|
Input image for processing. Shape is typically (H, W, C) for RGB images. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Processed image with speed estimations and annotations. |
Examples:
>>> estimator = SpeedEstimator()
>>> image = np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8)
>>> processed_image = estimator.estimate_speed(image)