# Script forward frames

This example shows how to use [Script](https://docs.luxonis.com/software/depthai-components/nodes/script.md) node to forward
(demultiplex) frames to two different outputs - in this case directly to two
[XLinkOut](https://docs.luxonis.com/software/depthai-components/nodes/xlink_out.md) nodes. Script also changes exposure ratio for
each frame, which results in two streams, one lighter and one darker.

## Demo

## 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 cv2
import depthai as dai

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

cam = pipeline.create(dai.node.ColorCamera)
# Not needed, you can display 1080P frames as well
cam.setIspScale(1,2)

# Script node
script = pipeline.create(dai.node.Script)
script.setScript("""
    ctrl = CameraControl()
    ctrl.setCaptureStill(True)
    # Initially send still event
    node.io['ctrl'].send(ctrl)

    normal = True
    while True:
        frame = node.io['frames'].get()
        if normal:
            ctrl.setAutoExposureCompensation(3)
            node.io['stream1'].send(frame)
            normal = False
        else:
            ctrl.setAutoExposureCompensation(-3)
            node.io['stream2'].send(frame)
            normal = True
        node.io['ctrl'].send(ctrl)
""")
cam.still.link(script.inputs['frames'])

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

xout2 = pipeline.create(dai.node.XLinkOut)
xout2.setStreamName('stream2')
script.outputs['stream2'].link(xout2.input)

script.outputs['ctrl'].link(cam.inputControl)

# Connect to device with pipeline
with dai.Device(pipeline) as device:
    qStream1 = device.getOutputQueue("stream1")
    qStream2 = device.getOutputQueue("stream2")
    while True:
        cv2.imshow('stream1', qStream1.get().getCvFrame())
        cv2.imshow('stream2', qStream2.get().getCvFrame())
        if cv2.waitKey(1) == ord('q'):
            break
```

#### 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;

    // Define a source - color camera
    auto cam = pipeline.create<dai::node::ColorCamera>();
    // Not needed, you can display 1080P frames as well
    cam->setIspScale(1, 2);

    // Script node
    auto script = pipeline.create<dai::node::Script>();
    script->setScript(R"(
    ctrl = CameraControl()
    ctrl.setCaptureStill(True)
    # Initially send still event
    node.io['ctrl'].send(ctrl)

    normal = True
    while True:
        frame = node.io['frames'].get()
        if normal:
            ctrl.setAutoExposureCompensation(3)
            node.io['stream1'].send(frame)
            normal = False
        else:
            ctrl.setAutoExposureCompensation(-3)
            node.io['stream2'].send(frame)
            normal = True
        node.io['ctrl'].send(ctrl)
    )");

    cam->still.link(script->inputs["frames"]);

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

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

    // Connections
    script->outputs["ctrl"].link(cam->inputControl);

    // Connect to device with pipeline
    dai::Device device(pipeline);
    auto qStream1 = device.getOutputQueue("stream1");
    auto qStream2 = device.getOutputQueue("stream2");
    while(true) {
        cv::imshow("stream1", qStream1->get<dai::ImgFrame>()->getCvFrame());
        cv::imshow("stream2", qStream2->get<dai::ImgFrame>()->getCvFrame());
        if(cv::waitKey(1) == 'q') {
            break;
        }
    }
}
```

## Pipeline

### Need assistance?

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