# Casting NN Blur

This example demonstrates how to apply a blur effect using a neural network and the
[Cast](https://docs.luxonis.com/software/depthai-components/nodes/cast_node.md) node. The output of the cast node can be used in
second stage neural networks or for further processing.

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

SHAPE = 300

p = dai.Pipeline()

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

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

nnBlobPath = (Path(__file__).parent / Path('../models/blur_simplified_openvino_2021.4_6shave.blob')).resolve().absolute()

nn.setBlobPath(nnBlobPath)

rgbOut.setStreamName("rgb")

castXout.setStreamName("cast")

cast.setOutputFrameType(dai.RawImgFrame.Type.BGR888p)

# Linking
camRgb.preview.link(nn.input)
camRgb.preview.link(rgbOut.input)
nn.out.link(cast.input)
cast.output.link(castXout.input)

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:
        inCast = qCast.get()
        assert isinstance(inCast, dai.ImgFrame)
        inRgb = qCam.get()
        assert isinstance(inRgb, dai.ImgFrame)
        cv2.imshow("Blur", inCast.getCvFrame())
        cv2.imshow("Original", inRgb.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 nn = p.create<dai::node::NeuralNetwork>();
    auto rgbOut = p.create<dai::node::XLinkOut>();
    auto cast = p.create<dai::node::Cast>();
    auto castXout = p.create<dai::node::XLinkOut>();

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

    nn->setBlobPath(BLOB_PATH);

    rgbOut->setStreamName("rgb");
    castXout->setStreamName("cast");

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

    // Linking
    camRgb->preview.link(nn->input);
    camRgb->preview.link(rgbOut->input);
    nn->out.link(cast->input);
    cast->output.link(castXout->input);

    dai::Device device(p);
    auto qCam = device.getOutputQueue("rgb", 4, false);
    auto qCast = device.getOutputQueue("cast", 4, false);

    while(true) {
        auto inCast = qCast->get<dai::ImgFrame>();
        auto inRgb = qCam->get<dai::ImgFrame>();

        if(inCast && inRgb) {
            cv::imshow("Blur", inCast->getCvFrame());
            cv::imshow("Original", inRgb->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.
