您的位置:首页 > 房产 > 建筑 > VScode+YOLOv8+深度相机D435i实现物体追踪

VScode+YOLOv8+深度相机D435i实现物体追踪

2024/10/6 16:30:14 来源:https://blog.csdn.net/doudou2weiwei/article/details/139426868  浏览:    关键词:VScode+YOLOv8+深度相机D435i实现物体追踪

一、相关环境搭建

Anacode+YOLO识别图片-CSDN博客

二、物体追踪实现

通过导入相关的检测模型后,就可以实现物体追踪与识别。

import cv2
import numpy as np
import pyrealsense2 as rs
from ultralytics import YOLO  # 将YOLOv8导入到该py文件中# 加载官方或自定义模型
model = YOLO(r"E:\Deep learning\YOLOv8\yolov8n.pt")  # 加载一个官方的检测模型
model = YOLO(r"E:\Deep learning\YOLOv8\yolov8s.pt")  # 加载一个官方的检测模型
# model = YOLO(r"E:\Deep learning\YOLOv8\yolov8n-seg.pt")  # 加载一个官方的分割模型
# model = YOLO(r"E:\Deep learning\YOLOv8\yolov8n-pose.pt")  # 加载一个官方的姿态模型# 深度相机配置
pipeline = rs.pipeline()  # 定义流程pipeline,创建一个管道
config = rs.config()  # 定义配置config
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)  # 初始化摄像头深度流
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
pipe_profile = pipeline.start(config)  # 启用管段流
align = rs.align(rs.stream.color)  # 这个函数用于将深度图像与彩色图像对齐def get_aligned_images():  # 定义一个获取图像帧的函数,返回深度和彩色数组frames = pipeline.wait_for_frames()  # 等待获取图像帧depth_frame = frames.get_depth_frame()  # 获取深度帧color_frame = frames.get_color_frame()  # 获取对齐帧中的的color帧depth_image = np.asanyarray(depth_frame.get_data())  # 将深度帧转换为NumPy数组color_image = np.asanyarray(color_frame.get_data())  # 将彩色帧转化为numpy数组return depth_image, color_imageif __name__ == '__main__':try:while True:img_depth, img_color = get_aligned_images()  # 获取深度帧和彩色帧# cv2.applyColorMap()将深度图像转化为彩色图像,以便更好的可视化分析depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(img_depth, alpha=0.07), cv2.COLORMAP_JET)source = [img_color]# 轨迹追踪,persist=true表示数据储存results = model.track(source, persist=True)img_color = results[0].plot()  # 在图像上添加色彩帧(追踪结果)# 将图像color_impage和depth_colormap水平堆叠# images = np.hstack((img_color, depth_colormap))# 设置窗口,窗口大小根据图像自动调整cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)# 将图像images显示在窗口中,这个显示的是带有追踪结果的图像cv2.imshow('RealSense', img_color)key = cv2.waitKey(1)  # 等待用户输入# Press esc or 'q' to close the image windowif key & 0xFF == ord('q') or key == 27:cv2.destroyAllWindows()pipeline.stop()breakfinally:# Stop streamingpipeline.stop()

相关识别效果如下视频:

这里当人或者物体移动的时候,相应的识别框和标识也会跟着动。此外,如果采用了-Pose还可以识别人的姿态。

D435i相机+VScode+YOLOv8视频识别追踪

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com