Software Stack
DepthAI

ON THIS PAGE

  • Neural Assisted Stereo
  • Source code

Neural Assisted Stereo

Supported on:RVC4
This example demonstrates the NeuralAssistedStereo node, which fuses NeuralDepth with classical StereoDepth.This example requires the DepthAI v3 API, see installation instructions.

Source code

Python
C++

Python

Python
GitHub
1import numpy as np
2import cv2 as cv
3import depthai as dai
4
5FPS = 20
6
7def showDepth(depthFrame, windowName="Depth", minDistance=500, maxDistance=5000,
8               colormap=cv.COLORMAP_TURBO, useLog=False):
9    """
10    Nicely visualize a depth map.
11
12    Args:
13        depthFrame (np.ndarray): Depth frame (in millimeters).
14        windowName (str): OpenCV window name.
15        minDistance (int): Minimum depth to display (in mm).
16        maxDistance (int): Maximum depth to display (in mm).
17        colormap (int): OpenCV colormap (e.g., cv.COLORMAP_JET, COLORMAP_TURBO, etc.).
18        useLog (bool): Apply logarithmic scaling for better visual contrast.
19
20    Example:
21        frame = depth.getCvFrame()
22        showDepth(frame)
23    """
24    # Convert to float for processing
25    depthFrame = depthFrame.astype(np.float32)
26
27    # Optionally apply log scaling
28    if useLog:
29        depthFrame = np.log(depthFrame + 1)
30
31    # Clip to defined range (avoid far-out values)
32    depthFrame = np.uint8(np.clip(depthFrame, minDistance, maxDistance) / maxDistance * 255)
33
34    # Apply color map
35    depthColor = cv.applyColorMap(depthFrame, colormap)
36
37    # Show in a window
38    cv.imshow(windowName, depthColor)
39
40if __name__ == "__main__":
41    device = dai.Device()
42    pipeline = dai.Pipeline(device)
43    if not device.isNeuralDepthSupported():
44        print("Exiting NeuralAssistedStereo example: device doesn't support NeuralDepth.")
45        exit()
46
47    monoLeft = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_B, sensorFps=FPS)
48    monoRight = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_C, sensorFps=FPS)
49
50    monoLeftOut = monoLeft.requestFullResolutionOutput()
51    monoRightOut = monoRight.requestFullResolutionOutput()
52
53    neuralAssistedStereo = pipeline.create(dai.node.NeuralAssistedStereo).build(monoLeftOut, monoRightOut, neuralModel=dai.DeviceModelZoo.NEURAL_DEPTH_NANO)
54
55    disparityQueue = neuralAssistedStereo.disparity.createOutputQueue()
56
57    with pipeline:
58        pipeline.start()
59        while pipeline.isRunning():
60            disparity = disparityQueue.get()
61            showDepth(disparity.getCvFrame(), minDistance=100, maxDistance=6000, useLog=False)
62
63            key = cv.waitKey(1)
64            if key == ord('q'):
65                quit()

Need assistance?

Head over to Discussion Forum for technical support or any other questions you might have.