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


    一、相关环境搭建

    Anacode+YOLO识别图片-CSDN博客

    二、物体追踪实现

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

    1. import cv2
    2. import numpy as np
    3. import pyrealsense2 as rs
    4. from ultralytics import YOLO # 将YOLOv8导入到该py文件中
    5. # 加载官方或自定义模型
    6. model = YOLO(r"E:\Deep learning\YOLOv8\yolov8n.pt") # 加载一个官方的检测模型
    7. model = YOLO(r"E:\Deep learning\YOLOv8\yolov8s.pt") # 加载一个官方的检测模型
    8. # model = YOLO(r"E:\Deep learning\YOLOv8\yolov8n-seg.pt") # 加载一个官方的分割模型
    9. # model = YOLO(r"E:\Deep learning\YOLOv8\yolov8n-pose.pt") # 加载一个官方的姿态模型
    10. # 深度相机配置
    11. pipeline = rs.pipeline() # 定义流程pipeline,创建一个管道
    12. config = rs.config() # 定义配置config
    13. config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30) # 初始化摄像头深度流
    14. config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
    15. pipe_profile = pipeline.start(config) # 启用管段流
    16. align = rs.align(rs.stream.color) # 这个函数用于将深度图像与彩色图像对齐
    17. def get_aligned_images(): # 定义一个获取图像帧的函数,返回深度和彩色数组
    18. frames = pipeline.wait_for_frames() # 等待获取图像帧
    19. depth_frame = frames.get_depth_frame() # 获取深度帧
    20. color_frame = frames.get_color_frame() # 获取对齐帧中的的color帧
    21. depth_image = np.asanyarray(depth_frame.get_data()) # 将深度帧转换为NumPy数组
    22. color_image = np.asanyarray(color_frame.get_data()) # 将彩色帧转化为numpy数组
    23. return depth_image, color_image
    24. if __name__ == '__main__':
    25. try:
    26. while True:
    27. img_depth, img_color = get_aligned_images() # 获取深度帧和彩色帧
    28. # cv2.applyColorMap()将深度图像转化为彩色图像,以便更好的可视化分析
    29. depth_colormap = cv2.applyColorMap(
    30. cv2.convertScaleAbs(img_depth, alpha=0.07), cv2.COLORMAP_JET)
    31. source = [img_color]
    32. # 轨迹追踪,persist=true表示数据储存
    33. results = model.track(source, persist=True)
    34. img_color = results[0].plot() # 在图像上添加色彩帧(追踪结果)
    35. # 将图像color_impage和depth_colormap水平堆叠
    36. # images = np.hstack((img_color, depth_colormap))
    37. # 设置窗口,窗口大小根据图像自动调整
    38. cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
    39. # 将图像images显示在窗口中,这个显示的是带有追踪结果的图像
    40. cv2.imshow('RealSense', img_color)
    41. key = cv2.waitKey(1) # 等待用户输入
    42. # Press esc or 'q' to close the image window
    43. if key & 0xFF == ord('q') or key == 27:
    44. cv2.destroyAllWindows()
    45. pipeline.stop()
    46. break
    47. finally:
    48. # Stop streaming
    49. pipeline.stop()

    相关识别效果如下视频:

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

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

  • 相关阅读:
    GAN生成漫画脸
    leetocode 29. 两数相除-java
    Favicon网页收藏图标在线制作PHP网站源码/ICO图片在线生成/支持多种图片格式转换
    使用python-docx完成word操作
    vulnhub_DeRPnStiNK靶机渗透测试
    已解决urllib.request.urlretrieve下载文件报错403
    五魔方、二阶五魔方
    三维数组循环里面的二维数组,加参数循环的方法
    echars柱状图怎么每个柱子设置不同颜色
    练习实践:ubuntu18.04安装、配置Nginx+PHP环境,两种配置方式,多站点
  • 原文地址:https://blog.csdn.net/doudou2weiwei/article/details/139426868