ON THIS PAGE

  • Demo
  • Source code
  • Pipeline

Script NNData example

This example shows how to create a NNData message inside the Script node and then send it to the host (where it gets printed to the console).

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]
This example requires the DepthAI v3 API, see installation instructions.

Source code

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}

Pipeline

Need assistance?

Head over to Discussion Forum for technical support or any other questions you might have.