Script

Script node allows users to run custom Python scripts on the device. Due to the computational resource constraints, script node shouldn’t be used for heavy computing (eg. image manipulation/CV), but for managing the flow of the pipeline. Example use cases would be controlling nodes like ImageManip, ColorCamera, SpatialLocationCalculator, decoding NeuralNetwork results, or interfacing with GPIOs.

How to place it

pipeline = dai.Pipeline()
script = pipeline.create(dai.node.Script)
dai::Pipeline pipeline;
auto script = pipeline.create<dai::node::Script>();

Inputs and Outputs

inputs[]  ┌──────────────┐  outputs[]
---------►│              ├-----------►
---------►│              ├-----------►
   ...    │    Script    |    ...
   ...    │              │    ...
---------►│              ├-----------►
          └──────────────┘

Users can define as many inputs and outputs as they need. Inputs and outputs can be any Message type.

Usage

script = pipeline.create(dai.node.Script)
script.setScript("""
    import time
    import marshal
    num = 123
    node.warn(f"Number {num}") # Print to host
    x = [1, "Hello", {"Foo": "Bar"}]
    x_serial = marshal.dumps(x)
    b = Buffer(len(x_serial))
    while True:
        time.sleep(1)
        b.getData()[:] = x_serial
        node.io['out'].send(b)
""")
script.outputs['out'].link(xout.input)

# ...
# After initializing the device, enable log levels
device.setLogLevel(dai.LogLevel.WARN)
device.setLogOutputLevel(dai.LogLevel.WARN)
auto script = pipeline.create<dai::node::Script>();
script->setScript(R"(
    import time
    import marshal
    num = 123
    node.warn(f"Number {num}") # Print to host
    x = [1, "Hello", {"Foo": "Bar"}]
    x_serial = marshal.dumps(x)
    b = Buffer(len(x_serial))
    while True:
        time.sleep(1)
        b.getData()[:] = x_serial
        node.io['out'].send(b)
)");
script->outputs["out"].link(xout->input);

// ...
// After initializing the device, enable log levels
device.setLogLevel(dai::LogLevel.WARN);
device.setLogOutputLevel(dai::LogLevel.WARN);

Interfacing with GPIOs

In the script node you can interface with GPIOs of the VPU. Currently supported functions are:

import GPIO # module
GPIO.read(pin)
GPIO.write(pin, value)
GPIO.setPwm(pin, highCount, lowCount, repeat=0) # repeat == 0 means indefinite
GPIO.enablePwm(pin, enable)

Using DepthAI Messages

The depthai module is implicitly imported to the script node. You can create new depthai messages and assign data to it, for example:

buf = Buffer(100) # Assign 100 bytes to the Buffer message

# Create CameraControl message, set manual focus
control = CameraControl()
control.setManualFocus(100)

imgFrame = ImgFrame(300*300*3) # Buffer with 300x300x3 bytes

Examples of functionality

Reference

class depthai.node.Script
class Connection

Connection between an Input and Output

class Id

Node identificator. Unique for every node on a single Pipeline

Properties

alias of depthai.ScriptProperties

getAssetManager(*args, **kwargs)

Overloaded function.

  1. getAssetManager(self: depthai.Node) -> depthai.AssetManager

Get node AssetManager as a const reference

  1. getAssetManager(self: depthai.Node) -> depthai.AssetManager

Get node AssetManager as a const reference

getInputRefs(*args, **kwargs)

Overloaded function.

  1. getInputRefs(self: depthai.Node) -> List[depthai.Node.Input]

Retrieves reference to node inputs

  1. getInputRefs(self: depthai.Node) -> List[depthai.Node.Input]

Retrieves reference to node inputs

getInputs(self: depthai.Node) → List[depthai.Node.Input]

Retrieves all nodes inputs

getName(self: depthai.Node)str

Retrieves nodes name

getOutputRefs(*args, **kwargs)

Overloaded function.

  1. getOutputRefs(self: depthai.Node) -> List[depthai.Node.Output]

Retrieves reference to node outputs

  1. getOutputRefs(self: depthai.Node) -> List[depthai.Node.Output]

Retrieves reference to node outputs

getOutputs(self: depthai.Node) → List[depthai.Node.Output]

Retrieves all nodes outputs

getParentPipeline(*args, **kwargs)

Overloaded function.

  1. getParentPipeline(self: depthai.Node) -> depthai.Pipeline

  2. getParentPipeline(self: depthai.Node) -> depthai.Pipeline

getProcessor(self: depthai.node.Script)depthai.ProcessorType

Get on which processor the script should run

Returns

Processor type - Leon CSS or Leon MSS

getScriptName(self: depthai.node.Script)str

Get filesystem path from where script was loaded. If script wasn’t set by path, function returns empty string

getScriptPath(self: depthai.node.Script)str

Get filesystem path from where script was loaded. If script wasn’t set by path, function returns empty string

property id

Id of node

setProcessor(self: depthai.node.Script, arg0: depthai.ProcessorType)None

Set on which processor the script should run

Parameter type:

Processor type - Leon CSS or Leon MSS

setScript(*args, **kwargs)

Overloaded function.

  1. setScript(self: depthai.node.Script, script: str, name: str = ‘’) -> None

Sets script data to be interpreted

Parameter script:

Script string to be interpreted

  1. setScript(self: depthai.node.Script, data: List[int], name: str = ‘’) -> None

Sets script data to be interpreted

Parameter data:

Binary data that represents the script to be interpreted

setScriptPath(self: depthai.node.Script, arg0: str)None

Specify local filesystem path to load the script

class dai::node::Script : public dai::Node

Public Types

using Properties = dai::ScriptProperties

Public Functions

std::string getName() const override

Retrieves nodes name.

Script(const std::shared_ptr<PipelineImpl> &par, int64_t nodeId)
void setScriptPath(const std::string &path)

Specify local filesystem path to load the script

void setScript(const std::string &script, const std::string &name = "")

Sets script data to be interpreted

Parameters
  • script: Script string to be interpreted

void setScript(const std::vector<std::uint8_t> &data, const std::string &name = "")

Sets script data to be interpreted

Parameters
  • data: Binary data that represents the script to be interpreted

std::string getScriptPath() const

Get filesystem path from where script was loaded. If script wasn’t set by path, function returns empty string

std::string getScriptName() const

Get filesystem path from where script was loaded. If script wasn’t set by path, function returns empty string

void setProcessor(ProcessorType type)

Set on which processor the script should run

Parameters
  • type: Processor type - Leon CSS or Leon MSS

ProcessorType getProcessor() const

Get on which processor the script should run

Return

Processor type - Leon CSS or Leon MSS

Public Members

InputMap inputs

Inputs to Script node. Can be accessed using subscript operator (Eg: inputs[‘in1’]) By default inputs are set to blocking with queue size 8

OutputMap outputs

Outputs from Script node. Can be accessed subscript operator (Eg: outputs[‘out1’])

Private Functions

nlohmann::json getProperties() override
std::shared_ptr<Node> clone() override

Private Members

dai::ScriptProperties properties
std::string scriptPath = ""

Got questions?

We’re always happy to help with code or other questions you might have.