# Neural Depth Minimal

Minimal example showing basic [NeuralDepth](https://docs.luxonis.com/software-v3/depthai/depthai-components/nodes/neural_depth.md)
usage with disparity output visualization.

## Pipeline

Pipeline nodes:
- Camera(0)
- Sync (3)
- MessageDemux (4)
- Rectification (5)
- NeuralNetwork (6)
- NeuralDepth (2)
- Camera(1)

Pipeline connections:
- Sync (3) -> MessageDemux (4) (out -> input)
- MessageDemux (4) -> Rectification (5) (right -> input2)
- MessageDemux (4) -> Rectification (5) (left -> input1)
- Rectification (5) -> NeuralNetwork (6) (output2 -> right)
- Rectification (5) -> NeuralNetwork (6) (output1 -> left)
- Rectification (5) -> NeuralDepth (2) (output2 -> rightFrameInternal)
- Rectification (5) -> NeuralDepth (2) (output1 -> leftFrameInternal)
- NeuralNetwork (6) -> NeuralDepth (2) (out -> nnDataInput)
- Camera(1) -> Sync (3) (0 -> right)
- Camera(0) -> Sync (3) (0 -> left)

## Source code

#### Python

```python
#!/usr/bin/env python3

import cv2
import depthai as dai

FPS = 25

# Create pipeline
with dai.Pipeline() as pipeline:
    cameraLeft = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_B, sensorFps=FPS)
    cameraRight = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_C, sensorFps=FPS)
    leftOutput = cameraLeft.requestFullResolutionOutput()
    rightOutput = cameraRight.requestFullResolutionOutput()

    neuralDepth = pipeline.create(dai.node.NeuralDepth).build(leftOutput, rightOutput, dai.DeviceModelZoo.NEURAL_DEPTH_LARGE)

    depthQueue = neuralDepth.depth.createOutputQueue()

    # Connect to device and start pipeline
    pipeline.start()
    while pipeline.isRunning():
        depthData = depthQueue.get()
        assert isinstance(depthData, dai.ImgFrame)
        colorizedDepth = dai.utility.colorizeDepthFrame(depthData).getCvFrame()
        cv2.imshow("depth", colorizedDepth)

        key = cv2.waitKey(1)
        if key == ord('q'):
            pipeline.stop()
            break

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

#### C++

```cpp
#include <atomic>
#include <csignal>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <vector>

#include "depthai/depthai.hpp"

// Global flag to allow for a graceful shutdown
std::atomic<bool> quitEvent(false);

void signalHandler(int signum) {
    quitEvent = true;
}

int main() {
    // Set up signal handlers for clean exit on Ctrl+C
    signal(SIGTERM, signalHandler);
    signal(SIGINT, signalHandler);

    constexpr float FPS = 25.0f;

    // Create the DepthAI pipeline
    dai::Pipeline pipeline;

    // Define camera sources for the stereo pair
    auto cameraLeft = pipeline.create<dai::node::Camera>();
    cameraLeft->build(dai::CameraBoardSocket::CAM_B, std::nullopt, FPS);

    auto cameraRight = pipeline.create<dai::node::Camera>();
    cameraRight->build(dai::CameraBoardSocket::CAM_C, std::nullopt, FPS);

    // Request full resolution output from each camera
    auto* leftOutput = cameraLeft->requestFullResolutionOutput();
    auto* rightOutput = cameraRight->requestFullResolutionOutput();

    // Create and build the NeuralDepth node, linking the camera outputs to it
    auto neuralDepth = pipeline.create<dai::node::NeuralDepth>();
    neuralDepth->build(*leftOutput, *rightOutput, dai::DeviceModelZoo::NEURAL_DEPTH_LARGE);

    // Create an output queue to get the depth frames from the node
    auto depthQueue = neuralDepth->depth.createOutputQueue();

    // Start the pipeline
    pipeline.start();

    while(!quitEvent && pipeline.isRunning()) {
        auto depthData = depthQueue->get<dai::ImgFrame>();
        cv::imshow("depth", dai::utility::colorizeDepthFrame(*depthData).getCvFrame());

        // Check for keyboard input to quit
        int key = cv::waitKey(1);
        if(key == 'q') {
            break;
        }
    }

    // The pipeline is stopped automatically when the 'pipeline' object goes out of scope
    // at the end of the main function.
    return 0;
}
```

### Need assistance?

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