realsensed435和yolov5结合
时间: 2025-05-23 11:15:15 浏览: 15
### 实现Realsense D435相机与YOLOv5目标检测模型的集成
为了实现实时物体检测,可以采用PyTorch框架来加载预训练好的YOLOv5模型,并利用Intel RealSense SDK读取D435相机的数据流。具体来说,在初始化阶段需配置好摄像头参数以及神经网络环境;运行期间则要不断抓拍图像帧送入算法处理得到边界框信息。
当涉及到获取被标记对象的空间位置时,由于RGB-D传感器能够提供深度图,因此可以根据像素点对应的深度值计算出世界坐标系中的(x,y,z)[^2]。
下面给出一段简化后的Python脚本作为示范:
```python
import torch
from realsense_camera import *
from utils.general import non_max_suppression, scale_coords
from models.experimental import attempt_load
def detect_objects():
weights = 'path_to_yolov5_weights' # YOLOv5权重路径
imgsz = 640 # 输入图片尺寸
device = torch.device('cuda') # 使用GPU加速推理过程
model = attempt_load(weights, map_location=device) # 加载模型
half = True # 是否开启半精度浮点数支持
if half:
model.half()
cam = RsCamera() # 创建RealSense摄像机实例
while True:
frames = cam.get_frame() # 获取一帧数据
color_image = np.asanyarray(frames.color_frame.get_data())
# 数据预处理...
pred = model(color_image, augment=False)[0]
det = non_max_suppression(pred, conf_thres=0.25, iou_thres=0.45)
for *xyxy, conf, cls in reversed(det[0]):
depth_intrin = frames.depth_frame.profile.as_video_stream_profile().intrinsics
bbox_center_x = int((xyxy[0]+xyxy[2])/2)
bbox_center_y = int((xyxy[1]+xyxy[3])/2)
distance = frames.depth_frame.get_distance(bbox_center_x,bbox_center_y)
camera_coordinate = rs.rs2_deproject_pixel_to_point(depth_intrin,[bbox_center_x,bbox_center_y],distance)
print(f"Detected object at {camera_coordinate}")
detect_objects()
```
此段代码展示了如何从RealSense设备中提取彩色视频流并将其传递给YOLOv5进行分析,同时还演示了怎样依据包围盒中心处的深度测量值得到该点在笛卡尔空间里的确切方位[^1]。
阅读全文
相关推荐









