# Casting NN subtraction

This example demonstrates how to perform frame subtraction 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 cv2
import depthai as dai
from pathlib import Path

SHAPE = 720

p = dai.Pipeline()

camRgb = p.create(dai.node.ColorCamera)
nn = p.create(dai.node.NeuralNetwork)
script = p.create(dai.node.Script)
rgbXout = p.create(dai.node.XLinkOut)
cast = p.create(dai.node.Cast)
castXout = p.create(dai.node.XLinkOut)

camRgb.setVideoSize(SHAPE, SHAPE)
camRgb.setPreviewSize(SHAPE, SHAPE)
camRgb.setInterleaved(False)

nnBlobPath = (Path(__file__).parent / Path('../models/diff_openvino_2022.1_6shave.blob')).resolve().absolute()
nn.setBlobPath(nnBlobPath)

script.setScript("""
old = node.io['in'].get()
while True:
    frame = node.io['in'].get()
    node.io['img1'].send(old)
    node.io['img2'].send(frame)
    old = frame
""")

rgbXout.setStreamName("rgb")
castXout.setStreamName("cast")
cast.setOutputFrameType(dai.RawImgFrame.Type.GRAY8)

# Linking
camRgb.preview.link(script.inputs['in'])
script.outputs['img1'].link(nn.inputs['img1'])
script.outputs['img2'].link(nn.inputs['img2'])
camRgb.video.link(rgbXout.input)
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:
    qCam = device.getOutputQueue(name="rgb", maxSize=4, blocking=False)
    qCast = device.getOutputQueue(name="cast", maxSize=4, blocking=False)

    while True:
        colorFrame = qCam.get()
        assert isinstance(colorFrame, dai.ImgFrame)
        cv2.imshow("Color", colorFrame.getCvFrame())

        inCast = qCast.get()
        assert isinstance(inCast, dai.ImgFrame)
        cv2.imshow("Diff", inCast.getCvFrame())

        if cv2.waitKey(1) == ord('q'):
            break
```

#### C++

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

constexpr int SHAPE = 720;

int main() {
    dai::Pipeline p;

    auto camRgb = p.create<dai::node::ColorCamera>();
    auto nn = p.create<dai::node::NeuralNetwork>();
    auto script = p.create<dai::node::Script>();
    auto rgbXout = p.create<dai::node::XLinkOut>();
    auto cast = p.create<dai::node::Cast>();
    auto castXout = p.create<dai::node::XLinkOut>();

    camRgb->setVideoSize(SHAPE, SHAPE);
    camRgb->setPreviewSize(SHAPE, SHAPE);
    camRgb->setInterleaved(false);

    nn->setBlobPath(BLOB_PATH);

    script->setScript(R"(
        old = node.io['in'].get()
        while True:
            frame = node.io['in'].get()
            node.io['img1'].send(old)
            node.io['img2'].send(frame)
            old = frame
    )");

    rgbXout->setStreamName("rgb");
    castXout->setStreamName("cast");
    cast->setOutputFrameType(dai::RawImgFrame::Type::GRAY8);

    // Linking
    camRgb->preview.link(script->inputs["in"]);
    script->outputs["img1"].link(nn->inputs["img1"]);
    script->outputs["img2"].link(nn->inputs["img2"]);
    camRgb->video.link(rgbXout->input);
    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 qCam = device.getOutputQueue("rgb", 4, false);
    auto qCast = device.getOutputQueue("cast", 4, false);

    while(true) {
        auto colorFrame = qCam->get<dai::ImgFrame>();
        if(colorFrame) {
            cv::imshow("Color", colorFrame->getCvFrame());
        }

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