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

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

Setup

Please run the install script to download all required dependencies. Please note that this script must be ran from git context, so you have to download the depthai-python repository first and then run the script

git clone https://github.com/luxonis/depthai-python.git
cd depthai-python/examples
python3 install_requirements.py

For additional information, please follow installation guide

Source code

Also available on GitHub

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python3
import depthai as dai
import time

# Start defining a pipeline
pipeline = dai.Pipeline()

# Script node
script = pipeline.create(dai.node.Script)
script.setScript("""
    buf = NNData(150)
    buf.setLayer("fp16", [1.0, 1.2, 3.9, 5.5])
    buf.setLayer("uint8", [6, 9, 4, 2, 0])
    node.warn("Names of layers: " + str(buf.getAllLayerNames()))
    node.io['host'].send(buf)
""")

# XLinkOut
xout = pipeline.create(dai.node.XLinkOut)
xout.setStreamName('host')
script.outputs['host'].link(xout.input)

# Connect to device with pipeline
with dai.Device(pipeline) as device:
    device.setLogLevel(dai.LogLevel.WARN)
    device.setLogOutputLevel(dai.LogLevel.WARN)

    nndata = device.getOutputQueue("host").get()
    time.sleep(0.5)

    print(f"NNData size: {len(nndata.getData())}")
    print("FP16 values:", nndata.getLayerFp16("fp16"))
    print("UINT8 values:",nndata.getLayerUInt8("uint8"))

Also available on GitHub

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>

// Includes common necessary includes for development using depthai library
#include "depthai/depthai.hpp"

int main() {
    using namespace std;

    // Start defining a pipeline
    dai::Pipeline pipeline;

    // Script node
    auto script = pipeline.create<dai::node::Script>();
    script->setScript(R"(
    buf = NNData(150)
    buf.setLayer("fp16", [1.0, 1.2, 3.9, 5.5])
    buf.setLayer("uint8", [6, 9, 4, 2, 0])
    node.info("Names of layers: " + str(buf.getAllLayerNames()))
    node.io['host'].send(buf)
    )");

    // XLinkOut
    auto xout = pipeline.create<dai::node::XLinkOut>();
    xout->setStreamName("host");
    script->outputs["host"].link(xout->input);

    // Connect to device with pipeline
    dai::Device device(pipeline);

    device.setLogLevel(dai::LogLevel::WARN);
    device.setLogOutputLevel(dai::LogLevel::WARN);

    auto nndata = device.getOutputQueue("host")->get<dai::NNData>();

    std::cout << "NNData size: " << nndata->getData().size() << std::endl;

    std::cout << "FP16 values: ";
    for(auto val : nndata->getLayerFp16("fp16")) {
        std::cout << to_string(val) + " ";
    }
    std::cout << std::endl;

    std::cout << "UINT8 values: ";
    for(auto val : nndata->getLayerUInt8("uint8")) {
        std::cout << to_string(val) + " ";
    }
    std::cout << std::endl;
    return 0;
}

Got questions?

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