DepthAI v2 has been superseded by DepthAI v3. You are viewing legacy documentation.
DepthAI 教程
DepthAI API 参考

本页目录

  • Demo
  • 设置
  • 源代码
  • 流水线

Script NNData 示例

本示例展示了如何在 Script 节点内创建 NNData 消息,然后将其发送到主机(在主机上打印到控制台)。

Demo

Command Line
1~/depthai-python/examples/Script$ python3 script_nndata_datatype.py
2Names of layers: ['fp16', 'uint8']
3NNData size: 13
4FP16 values: [1.0, 1.2001953125, 3.900390625, 5.5]
5UINT8 values: [6, 9, 4, 2, 0]

设置

请运行安装脚本以下载所有必需的依赖项。请注意,此脚本必须在 git 上下文中运行,因此您需要先下载 depthai-python 仓库,然后运行该脚本。
Command Line
1git clone https://github.com/luxonis/depthai-python.git
2cd depthai-python/examples
3python3 install_requirements.py
更多信息,请参考安装指南

源代码

Python

Python
GitHub
1#!/usr/bin/env python3
2import depthai as dai
3import time
4
5# Start defining a pipeline
6pipeline = dai.Pipeline()
7
8# Script node
9script = pipeline.create(dai.node.Script)
10script.setScript("""
11    buf = NNData(150)
12    buf.setLayer("fp16", [1.0, 1.2, 3.9, 5.5])
13    buf.setLayer("uint8", [6, 9, 4, 2, 0])
14    node.warn("Names of layers: " + str(buf.getAllLayerNames()))
15    node.io['host'].send(buf)
16""")
17
18# XLinkOut
19xout = pipeline.create(dai.node.XLinkOut)
20xout.setStreamName('host')
21script.outputs['host'].link(xout.input)
22
23# Connect to device with pipeline
24with dai.Device(pipeline) as device:
25    device.setLogLevel(dai.LogLevel.WARN)
26    device.setLogOutputLevel(dai.LogLevel.WARN)
27
28    nndata = device.getOutputQueue("host").get()
29    time.sleep(0.5)
30
31    print(f"NNData size: {len(nndata.getData())}")
32    print("FP16 values:", nndata.getLayerFp16("fp16"))
33    print("UINT8 values:",nndata.getLayerUInt8("uint8"))

C++

1#include <iostream>
2
3// Includes common necessary includes for development using depthai library
4#include "depthai/depthai.hpp"
5
6int main() {
7    using namespace std;
8
9    // Start defining a pipeline
10    dai::Pipeline pipeline;
11
12    // Script node
13    auto script = pipeline.create<dai::node::Script>();
14    script->setScript(R"(
15    buf = NNData(150)
16    buf.setLayer("fp16", [1.0, 1.2, 3.9, 5.5])
17    buf.setLayer("uint8", [6, 9, 4, 2, 0])
18    node.info("Names of layers: " + str(buf.getAllLayerNames()))
19    node.io['host'].send(buf)
20    )");
21
22    // XLinkOut
23    auto xout = pipeline.create<dai::node::XLinkOut>();
24    xout->setStreamName("host");
25    script->outputs["host"].link(xout->input);
26
27    // Connect to device with pipeline
28    dai::Device device(pipeline);
29
30    device.setLogLevel(dai::LogLevel::WARN);
31    device.setLogOutputLevel(dai::LogLevel::WARN);
32
33    auto nndata = device.getOutputQueue("host")->get<dai::NNData>();
34
35    std::cout << "NNData size: " << nndata->getData().size() << std::endl;
36
37    std::cout << "FP16 values: ";
38    for(auto val : nndata->getLayerFp16("fp16")) {
39        std::cout << to_string(val) + " ";
40    }
41    std::cout << std::endl;
42
43    std::cout << "UINT8 values: ";
44    for(auto val : nndata->getLayerUInt8("uint8")) {
45        std::cout << to_string(val) + " ";
46    }
47    std::cout << std::endl;
48    return 0;
49}

流水线

需要帮助?

请前往 OAKChina 官网 获取技术支持或解答您的任何疑问。