此页面由 AI 自动翻译。查看英文原版

本页目录

  • 演示
  • 源代码
  • 工作原理

IMU 和视频同步

此示例演示了如何使用 DepthAI 的 Sync 节点将 IMU(惯性测量单元)数据与彩色摄像头的视频帧同步。它重点介绍了实时处理和显示 IMU 的最新旋转矢量以及视频流的功能。

类似示例:

演示

Command Line
1~/depthai-python/examples/Sync $ python3 imu_video_synced.py
2IMU 类型:BNO086,固件版本:3.9.7
3
4设备时间戳 imu:0:00:05.379914
5设备时间戳 video:0:00:05.385096
6四元数:i: -0.0549 j: -0.0335 k: 0.0018 real: 0.9979
7
8设备时间戳 imu:0:00:05.410274
9设备时间戳 video:0:00:05.418425
10四元数:i: -0.0549 j: -0.0334 k: 0.0018 real: 0.9979
11
12设备时间戳 imu:0:00:05.445439
13设备时间戳 video:0:00:05.451753
14四元数:i: -0.0548 j: -0.0334 k: 0.0018 real: 0.9979
15
16设备时间戳 imu:0:00:05.475084
17设备时间戳 video:0:00:05.485082
18四元数:i: -0.0547 j: -0.0334 k: 0.0018 real: 0.9979
19
20设备时间戳 imu:0:00:05.510046
21设备时间戳 video:0:00:05.518411
22四元数:i: -0.0546 j: -0.0334 k: 0.0018 real: 0.9979
此示例需要 DepthAI v3 API,请参阅 安装说明

源代码

Python

Python
GitHub
1import depthai as dai
2import numpy as np
3import cv2
4from datetime import timedelta
5
6device = dai.Device()
7
8imuType = device.getConnectedIMU()
9imuFirmwareVersion = device.getIMUFirmwareVersion()
10print(f"IMU type: {imuType}, firmware version: {imuFirmwareVersion}")
11
12if imuType != "BNO086":
13    print("Rotation vector output is supported only by BNO086!")
14    exit(0)
15
16pipeline = dai.Pipeline()
17
18color = pipeline.create(dai.node.ColorCamera)
19imu = pipeline.create(dai.node.IMU)
20sync = pipeline.create(dai.node.Sync)
21xoutImu = pipeline.create(dai.node.XLinkOut)
22xoutImu.setStreamName("imu")
23
24xoutGrp = pipeline.create(dai.node.XLinkOut)
25xoutGrp.setStreamName("xout")
26
27color.setCamera("color")
28
29imu.enableIMUSensor(dai.IMUSensor.ROTATION_VECTOR, 120)
30imu.setBatchReportThreshold(1)
31imu.setMaxBatchReports(10)
32
33sync.setSyncThreshold(timedelta(milliseconds=10))
34sync.setSyncAttempts(-1)
35
36color.video.link(sync.inputs["video"])
37imu.out.link(sync.inputs["imu"])
38
39sync.out.link(xoutGrp.input)
40
41
42with device:
43    device.startPipeline(pipeline)
44    groupQueue = device.getOutputQueue("xout", 3, True)
45    while True:
46        groupMessage = groupQueue.get()
47        imuMessage = groupMessage["imu"]
48        colorMessage = groupMessage["video"]
49        print()
50        print("Device timestamp imu: " + str(imuMessage.getTimestampDevice()))
51        print("Device timestamp video:" + str(colorMessage.getTimestampDevice()))
52        latestRotationVector = imuMessage.packets[-1].rotationVector
53        imuF = "{:.4f}"
54        print(f"Quaternion: i: {imuF.format(latestRotationVector.i)} j: {imuF.format(latestRotationVector.j)} "
55        f"k: {imuF.format(latestRotationVector.k)} real: {imuF.format(latestRotationVector.real)}")
56        print()
57        cv2.imshow("video", colorMessage.getCvFrame())
58        if cv2.waitKey(1) == ord("q"):
59            break

C++

1#include <iostream>
2#include <opencv2/opencv.hpp>
3
4#include "depthai/depthai.hpp"
5
6int main() {
7    dai::Device device;
8
9    auto imuType = device.getConnectedIMU();
10    auto imuFirmwareVersion = device.getIMUFirmwareVersion();
11    std::cout << "IMU type: " << imuType << ", firmware version: " << imuFirmwareVersion << std::endl;
12
13    if(imuType != "BNO086") {
14        std::cout << "Rotation vector output is supported only by BNO086!" << std::endl;
15        return 1;
16    }
17
18    dai::Pipeline pipeline;
19
20    auto colorCamera = pipeline.create<dai::node::ColorCamera>();
21    auto imu = pipeline.create<dai::node::IMU>();
22    auto sync = pipeline.create<dai::node::Sync>();
23    auto xoutGroup = pipeline.create<dai::node::XLinkOut>();
24
25    xoutGroup->setStreamName("xout");
26
27    colorCamera->setCamera("color");
28
29    imu->enableIMUSensor(dai::IMUSensor::ROTATION_VECTOR, 120);
30    imu->setBatchReportThreshold(1);
31    imu->setMaxBatchReports(10);
32
33    sync->setSyncThreshold(std::chrono::milliseconds(10));
34    sync->setSyncAttempts(-1);  // Infinite attempts
35
36    colorCamera->video.link(sync->inputs["video"]);
37    imu->out.link(sync->inputs["imu"]);
38
39    sync->out.link(xoutGroup->input);
40
41    device.startPipeline(pipeline);
42
43    auto groupQueue = device.getOutputQueue("xout", 3, false);
44
45    while(true) {
46        auto groupMessage = groupQueue->get<dai::MessageGroup>();
47        auto imuData = groupMessage->get<dai::IMUData>("imu");
48        auto colorData = groupMessage->get<dai::ImgFrame>("video");
49        auto timeDifference = imuData->getTimestampDevice() - colorData->getTimestampDevice();
50        auto timeDifferenceUs = std::chrono::duration_cast<std::chrono::microseconds>(timeDifference).count();
51
52        std::cout << "Time difference between messages is: " << std::abs(timeDifferenceUs / 1000.0) << " ms" << std::endl;
53
54        for(auto& packet : imuData->packets) {
55            auto& rv = packet.rotationVector;
56
57            printf(
58                "Quaternion: i: %.3f j: %.3f k: %.3f real: %.3f\n"
59                "Accuracy (rad): %.3f \n",
60                rv.i,
61                rv.j,
62                rv.k,
63                rv.real,
64                rv.rotationVectorAccuracy);
65        }
66
67        cv::imshow("Color", colorData->getCvFrame());
68        if(cv::waitKey(1) == 'q') {
69            break;
70        }
71    }
72
73    return 0;
74}

工作原理

  • 初始化 DepthAI 设备。
  • 检查连接的 IMU 类型和固件版本。
  • 创建一个管道并添加 ColorCamera 和 IMU 节点。
  • 设置 Sync 节点以将 IMU 数据与视频帧同步。
  • 将 ColorCamera 和 IMU 节点的输出链接到 Sync 节点。
  • 启动管道并持续接收同步数据。
  • 显示视频帧并打印 IMU 旋转矢量数据,包括四元数值。

需要帮助?

请前往 Discussion Forum 获取技术支持或提出您可能有的任何其他问题。