Reference for ultralytics/solutions/distance_calculation.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/solutions/distance_calculation.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.solutions.distance_calculation.DistanceCalculation
DistanceCalculation(names, pixels_per_meter=10, view_img=False, line_thickness=2, line_color=(255, 255, 0), centroid_color=(255, 0, 255))
A class to calculate distance between two objects in a real-time video stream based on their tracks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
names |
dict
|
Dictionary of classes names. |
required |
pixels_per_meter |
int
|
Conversion factor from pixels to meters. Defaults to 10. |
10
|
view_img |
bool
|
Flag to indicate if the video stream should be displayed. Defaults to False. |
False
|
line_thickness |
int
|
Thickness of the lines drawn on the image. Defaults to 2. |
2
|
line_color |
tuple
|
Color of the lines drawn on the image (BGR format). Defaults to (255, 255, 0). |
(255, 255, 0)
|
centroid_color |
tuple
|
Color of the centroids drawn (BGR format). Defaults to (255, 0, 255). |
(255, 0, 255)
|
Source code in ultralytics/solutions/distance_calculation.py
calculate_centroid
staticmethod
Calculates the centroid of a bounding box.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
box |
list
|
Bounding box coordinates [x1, y1, x2, y2]. |
required |
Returns:
Type | Description |
---|---|
tuple
|
Centroid coordinates (x, y). |
Source code in ultralytics/solutions/distance_calculation.py
calculate_distance
Calculates the distance between two centroids.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
centroid1 |
tuple
|
Coordinates of the first centroid (x, y). |
required |
centroid2 |
tuple
|
Coordinates of the second centroid (x, y). |
required |
Returns:
Type | Description |
---|---|
tuple
|
Distance in meters and millimeters. |
Source code in ultralytics/solutions/distance_calculation.py
display_frames
Displays the current frame with annotations.
Source code in ultralytics/solutions/distance_calculation.py
extract_tracks
Extracts tracking results from the provided data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tracks |
list
|
List of tracks obtained from the object tracking process. |
required |
Source code in ultralytics/solutions/distance_calculation.py
mouse_event_for_distance
Handles mouse events to select regions in a real-time video stream.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event |
int
|
Type of mouse event (e.g., cv2.EVENT_MOUSEMOVE, cv2.EVENT_LBUTTONDOWN, etc.). |
required |
x |
int
|
X-coordinate of the mouse pointer. |
required |
y |
int
|
Y-coordinate of the mouse pointer. |
required |
flags |
int
|
Flags associated with the event (e.g., cv2.EVENT_FLAG_CTRLKEY, cv2.EVENT_FLAG_SHIFTKEY, etc.). |
required |
param |
dict
|
Additional parameters passed to the function. |
required |
Source code in ultralytics/solutions/distance_calculation.py
start_process
Processes the video frame and calculates the distance between two bounding boxes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
im0 |
ndarray
|
The image frame. |
required |
tracks |
list
|
List of tracks obtained from the object tracking process. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
The processed image frame. |
Source code in ultralytics/solutions/distance_calculation.py
Created 2024-01-05, Updated 2024-07-21
Authors: glenn-jocher (3), Burhan-Q (1), AyushExel (1), RizwanMunawar (1)