guides/distance-calculation/ #7956
Replies: 49 comments 139 replies
-
Hi, I'm clicking on the objects to track distance. But it's not working. Why? |
Beta Was this translation helpful? Give feedback.
-
Hi, I am actually using mss to detect objs from my screen so how can i implement the distance caluclations as there are about 10 objects and their distance must be mapped for every obj with every other obj ? |
Beta Was this translation helpful? Give feedback.
-
Hi @pderrenger. The mouse clicking feature distance calculation ain't working for me. I'm clicking the mouse but no response. I'm using windows 11. Code: from ultralytics import YOLO model = YOLO("yolov8n.pt") cap = cv2.VideoCapture("file.mp4") Video writervideo_writer = cv2.VideoWriter("MyFile.avi", Init distance-calculation objdist_obj = distance_calculation.DistanceCalculation() while cap.isOpened():
cap.release() Please help. |
Beta Was this translation helpful? Give feedback.
-
How to find the distance of the objects from the camera? For example, in your test video, how to calculate the distance of each person from the camera? Would be glad to have a solution. TIA. |
Beta Was this translation helpful? Give feedback.
-
#I am trying to find the distance between the object and camera in real-time object detection using my webcam on yolov8n.pt model. but I am getting this error: Traceback (most recent call last): Process finished with exit code 1 #Would be glad to have a solution. TIA. from ultralytics.solutions import distance_calculation
import cv2
import argparse
from ultralytics import YOLO
def parse_arguments() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="YOLOv8 live")
parser.add_argument(
"--webcam-resolution",
default=[1280, 720],
nargs=2,
type=int
)
args = parser.parse_args()
return args
# Load the model
model = YOLO("yolov8n.pt")
# Initialize the video capture
args = parse_arguments()
frame_width, frame_height = args.webcam_resolution
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, frame_width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, frame_height)
assert cap.isOpened(), "Error opening video file"
# Initialize distance calculation object
dist_obj = distance_calculation.DistanceCalculation()
# Process video frames
while True:
success, frame = cap.read()
if not success:
break
# Run object tracking
tracks = model.track(frame, persist=True)
# Calculate distances and update the frame
frame = dist_obj.start_process(frame, tracks)
print(frame.shape)
# Display the frame
cv2.imshow('Distance Calculation', frame)
if cv2.waitKey(1) == ord('q'): # Press 'q' to quit
break
# Release resources
cap.release()
cv2.destroyAllWindows() |
Beta Was this translation helpful? Give feedback.
-
Hi, I wanted to measure distance of vehicles using a camera, but the camera's POV is inside the car (example: dashcam). So, I wanna ask if we are able to measure distance automatically without needing to click on the bounding boxes. Thanks . |
Beta Was this translation helpful? Give feedback.
-
Hello, I am running this code, the code works perfectly, however the measurement made in real time is not correct, how can I improve this problem? from ultralytics import YOLO model = YOLO("yolov8m.pt") Use 0 for the default webcamcap = cv2.VideoCapture(1) #Init distance-calculation obj while cap.isOpened(): cap.release() |
Beta Was this translation helpful? Give feedback.
-
Hello, I am running this code, the code works perfectly; I mean, the code can detect de object and it gets a measure. However the measurement made in real time is not correct, how can I improve this problem? from ultralytics import YOLO model = YOLO("yolov8m.pt") Use 0 for the default webcamcap = cv2.VideoCapture(1) #Init distance-calculation obj while cap.isOpened(): cap.release() |
Beta Was this translation helpful? Give feedback.
-
Hi, I already calibrated my camera to measure the correct distance between objects with the camera at a distance of 1.75 cm. However, when I move the camera away and measure the distance between the objects again, the measurement is incorrect. How can I improve this measurement by moving the camera away and maintaining the correct distance? |
Beta Was this translation helpful? Give feedback.
-
Hey, I'm unable the interact with the objects in my .avi file. Does this work on Ubuntu 20.04.6 LTS? My python version is 3.11.8. The code is running and it's detecting objects from my mp4 file, but I'm unable to interact with distance_calculation.avi. I don't get any popup window while running the code file. Code file from ultralytics import YOLO model = YOLO("yolov8m.pt") cap = cv2.VideoCapture("/home/abdullah/Desktop/intern depth map/depthultra/car.mp4") Video writervideo_writer = cv2.VideoWriter("distance_calculation.avi", Init distance-calculation objdist_obj = distance_calculation.DistanceCalculation() while cap.isOpened():
cap.release() |
Beta Was this translation helpful? Give feedback.
-
can we use google colab to rrun the program and draw the lines? |
Beta Was this translation helpful? Give feedback.
-
hi i am trying to measure the distance of potholes from a car camera. can you provide the full code to measure the distance between the camera and the potholes in a video file |
Beta Was this translation helpful? Give feedback.
-
Hi, when running the following code: from ultralytics import YOLO Use 0 for the default webcamcap = cv2.VideoCapture(1) I get the following in my terminal: However, I would like to disappear all that and only have in the terminal the distance between the two selected objects. How can I do this? |
Beta Was this translation helpful? Give feedback.
-
whenever i try to run any version of this code i get an error message that there's no module called ultralytics.solutions despite having downloaded ultralytics using pip install. TIA!! |
Beta Was this translation helpful? Give feedback.
-
how can I use this in images with normal bounding boxes, how can I use it that way? |
Beta Was this translation helpful? Give feedback.
-
You've set the pixel per meter to 10 in this. Could you tell me how you found out the pixel per meter? In a real-time scenario, there could be different cameras, so for accurate results, the pixel per meter should also be accurate. I'm awaiting your response |
Beta Was this translation helpful? Give feedback.
-
how is this "Conversion factor from pixels to meters" working? |
Beta Was this translation helpful? Give feedback.
-
is it gonna integrate with object detection by YOLO v8 both together showing the bounding boxes with Distance and type of the objects? |
Beta Was this translation helpful? Give feedback.
-
How can I adapt the code from above to work with an instance segmentation model? I am detecting railroad lines, so the bounding boxes are not very accurate a lot of the time, and I want to use the masks from my segmentation model to calculate my distances. Here is my code: model = YOLO("YOLOv8-pretrained-200-epochs.pt") cap = cv2.VideoCapture("IMG_2688.MOV") Video writervideo_writer = cv2.VideoWriter("distance_calculation.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h)) Init distance-calculation objdist_obj = solutions.DistanceCalculation(names=names, view_img=True) while cap.isOpened():
cap.release() |
Beta Was this translation helpful? Give feedback.
-
I am trying to calculate the distance from each detected object to the vertical center of the screen so I can compare how far each of them are from the center. I would prefer to do this using the masks because I have an instance segmentation model, but I can do it with bounding boxes too. How can I figure out how far the centroids of each box or mask are from the center? For example, if my image is 640 pixels wide and 320 pixels tall, I want the center of the 640. |
Beta Was this translation helpful? Give feedback.
-
Hi, I am very new to deep learning, and I'm trying to build a model from YOLOv8 with transfer learning to estimate the speed and calculate the distance of vehicles in a given data set. Although the ultralytics documentation is quite robust, I am still determining how to code them together. I want to train my model on the custom data set, and finally, from the measured distance and speed, I want to calculate the time to collision. So, I would appreciate any help from the community; thanks! By the way, I am working with a dataset consisting of temporally nearby frames. |
Beta Was this translation helpful? Give feedback.
-
May I ask if it is possible to detect the length and width of objects in the video |
Beta Was this translation helpful? Give feedback.
-
where is DistanceCalculation.py file? |
Beta Was this translation helpful? Give feedback.
-
Hi Guys, |
Beta Was this translation helpful? Give feedback.
-
Hi,Is there a method to calculate the distance between the camera and the objects? |
Beta Was this translation helpful? Give feedback.
-
Hello everyone, I want to deploy an yolov8-application on the edge device and estimate the distance through the user's camera without having to click on the detected object, how can I find the distance of the objects to the camera without need to click? |
Beta Was this translation helpful? Give feedback.
-
Hey there, I have a model that is trained for object detection not tracking. We are detecting two animals that are inside a cage. What we want to do is import a mp4 video then calculate the distance between the two objects while also showing the bounding boxes. However when i use the VideoStream code snippet from above, the popup window is larger then my screen. How could i adjust that code to make it a different window size so that I am able to track them in real time. Also after this is done and the distances are calculated is there anyway to save the whole video with the bounding boxes and the distance calculations that were done in real time? Or would I have to save the video by doing the results = model.predict(save=true) and also do the VideoStream code snippet from above at the same time? and not have the distances shown in the saved video? Get back to me as soon as possible as this is for a project for school! Thank You!! |
Beta Was this translation helpful? Give feedback.
-
Hi, I am using the example code of Distance Calculation using YOLO11 Example, but there is a problem of |
Beta Was this translation helpful? Give feedback.
-
How can we find the distance of in the bounding box like from one point to another point in the bounding box to measure the size of object |
Beta Was this translation helpful? Give feedback.
-
Is it possible to know the distance from an object and another object without using mouse? |
Beta Was this translation helpful? Give feedback.
-
guides/distance-calculation/
Distance Calculation Using Ultralytics YOLOv8
Python Usage
Documentation
For more information, you can explore: https://docs.ultralytics.com/guides/distance-calculation/
Beta Was this translation helpful? Give feedback.
All reactions