YOLO + 3D 空间定位 + 抓取姿态 完整方案

我给你工业级最简实现 :相机识别物体 → 输出 3D 坐标 (X,Y,Z) + 旋转角 → 生成机械臂可直接用的抓取姿态。不用复杂配置,Python + YOLO 直接跑。


一、你需要的硬件(二选一)

方案 A(最简单):深度相机

  • 推荐:Intel RealSense D435i / D455
  • 优势:直接输出 3D 坐标,不用标定,10 分钟出结果

方案 B(普通相机):2D 相机 + 标定

  • 需要:标定板 + 手眼标定
  • 适合固定高度场景

我下面给你【深度相机 + YOLO】的完整代码,开箱即用!


二、安装依赖

bash

运行

复制代码
pip install ultralytics opencv-python pyrealsense2 numpy transforms3d

三、完整源码:YOLO 识别 + 3D 坐标 + 抓取姿态

python

运行

复制代码
import cv2
import numpy as np
import pyrealsense2 as rs
from ultralytics import YOLO
import transforms3d.euler as euler

# ====================== 1. 初始化 YOLO ======================
model = YOLO("yolov8n.pt")  # 换成你训练的自定义模型
TARGET_CLASS = 0  # 0=人,可改成你要识别的物体类别

# ====================== 2. 初始化深度相机 ======================
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)

# 对齐深度图到彩色图(关键!3D定位必须)
align_to = rs.stream.color
align = rs.align(align_to)

# 启动相机
pipeline.start(config)

# 获取相机内参(用于像素转3D)
profile = pipeline.get_active_profile()
intr = profile.get_stream(rs.stream.color).as_video_stream_profile().get_intrinsics()

print("相机启动成功,开始识别...")

# ====================== 3. 主循环 ======================
while True:
    # 获取图像
    frames = pipeline.wait_for_frames()
    aligned_frames = align.process(frames)
    
    color_frame = aligned_frames.get_color_frame()
    depth_frame = aligned_frames.get_depth_frame()
    
    if not color_frame or not depth_frame:
        continue

    # 转成OpenCV格式
    img = np.asanyarray(color_frame.get_data())
    depth_img = np.asanyarray(depth_frame.get_data())

    # YOLO 检测
    results = model(img, classes=[TARGET_CLASS])

    for result in results:
        boxes = result.boxes
        for box in boxes:
            # 1. 获取2D框中心
            x1, y1, x2, y2 = map(int, box.xyxy[0])
            cx = (x1 + x2) // 2
            cy = (y1 + y2) // 2

            # 2. 获取深度值 Z
            z = depth_frame.get_distance(cx, cy)
            if z < 0.1 or z > 3.0:  # 过滤无效距离
                continue

            # 3. 像素坐标 → 3D空间坐标(相机坐标系)
            X = (cx - intr.ppx) * z / intr.fx
            Y = (cy - intr.ppy) * z / intr.fy
            Z = z

            # 4. 计算抓取姿态(默认垂直向下抓取)
            # 欧拉角:roll, pitch, yaw(可直接发给机械臂)
            roll = 0.0
            pitch = 0.0
            yaw = 0.0

            # 转四元数(机械臂常用)
            quat = euler.euler2quat(roll, pitch, yaw, axes='sxyz')

            # ====================== 输出结果 ======================
            print(f"\n===== 抓取位姿 =====")
            print(f"3D坐标 X: {X:.3f}  Y: {Y:.3f}  Z: {Z:.3f} (米)")
            print(f"欧拉角 R:{roll:.2f} P:{pitch:.2f} Y:{yaw:.2f}")
            print(f"四元数 W:{quat[0]:.3f} X:{quat[1]:.3f} Y:{quat[2]:.3f} Z:{quat[3]:.3f}")

            # 画框
            cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
            cv2.circle(img, (cx, cy), 5, (0, 0, 255), -1)
            cv2.putText(img, f"X:{X:.2f} Y:{Y:.2f} Z:{Z:.2f}",
                        (cx-80, cy-20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,0,0), 2)

    # 显示画面
    cv2.imshow("YOLO 3D抓取定位", img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

pipeline.stop()
cv2.destroyAllWindows()

四、输出说明(机械臂直接可用)

程序会实时输出:

  1. 3D 空间坐标

    • X:左右
    • Y:前后
    • Z:高度(距离相机)
  2. 抓取姿态

    • 欧拉角 Roll / Pitch / Yaw
    • 四元数 WXYZ

这些数据可以直接发给:

  • 机械臂(Robotiq、遨博、优傲、埃夫特等)
  • PLC
  • 运动控制器
相关推荐
YOLO数据集集合5 小时前
红外无人机与鸟类目标检测数据集 | 红外检测 无人机识别 鸟类检测 低空安防 反无人机 yolov11模型9151期
yolo·目标检测·无人机
创世虚拟世界6 小时前
从零打通 MCP 访问创世虚拟世界CRM的 3D 数据,我搭了一个可直接调用的演示站
3d
李游Leo11 小时前
HarmonyOS 7 + Spatial Recon Kit-C++:3DGS 高斯参数的非有限值隔离与可渲染性门禁【鸿蒙心迹】
开发语言·c++·3d·harmonyos
迁移科技11 小时前
小体积蕴含大能量:Epic Eye Pixel Mini轻量化相机的应用美学
人工智能·3d·自动化·视觉检测
牧子川13 小时前
YOLO-AMC:裂缝检测再进化,给 YOLOv11 换上更合适的「注意力」
yolo·目标检测·yolo11·裂缝检测
Martina_032113 小时前
同一片森林换个镜头就忽冷忽暖?用6步统一曝光、LUT与相机后处理
人工智能·游戏·数学建模·3d·自然语言处理·aigc·游戏策划
Better Bench15 小时前
奇异值分解(SVD)在3D骨骼旋转中的应用:原理与实现
3d·人体姿态估计·骨骼关键点
追烽少年x16 小时前
从零构建一个3D点云预览器:Python + PySide6 + pyqtgraph 实战
python·3d
FL16238631291 天前
无人机视角海边沙滩垃圾检测数据集VOC+YOLO格式603张3类别
人工智能·yolo
YOLO数据集集合1 天前
光伏组件异常检测数据集 | 光伏组件 异常检测 红外检测 热斑识别 光伏巡检9147期
人工智能·yolo·目标检测·语言模型·太阳能板·光伏组件