Meet YOLO26: next-gen vision AI.

Link to this section使用 Ultralytics YOLO26 进行距离计算#

Link to this section什么是距离计算?#

距离计算是测量图像或视频帧中两个检测到的对象之间空间距离的过程。对于 Ultralytics YOLO26,系统使用边界框的质心来计算用户选定边界框之间的距离。



Watch: How to estimate distance between detected objects with Ultralytics YOLO in Pixels 🚀

Link to this section视觉效果#

使用 Ultralytics YOLO26 进行距离计算
Ultralytics YOLO26 距离计算

Link to this section距离计算的优势#

  • 定位 精度 增强了计算机视觉任务中空间定位的准确性。
  • 尺寸估计: 允许估算对象尺寸,从而实现更好的上下文理解。
  • 场景理解: 改进了对 3D 场景的理解,从而在自动驾驶和监控系统等应用中做出更好的决策。
  • 防碰撞: 使系统能够通过监测移动对象之间的距离来检测潜在碰撞。
  • 空间分析: 有助于分析受监控环境中对象之间的关系和交互。
距离计算
  • 使用鼠标左键点击任意两个边界框以计算距离。
  • 使用鼠标右键删除所有已绘制的点。
  • 在画面中任意位置点击鼠标左键以添加新点。
距离仅为估算值

距离计算仅为估算值,由于它是使用缺乏深度信息的 2D 数据计算得出的,因此可能不够完全准确。

使用 Ultralytics YOLO 进行距离计算
import cv2

from ultralytics import solutions

cap = 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="yolo26n.pt",  # path to the YOLO26 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.")
        break

    results = distancecalculator(im0)

    print(results)  # access the output

    video_writer.write(results.plot_im)  # write the processed frame.

cap.release()
video_writer.release()
cv2.destroyAllWindows()  # destroy all opened windows

Link to this sectionDistanceCalculation() 参数#

下表列出了 DistanceCalculation 的参数:

参数类型默认值描述
modelstrNoneUltralytics YOLO 模型文件的路径。

You can also make use of various track arguments in the DistanceCalculation solution.

参数类型默认值描述
trackerstr'botsort.yaml'指定要使用的跟踪算法,例如 bytetrack.yamlbotsort.yaml
conffloat0.1设置检测的置信度阈值;较低的值允许跟踪更多对象,但可能会包含误报。
ioufloat0.7设置用于过滤重叠检测的交并比 (IoU) 阈值。
classeslistNone按类别索引过滤结果。例如,classes=[0, 2, 3] 仅跟踪指定的类别。
verboseboolTrue控制跟踪结果的显示,提供跟踪对象的视觉输出。
devicestrNone指定推理设备(例如 cpucuda:00)。允许用户在 CPU、特定 GPU 或其他计算设备之间进行选择以执行模型。

此外,还提供以下视觉参数:

参数类型默认值描述
showboolFalse如果设为 True,则会在窗口中显示带标注的图像或视频。这对于开发或测试过程中的即时视觉反馈非常有用。
line_widthint or NoneNone指定边界框的线宽。如果为 None,则根据图像大小自动调整线宽。提供清晰的视觉自定义选项。
show_confboolTrue在视觉输出中显示每个检测结果的置信度得分及其标签。这有助于了解模型对每次检测的确定性。
show_labelsboolTrue在视觉输出中显示每个检测结果的标签。提供对检测对象的直观理解。

Link to this section实现细节#

DistanceCalculation 类通过在视频帧中跟踪对象并计算选定边界框质心之间的欧几里得距离来工作。当你点击两个对象时,该解决方案会执行以下操作:

  1. 提取选定边界框的质心(中心点)
  2. 计算这些质心之间以像素为单位的欧几里得距离
  3. 在画面上显示距离,并在对象之间绘制一条连接线

该实现使用 mouse_event_for_distance 方法处理鼠标交互,允许用户根据需要选择对象和清除选择。process 方法负责处理逐帧过程、跟踪对象以及计算距离。

Link to this section应用场景#

结合 YOLO26 的距离计算具有许多实际应用:

  • 零售分析: 测量顾客与产品的距离并分析商店布局的有效性
  • 工业安全: 监控工人和机械之间的安全距离
  • 交通管理: 分析车辆间距并检测跟车行为
  • 运动分析: 计算球员、球和场地关键位置之间的距离
  • 医疗保健: 确保候诊区保持适当距离并监控患者移动
  • 机器人技术: 使机器人能够与障碍物和人员保持适当距离

Link to this section常见问题解答#

Link to this section我该如何使用 Ultralytics YOLO26 计算对象之间的距离?#

To calculate distances between objects using Ultralytics YOLO26, 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.

Link to this section使用 Ultralytics YOLO26 进行距离计算有什么优势?#

使用 Ultralytics YOLO26 进行距离计算有以下几点优势:

  • 定位精度: 为对象提供准确的空间定位。
  • 尺寸估计: 有助于估算物理尺寸,有助于更好地理解上下文。
  • 场景理解: 增强对 3D 场景的理解,辅助在自动驾驶和监控等应用中做出更好的决策。
  • 实时处理: 即时执行计算,使其适用于实时视频分析。
  • 集成能力: 可与其他 YOLO26 解决方案(如对象跟踪速度估计)无缝协作。

Link to this section我可以在 Ultralytics YOLO26 的实时视频流中进行距离计算吗?#

可以,你可以使用 Ultralytics YOLO26 在实时视频流中进行距离计算。该过程涉及使用 OpenCV 捕获视频帧,运行 YOLO26 对象检测,并使用 DistanceCalculation 类计算连续帧中对象之间的距离。有关详细实现,请参阅视频流示例

Link to this section我该如何删除在使用 Ultralytics YOLO26 进行距离计算时绘制的点?#

要删除在使用 Ultralytics YOLO26 进行距离计算时绘制的点,可以使用鼠标右键单击。此操作将清除你绘制的所有点。有关更多详细信息,请参阅距离计算示例下的说明部分。

Link to this section在 Ultralytics YOLO26 中初始化 DistanceCalculation 类的关键参数有哪些?#

在 Ultralytics YOLO26 中初始化 DistanceCalculation 类的关键参数包括:

  • model:YOLO26 模型文件的路径。
  • tracker:要使用的跟踪算法(默认值为 'botsort.yaml')。
  • conf:检测的置信度阈值。
  • show:用于显示输出的标志。

有关完整列表和默认值,请参阅 DistanceCalculation 的参数

评论