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 实验结果
测距功能
视频展示