# Script NNData example

This example shows how to create a [NNData](https://docs.luxonis.com/software-v3/depthai/depthai-components/messages/nn_data.md)
message inside the [Script](https://docs.luxonis.com/software-v3/depthai/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]
```

This example requires the DepthAI v3 API, see [installation instructions](https://docs.luxonis.com/software-v3/depthai.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

### examples/script_nndata_example.pipeline.json

```json
{
  "pipeline": {
    "connections": [
      {
        "node1Id": 0,
        "node1Output": "host",
        "node1OutputGroup": "io",
        "node2Id": 1,
        "node2Input": "in",
        "node2InputGroup": ""
      }
    ],
    "globalProperties": {
      "calibData": null,
      "cameraTuningBlobSize": null,
      "cameraTuningBlobUri": "",
      "leonCssFrequencyHz": 700000000.0,
      "leonMssFrequencyHz": 700000000.0,
      "pipelineName": null,
      "pipelineVersion": null,
      "sippBufferSize": 18432,
      "sippDmaBufferSize": 16384,
      "xlinkChunkSize": -1
    },
    "nodes": [
      [
        0,
        {
          "id": 0,
          "ioInfo": [
            [
              [
                "io",
                "host"
              ],
              {
                "blocking": false,
                "group": "io",
                "id": 1,
                "name": "host",
                "queueSize": 8,
                "type": 0,
                "waitForMessage": false
              }
            ]
          ],
          "name": "Script",
          "properties": {
            "processor": 1,
            "scriptName": "<script>",
            "scriptUri": "asset:__script"
          }
        }
      ],
      [
        1,
        {
          "id": 1,
          "ioInfo": [
            [
              [
                "",
                "in"
              ],
              {
                "blocking": true,
                "group": "",
                "id": 2,
                "name": "in",
                "queueSize": 8,
                "type": 3,
                "waitForMessage": true
              }
            ]
          ],
          "name": "XLinkOut",
          "properties": {
            "maxFpsLimit": -1.0,
            "metadataOnly": false,
            "streamName": "host"
          }
        }
      ]
    ]
  }
}
```

### Need assistance?

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