# Script camera control

This example shows how to use [Script](https://docs.luxonis.com/software/depthai-components/nodes/script.md) node. It controls the
[ColorCamera](https://docs.luxonis.com/software/depthai-components/nodes/color_camera.md) to capture a still image every second.

## 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()

# Define a source - color camera
cam = pipeline.create(dai.node.ColorCamera)

# Script node
script = pipeline.create(dai.node.Script)
script.setScript("""
    import time
    ctrl = CameraControl()
    ctrl.setCaptureStill(True)
    while True:
        time.sleep(1)
        node.io['out'].send(ctrl)
""")

# XLinkOut
xout = pipeline.create(dai.node.XLinkOut)
xout.setStreamName('still')

# Connections
script.outputs['out'].link(cam.inputControl)
cam.still.link(xout.input)

# Connect to device with pipeline
with dai.Device(pipeline) as device:
    while True:
        img = device.getOutputQueue("still").get()
        cv2.imshow('still', img.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 colorCam = pipeline.create<dai::node::ColorCamera>();

    // Script node
    auto script = pipeline.create<dai::node::Script>();
    script->setScript(R"(
        import time
        ctrl = CameraControl()
        ctrl.setCaptureStill(True)
        while True:
            time.sleep(1)
            node.io['out'].send(ctrl)
    )");

    // XLinkOut
    auto xout = pipeline.create<dai::node::XLinkOut>();
    xout->setStreamName("still");

    // Connections
    script->outputs["out"].link(colorCam->inputControl);
    colorCam->still.link(xout->input);

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

    return 0;
}
```

## Pipeline

### Need assistance?

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