YOLOv5+Intel realsense D405(目标检测+深度相机测距)

加载好本地自己训练好的best.pt文件,一个非常要注意的地方:那就是输出的图片的R、G、B三通道是如何排列的,比如是(R,G,B)还是(B,G,R)。当通道数不是我们要求输出的格式时,对我们的目标检测(图像识别)效果存在天差地别的影响。简而言之,就是图像识别出现错误。!!!希望后面的读者要引起注意!!!

源代码:

import pyrealsense2 as rs
import numpy as np
import cv2
import random
import torch
import time

model = torch.hub.load('E:/yolo/yolov5-6.0', 'custom', path=r'best1.pt', source='local') 
model.conf = 0.8  # set confidence threshold

# 原始代码部分
def get_mid_pos(frame, box, depth_data, randnum):
    distance_list = []
    mid_pos = [(box[0] + box[2]) // 2, (box[1] + box[3]) // 2]  # 确定索引深度的中心像素位置
    min_val = min(abs(box[2] - box[0]), abs(box[3] - box[1]))  # 确定深度搜索范围
    # print(box,)
    for i in range(randnum):
        bias = random.randint(-min_val // 4, min_val // 4)
        dist = depth_data[int(mid_pos[1] + bias), int(mid_pos[0] + bias)]
        cv2.circle(frame, (int(mid_pos[0] + bias), int(mid_pos[1] + bias)), 4, (255, 0, 0), -1)
        # print(int(mid_pos[1] + bias), int(mid_pos[0] + bias))
        if dist:
            distance_list.append(dist)
    distance_list = np.array(distance_list)
    distance_list = np.sort(distance_list)[randnum // 2 - randnum // 4:randnum // 2 + randnum // 4]  # 冒泡排序+中值滤波
    # print(distance_list, np.mean(distance_list))
    return np.mean(distance_list)


def dectshow(org_img, boxs, depth_data):
    img = org_img.copy()
    for box in boxs:
        cv2.rectangle(img, (int(box[0]), int(box[1])), (int(box[2]), int(box[3])), (0, 255, 0), 2)
        dist = get_mid_pos(org_img, box, depth_data, 24)
        cv2.putText(img, box[-1] + str(dist / 10000)[:4] + 'm',
                    (int(box[0]), int(box[1])), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
    cv2.imshow('dec_img', img)

if __name__ == "__main__":
    # Configure depth and color streams
    pipeline = rs.pipeline()
    config = rs.config()
    config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 60)
    config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 60)
    # Start streaming
    pipeline.start(config)
    try:
        while True:
            # Wait for a coherent pair of frames: depth and color
            frames = pipeline.wait_for_frames()
            depth_frame = frames.get_depth_frame()
            color_frame = frames.get_color_frame()
            if not depth_frame or not color_frame:
                continue
            # Convert images to numpy arrays

            depth_image = np.asanyarray(depth_frame.get_data())

            color_image = np.asanyarray(color_frame.get_data())
            # https://blog.csdn.net/weixin_52527544/article/details/128008221
            img = color_image[:, :, ::-1]
            results = model(img)

            boxs = results.pandas().xyxy[0].values
            # boxs = np.load('temp.npy',allow_pickle=True)
            # dectshow(color_image, boxs, depth_frame) # 修改后的代码,这里得这样变化
            dectshow(color_image, boxs, depth_image)
            # Apply colormap on depth image (image must be converted to 8-bit per pixel first)
            depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)
            # Stack both images horizontally
            images = np.hstack((color_image, depth_colormap))
            # Show images
            cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
            cv2.imshow('RealSense', images)
            key = cv2.waitKey(1)
            # Press esc or 'q' to close the image window
            if key & 0xFF == ord('q') or key == 27:
                cv2.destroyAllWindows()
                break
    finally:
        # Stop streaming
        pipeline.stop()

【资源说明】 1、该资源包括项目的全部源码,下载可以直接使用! 2、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考资料学习借鉴。 3、本资源作为“参考资料”如果需要实现其他功能,需要能看懂代码,并且热爱钻研,自行调试。 基于YOLOv5+Intel_Realsense_D435i开发的物体之间三维距离测量python源码+项目说明.zip 结合YOLOv5Intel_Realsense_D435i 进行开发,实现实时检测物体之间的三维距离 [yolov5]:实时目标检测算法 [Intel Relsense D435i深度摄像头](https://www.intelrealsense.com/zh-hans/depth-camera-d435i/):Intel使用realsense(实感)技术开发出来的的深度摄像头,可以获取目标的三维信息 ## 1.Use and Environment: 如果您想直接使用,请使用yolov5_D435i_2.0 yolov5_D435i_1.0是本人学习时的版本。 ### Environment: 1.一个可运行yolov5的环境 2.一个Intel realsense D435i相机,pyrealsense2和各种依赖库 ``` 1. could run yolov5 2. pip install -r requirements.txt 3. pip install pyrealsense2 ``` ### Use: 配置yolov5_D435i_2.0/config/yolov5s.yaml,运行yolov5_D435i_2.0/config/main2.py即可 yolov5_D435i_2.0/config/yolov5s.yaml: ``` weight: "weights/yolov5s.pt" # 输入图像的尺寸 input_size: [640,480] # 类别个数 class_num: 80 # 标签名称 class_name: [ 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush' ] # 阈值设置 threshold: iou: 0.45 confidence: 0.6 # 计算设备 # - cpu # - 0 <- 使用GPU device: '0' target: ['person']#检测哪些类别之间的距离 which objects you want to detect ``` ## 2.Attenion ...
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值