YOLO11解决方案之距离计算探索
概述
Ultralytics提供了一系列的解决方案,利用YOLO11解决现实世界的问题,包括物体计数、模糊处理、热力图、安防系统、速度估计、物体追踪等多个方面的应用。
测量两个物体之间的间距被称为特定空间内的距离计算,YOLO11使用两个边界框的中心点计算距离。
使用距离计算,可以提供计算机视觉任务中比较精确的空间定位,分析视频环境中的对象关系,通过监控移动物体之间的距离,使系统能够检测到潜在的碰撞,为自动驾驶或者交通监控等应用提供更好的空间场景理解能力。
演示代码
Ultralytics提供了演示代码,展示如何使用距离计算解决方案。
import cv2from ultralytics import solutionscap = cv2.VideoCapture("path/to/video.mp4")
assert cap.isOpened(), "Error reading video file"# Video writer
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 = cv2.VideoWriter("distance_output.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))# Initialize distance calculation object
distancecalculator = solutions.DistanceCalculation(model="yolo11n.pt", # path to the YOLO11 model file.show=True, # display the output
)# Process video
while cap.isOpened():success, im0 = cap.read()if not success:print("Video frame is empty or processing is complete.")breakresults = distancecalculator(im0)print(results) # access the outputvideo_writer.write(results.plot_im) # write the processed frame.cap.release()
video_writer.release()
cv2.destroyAllWindows() # destroy all opened windows
DistanceCalculation
参数
基本参数
参数 | 类型 | 默认值 | 说明 |
---|---|---|---|
model | str | None | Ultralytics YOLO 模型文件的路径。 |
DistanceCalculation
支持使用track参数:
参数 | 类型 | 默认值 | 说明 |
---|---|---|---|
tracker | str | 'botsort.yaml' | 指定要使用的跟踪算法, bytetrack.yaml 或 botsort.yaml . |
conf | float | 0.3 | 设置检测的置信度阈值;数值越低,跟踪的物体越多,但可能会出现误报。 |
iou | float | 0.5 | 设置交叉重叠 (IoU) 阈值,用于过滤重叠检测。 |
classes | list | None | 按类别索引筛选结果。例如 classes=[0, 2, 3] 只跟踪指定的类别(class在COCO数据集定义)。 |
verbose | bool | True | 控制跟踪结果的显示,提供被跟踪物体的可视化输出。 |
device | str | None | 指定用于推理的设备(例如: cpu , cuda:0 或 0 ). 允许用户选择CPU 、特定GPU 或其他计算设备运行模型。 |
可视化参数:
参数 | 类型 | 默认值 | 说明 |
---|---|---|---|
show | bool | False | 如果 True 在一个窗口中显示注释的图像或视频。有助于在开发或测试过程中提供即时视觉反馈。 |
line_width | None or int | None | 指定边界框的线宽。如果 None 则根据图像大小自动调整线宽,使图像更加清晰。 |
show_conf | bool | True | 在标签旁显示每次检测的置信度得分。让人了解模型对每次检测的确定性。 |
show_labels | bool | True | 在可视输出中显示每次检测的标签。让用户立即了解检测到的物体。 |
工作原理
DistanceCalculation
类的工作原理是跟踪视频帧中的物体,并计算所选边界框中心点之间的欧氏距离。演示程序运行时,鼠标点击断定两个边界框,系统将提取选定边界框的中心点,以像素为单位计算这些中心点之间的欧氏距离,对象之间用连线连接,并在图像上显示距离。
执行时使用 mouse_event_for_distance
方法来处理鼠标交互,允许用户根据需要选择对象和清除选择。 process
方法处理逐帧处理、跟踪物体和计算距离。
查看DistanceCalculation
类中的mouse_event_for_distance
的代码:
def mouse_event_for_distance(self, event, x, y, flags, param):"""处理鼠标事件,在实时视频流中选择区域计算距离使用左键选择两个方框点击右键取消选择"""if event == cv2.EVENT_LBUTTONDOWN:self.left_mouse_count += 1if self.left_mouse_count <= 2:for box, track_id in zip(self.boxes, self.track_ids):if box[0] < x < box[2] and box[1] < y < box[3] and track_id not in self.selected_boxes:self.selected_boxes[track_id] = boxelif event == cv2.EVENT_RBUTTONDOWN:self.selected_boxes = {}self.left_mouse_count = 0
查看DistanceCalculation
类中的process
的代码:
def process(self, im0):"""处理一个视频帧,计算两个选择的边界框之间的距离输出处理过的视频图片(叠加了距离数据)、跟踪物体的数量、像素距离"""self.extract_tracks(im0) # Extract tracksannotator = SolutionAnnotator(im0, line_width=self.line_width) # Initialize annotatorpixels_distance = 0# Iterate over bounding boxes, track ids and classes indexfor box, track_id, cls, conf in zip(self.boxes, self.track_ids, self.clss, self.confs):annotator.box_label(box, color=colors(int(cls), True), label=self.adjust_box_label(cls, conf, track_id))# 如果选定的框是被跟踪的,则更新if len(self.selected_boxes) == 2:for trk_id in self.selected_boxes.keys():if trk_id == track_id:self.selected_boxes[track_id] = boxif len(self.selected_boxes) == 2:#计算选择框的中心点坐标self.centroids.extend([[int((box[0] + box[2]) // 2), int((box[1] + box[3]) // 2)] for box in self.selected_boxes.values()])#计算两点间的欧氏距离pixels_distance = math.sqrt((self.centroids[0][0] - self.centroids[1][0]) ** 2 + (self.centroids[0][1] - self.centroids[1][1]) ** 2)annotator.plot_distance_and_line(pixels_distance, self.centroids)self.centroids = [] # Reset centroids for next frameplot_im = annotator.result()self.display_output(plot_im) # Display output with base class functioncv2.setMouseCallback("Ultralytics Solutions", self.mouse_event_for_distance)# 返回处理过的图像和计算的指标return SolutionResults(plot_im=plot_im, pixels_distance=pixels_distance, total_tracks=len(self.track_ids))
效果展示
这里使用演示代码,在测试视频中,计算两辆车的距离。
随着车辆运动,其距离在不断变化。
需要注意的是,本方案的距离计算并不精确,它只是使用了平面数据,缺少物体间的深度信息(不能计算三维位置关系)。