# Script NNData example

This example shows how to create a [NNData](https://docs.luxonis.com/software/depthai-components/messages/nn_data.md) message
inside the [Script](https://docs.luxonis.com/software/depthai-components/nodes/script.md) node and then send it to the host (where
it gets printed to the console).

## Demo

```bash
~/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](https://github.com/luxonis/depthai-python/blob/main/examples/install_requirements.py) to download
all required dependencies. Please note that this script must be ran from git context, so you have to download the
[depthai-python](https://github.com/luxonis/depthai-python) repository first and then run the script

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

For additional information, please follow the [installation guide](https://docs.luxonis.com/software/depthai/manual-install.md).

## Source code

#### Python

```python
#!/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"))
```

#### C++

```cpp
#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;
}
```

## Pipeline

### Need assistance?

Head over to [Discussion Forum](https://discuss.luxonis.com/) for technical support or any other questions you might have.
