Are you sure you want to delete this access key?
comments | description | keywords |
---|---|---|
true | Learn how to calculate distances between objects using Ultralytics YOLOv8 for accurate spatial positioning and scene understanding. | Ultralytics, YOLOv8, distance calculation, computer vision, object tracking, spatial positioning |
Measuring the gap between two objects is known as distance calculation within a specified space. In the case of Ultralytics YOLOv8, the bounding box centroid is employed to calculate the distance for bounding boxes highlighted by the user.
Watch: Distance Calculation using Ultralytics YOLOv8
Distance Calculation using Ultralytics YOLOv8 |
---|
![]() |
???+ tip "Distance Calculation"
- Click on any two bounding boxes with Left Mouse click for distance calculation
!!! example "Distance Calculation using YOLOv8 Example"
=== "Video Stream"
```python
import cv2
from ultralytics import YOLO, solutions
model = YOLO("yolov8n.pt")
names = model.model.names
cap = cv2.VideoCapture("path/to/video/file.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
video_writer = cv2.VideoWriter("distance_calculation.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
# Init distance-calculation obj
dist_obj = solutions.DistanceCalculation(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 = dist_obj.start_process(im0, tracks)
video_writer.write(im0)
cap.release()
video_writer.release()
cv2.destroyAllWindows()
```
???+ note
- Mouse Right Click will delete all drawn points
- Mouse Left Click can be used to draw points
???+ warning "Distance is Estimate"
Distance will be an estimate and may not be fully accurate, as it is calculated using 2-dimensional data, which lacks information about the object's depth.
DistanceCalculation()
Name |
Type |
Default |
Description |
---|---|---|---|
names |
dict |
None |
Dictionary of classes names. |
view_img |
bool |
False |
Flag to indicate if the video stream should be displayed. |
line_thickness |
int |
2 |
Thickness of the lines drawn on the image. |
line_color |
tuple |
(255, 255, 0) |
Color of the lines drawn on the image (BGR format). |
centroid_color |
tuple |
(255, 0, 255) |
Color of the centroids drawn (BGR format). |
model.track
{% include "macros/track-args.md" %}
To calculate distances between objects using Ultralytics YOLOv8, you need to identify the bounding box centroids of the detected objects. This process involves initializing the DistanceCalculation
class from Ultralytics' solutions
module and using the model's tracking outputs to calculate the distances. You can refer to the implementation in the distance calculation example.
Using distance calculation with Ultralytics YOLOv8 offers several advantages:
Yes, you can perform distance calculation in real-time video streams with Ultralytics YOLOv8. The process involves capturing video frames using OpenCV, running YOLOv8 object detection, and using the DistanceCalculation
class to calculate distances between objects in successive frames. For a detailed implementation, see the video stream example.
To delete points drawn during distance calculation with Ultralytics YOLOv8, you can use a right mouse click. This action will clear all the points you have drawn. For more details, refer to the note section under the distance calculation example.
The key arguments for initializing the DistanceCalculation
class in Ultralytics YOLOv8 include:
names
: Dictionary mapping class indices to class names.view_img
: Flag to indicate if the video stream should be displayed.line_thickness
: Thickness of the lines drawn on the image.line_color
: Color of the lines drawn on the image (BGR format).centroid_color
: Color of the centroids (BGR format).For an exhaustive list and default values, see the arguments of DistanceCalculation.
Press p or to see the previous file or, n or to see the next file
Browsing data directories saved to S3 is possible with DAGsHub. Let's configure your repository to easily display your data in the context of any commit!
ultralytics is now integrated with AWS S3!
Are you sure you want to delete this access key?
Browsing data directories saved to Google Cloud Storage is possible with DAGsHub. Let's configure your repository to easily display your data in the context of any commit!
ultralytics is now integrated with Google Cloud Storage!
Are you sure you want to delete this access key?
Browsing data directories saved to Azure Cloud Storage is possible with DAGsHub. Let's configure your repository to easily display your data in the context of any commit!
ultralytics is now integrated with Azure Cloud Storage!
Are you sure you want to delete this access key?
Browsing data directories saved to S3 compatible storage is possible with DAGsHub. Let's configure your repository to easily display your data in the context of any commit!
ultralytics is now integrated with your S3 compatible storage!
Are you sure you want to delete this access key?