DepthAI
  • DepthAI Components
    • AprilTags
    • Benchmark
    • Camera
    • Calibration
    • DetectionNetwork
    • Events
    • FeatureTracker
    • Gate
    • HostNodes
    • ImageAlign
    • ImageManip
    • IMU
    • Misc
    • Model Zoo
    • NeuralDepth
    • NeuralNetwork
    • ObjectTracker
    • RecordReplay
    • RGBD
    • Script
    • SpatialDetectionNetwork
    • SpatialLocationCalculator
    • StereoDepth
    • Sync
    • VideoEncoder
    • Visualizer
    • Warp
    • RVC2-specific
  • Advanced Tutorials
  • API Reference
  • Tools
Software Stack

ON THIS PAGE

  • Pipeline
  • Source code

Script Simple

Supported on:RVC2RVC4
This example showcases how to setup the Script node.This example requires the DepthAI v3 API, see installation instructions.

Pipeline

Source code

Python

Python
GitHub
1import depthai as dai
2import time
3
4# Create pipeline
5pipeline = dai.Pipeline()
6script = pipeline.create(dai.node.Script)
7inputQueue = script.inputs["in"].createInputQueue()
8outputQueue = script.outputs["out"].createOutputQueue()
9
10
11script.setScript(
12    """
13    while True:
14        message = node.inputs["in"].get()
15        # Or alternatively:
16        # message = node.io["in"].get()
17        node.warn("I received a message!")
18        node.outputs["out"].send(message)
19        # Or alternatively:
20        # node.io["out"].send(message)
21"""
22)
23
24pipeline.start()
25with pipeline:
26    while pipeline.isRunning():
27        message = dai.ImgFrame()
28        print("Sending a message")
29        inputQueue.send(message)
30        output = outputQueue.get()
31        print("Received a message")
32        time.sleep(1)

C++

1#include <atomic>
2#include <chrono>
3#include <csignal>
4#include <iostream>
5#include <memory>
6#include <thread>
7
8#include "depthai/depthai.hpp"
9
10// Global flag for graceful shutdown
11std::atomic<bool> quitEvent(false);
12
13// Signal handler
14void signalHandler(int signum) {
15    quitEvent = true;
16}
17
18int main() {
19    // Set up signal handlers
20    signal(SIGTERM, signalHandler);
21    signal(SIGINT, signalHandler);
22
23    try {
24        // Create pipeline
25        dai::Pipeline pipeline;
26
27        // Create script node
28        auto script = pipeline.create<dai::node::Script>();
29
30        // Set the script content
31        script->setScript(R"(
32            while True:
33                message = node.inputs["in"].get()
34                # Or alternatively:
35                # message = node.io["in"].get()
36                node.warn("I received a message!")
37                node.outputs["out"].send(message)
38                # Or alternatively:
39                # node.io["out"].send(message)
40        )");
41
42        // Create input and output queues
43        auto inputQueue = script->inputs["in"].createInputQueue();
44        auto outputQueue = script->outputs["out"].createOutputQueue();
45
46        // Start pipeline
47        pipeline.start();
48
49        // Main loop
50        while(pipeline.isRunning() && !quitEvent) {
51            // Create and send a message
52            auto message = std::make_shared<dai::ImgFrame>();
53            std::cout << "Sending a message" << std::endl;
54            inputQueue->send(message);
55
56            // Receive the message
57            auto output = outputQueue->get<dai::ImgFrame>();
58            std::cout << "Received a message" << std::endl;
59
60            // Sleep for 1 second
61            std::this_thread::sleep_for(std::chrono::seconds(1));
62        }
63
64        // Cleanup
65        pipeline.stop();
66        pipeline.wait();
67
68    } catch(const std::exception& e) {
69        std::cerr << "Error: " << e.what() << std::endl;
70        return 1;
71    }
72
73    return 0;
74}

Need assistance?

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