# Casting NN Concatenation

This example demonstrates how to concatenate frames from multiple cameras (RGB, left, and right) using a
[NeuralNetwork](https://docs.luxonis.com/software/depthai-components/nodes/neural_network.md) and the
[Cast](https://docs.luxonis.com/software/depthai-components/nodes/cast_node.md) node.

## 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 numpy as np
import cv2
import depthai as dai
from pathlib import Path

SHAPE = 300

p = dai.Pipeline()

camRgb = p.create(dai.node.ColorCamera)
left = p.create(dai.node.MonoCamera)
right = p.create(dai.node.MonoCamera)
manipLeft = p.create(dai.node.ImageManip)
manipRight = p.create(dai.node.ImageManip)
nn = p.create(dai.node.NeuralNetwork)
cast = p.create(dai.node.Cast)
castXout = p.create(dai.node.XLinkOut)

camRgb.setPreviewSize(SHAPE, SHAPE)
camRgb.setInterleaved(False)
camRgb.setColorOrder(dai.ColorCameraProperties.ColorOrder.BGR)

left.setCamera("left")
left.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
manipLeft.initialConfig.setResize(SHAPE, SHAPE)
manipLeft.initialConfig.setFrameType(dai.ImgFrame.Type.BGR888p)

right.setCamera("right")
right.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
manipRight.initialConfig.setResize(SHAPE, SHAPE)
manipRight.initialConfig.setFrameType(dai.ImgFrame.Type.BGR888p)

nnBlobPath = (Path(__file__).parent / Path('../models/concat_openvino_2021.4_6shave.blob')).resolve().absolute()
nn.setBlobPath(nnBlobPath)
nn.setNumInferenceThreads(2)

castXout.setStreamName("cast")
cast.setOutputFrameType(dai.ImgFrame.Type.BGR888p)

# Linking
left.out.link(manipLeft.inputImage)
right.out.link(manipRight.inputImage)
manipLeft.out.link(nn.inputs['img1'])
camRgb.preview.link(nn.inputs['img2'])
manipRight.out.link(nn.inputs['img3'])
nn.out.link(cast.input)
cast.output.link(castXout.input)

# Pipeline is defined, now we can connect to the device
with dai.Device(p) as device:
    qCast = device.getOutputQueue(name="cast", maxSize=4, blocking=False)

    while True:
        inCast = qCast.get()
        assert isinstance(inCast, dai.ImgFrame)
        cv2.imshow("Concated frames", inCast.getCvFrame())

        if cv2.waitKey(1) == ord('q'):
            break
```

#### C++

```cpp
#include <depthai/depthai.hpp>
#include <opencv2/opencv.hpp>

constexpr int SHAPE = 300;

int main() {
    dai::Pipeline p;

    auto camRgb = p.create<dai::node::ColorCamera>();
    auto left = p.create<dai::node::MonoCamera>();
    auto right = p.create<dai::node::MonoCamera>();
    auto manipLeft = p.create<dai::node::ImageManip>();
    auto manipRight = p.create<dai::node::ImageManip>();
    auto nn = p.create<dai::node::NeuralNetwork>();
    auto cast = p.create<dai::node::Cast>();
    auto castXout = p.create<dai::node::XLinkOut>();

    camRgb->setPreviewSize(SHAPE, SHAPE);
    camRgb->setInterleaved(false);
    camRgb->setColorOrder(dai::ColorCameraProperties::ColorOrder::BGR);

    left->setCamera("left");
    left->setResolution(dai::MonoCameraProperties::SensorResolution::THE_400_P);
    manipLeft->initialConfig.setResize(SHAPE, SHAPE);
    manipLeft->initialConfig.setFrameType(dai::ImgFrame::Type::BGR888p);

    right->setCamera("right");
    right->setResolution(dai::MonoCameraProperties::SensorResolution::THE_400_P);
    manipRight->initialConfig.setResize(SHAPE, SHAPE);
    manipRight->initialConfig.setFrameType(dai::ImgFrame::Type::BGR888p);

    nn->setBlobPath(BLOB_PATH);
    nn->setNumInferenceThreads(2);

    castXout->setStreamName("cast");
    cast->setOutputFrameType(dai::ImgFrame::Type::BGR888p);

    // Linking
    left->out.link(manipLeft->inputImage);
    right->out.link(manipRight->inputImage);
    manipLeft->out.link(nn->inputs["img1"]);
    camRgb->preview.link(nn->inputs["img2"]);
    manipRight->out.link(nn->inputs["img3"]);
    nn->out.link(cast->input);
    cast->output.link(castXout->input);

    // Pipeline is defined, now we can connect to the device
    dai::Device device(p);
    auto qCast = device.getOutputQueue("cast", 4, false);

    while(true) {
        auto inCast = qCast->get<dai::ImgFrame>();
        if(inCast) {
            cv::imshow("Concated frames", inCast->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.
