yolov9直接调用zed相机实现三维测距(python)

yolov9直接调用zed相机实现三维测距(python)

  • [1. 相关配置](#1. 相关配置)
  • [2. 相关代码](#2. 相关代码)
    • [2.1 相机设置](#2.1 相机设置)
    • [2.2 测距模块](#2.2 测距模块)
    • [2.2 实验结果](#2.2 实验结果)

相关链接

此项目直接调用zed相机实现三维测距,无需标定,相关内容如下:

1. yolov4直接调用zed相机实现三维测距
2.yolov5直接调用zed相机实现三维测距(python)
3. yolov8直接调用zed相机实现三维测距(python)
4.具体实现效果已在哔哩哔哩发布,点击此链接跳转

本篇博文工程源码下载(麻烦github给个星星)
下载链接:https://github.com/up-up-up-up/zed-yolov9

附:Zed调用YOLOv7测距也已经实现,但是3060笔记本6G显存带不动,在大现存服务器上可以运行,可能是由于YOLOv7网络结构导致的,由于不具备普适性,就不再写相关文章了,有需要的可以仿照这个代码去改写

1. 相关配置

python==3.7

Windows-pycharm

zed api 具体配置见 (zed api 配置步骤)

2. 相关代码

2.1 相机设置

python 复制代码
zed = sl.Camera()
input_type = sl.InputType()
 if opt.svo is not None:
     input_type.set_from_svo_file(opt.svo)

 # Create a InitParameters object and set configuration parameters
 init_params = sl.InitParameters(input_t=input_type, svo_real_time_mode=True)
 init_params.camera_resolution = sl.RESOLUTION.HD720
 init_params.coordinate_units = sl.UNIT.METER
 init_params.depth_mode = sl.DEPTH_MODE.ULTRA  # QUALITY
 init_params.coordinate_system = sl.COORDINATE_SYSTEM.RIGHT_HANDED_Y_UP
 init_params.depth_maximum_distance = 5

 runtime_params = sl.RuntimeParameters()
 status = zed.open(init_params)

2.2 测距模块

python 复制代码
for *xyxy, conf, cls in reversed(det):
     xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist()  # normalized xywh
     cent_x = round(xywh[0] * im0.shape[1])
     cent_y = round(xywh[1] * im0.shape[0])
     cent_w = round(xywh[2] * im0.shape[1])
     point_1 = round(cent_x - 0.4 * cent_w)
     point_2 = round(cent_x + 0.4 * cent_w)
     wide_value_1 = point_cloud.get_value(point_1, cent_y)[1]
     wide_value_2 = point_cloud.get_value(point_2, cent_y)[1]

     try:
         wide = round(wide_value_1[0], 4) - round(wide_value_2[0], 4)
         wide = round(abs(wide * 1000))
     except:
         wide = 0.00
         pass
     point_cloud_value = point_cloud.get_value(cent_x, cent_y)[1]
     point_cloud_value = point_cloud_value * -1000.00
     if point_cloud_value[2] > 0.00:
         try:
             point_cloud_value[0] = round(point_cloud_value[0])
             point_cloud_value[1] = round(point_cloud_value[1])
             point_cloud_value[2] = round(point_cloud_value[2])
             print("x:", point_cloud_value[0], "y:", point_cloud_value[1], "z:",
                   point_cloud_value[2], "W:", wide)
             txt = 'x:{0} y:{1} z:{2} w:{3}'.format(point_cloud_value[0], point_cloud_value[1],
                                                    point_cloud_value[2], wide)
             a=point_cloud_value[0]
             b=point_cloud_value[1]
             c=point_cloud_value[2]
             distance = ((a ** 2 + b ** 2 + c ** 2) ** 0.5)

             # annotator.box_label(xyxy, txt, color=(255, 0, 0))
             label = f'{names[int(cls)]} {conf:.2f} '
             label = label + " " +"dis:" +str(distance)
             annotator.box_label(xyxy, label, color=colors(c, True))

         except:
             pass

2.2 实验结果

测距功能

视频展示

相关推荐
xyq20241 分钟前
Vue3 条件语句详解
开发语言
这个名有人用不10 分钟前
解决 uv 虚拟环境使用 pip 命令提示command not found的办法
python·pip·uv·claude code
浩浩kids25 分钟前
R•Homework
开发语言·r语言
Oueii32 分钟前
掌握Python魔法方法(Magic Methods)
jvm·数据库·python
qq_4160187238 分钟前
设计模式在C++中的实现
开发语言·c++·算法
Evand J41 分钟前
【MATLAB教程】在matlab中,gscatter和scatter两个命令的区别
开发语言·matlab·教程·绘图·命令·教学
2501_908329851 小时前
使用Python自动收发邮件
jvm·数据库·python
2301_776508721 小时前
C++与机器学习框架
开发语言·c++·算法
Albertbreak1 小时前
STL容器内部实现剖析
开发语言·c++·算法