DepthAI
Software Stack

ON THIS PAGE

  • Neural Depth
  • Demo
  • Pipeline
  • Source code

Neural Depth

Supported on:RVC4
This example demonstrates the NeuralDepth node with runtime configuration of confidence threshold, edge threshold, and temporal filtering.

Demo

Pipeline

Source code

Python
C++

Python

Python
GitHub
1#!/usr/bin/env python3
2
3import cv2
4import depthai as dai
5import numpy as np
6
7FPS = 10
8# Create pipeline
9with dai.Pipeline() as pipeline:
10    cameraLeft = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_B, sensorFps=FPS)
11    cameraRight = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_C, sensorFps=FPS)
12    leftOutput = cameraLeft.requestFullResolutionOutput()
13    rightOutput = cameraRight.requestFullResolutionOutput()
14
15    neuralDepth = pipeline.create(dai.node.NeuralDepth).build(leftOutput, rightOutput, dai.DeviceModelZoo.NEURAL_DEPTH_LARGE)
16
17    confidenceQueue = neuralDepth.confidence.createOutputQueue()
18    edgeQueue = neuralDepth.edge.createOutputQueue()
19    disparityQueue = neuralDepth.disparity.createOutputQueue()
20
21    inputConfigQueue = neuralDepth.inputConfig.createInputQueue()
22    # Connect to device and start pipeline
23    pipeline.start()
24    maxDisparity = 1
25    colorMap = cv2.applyColorMap(np.arange(256, dtype=np.uint8), cv2.COLORMAP_JET)
26    colorMap[0] = [0, 0, 0]  # to make zero-disparity pixels black
27    currentConfig = neuralDepth.initialConfig
28    print("For adjusting thresholds, use keys:")
29    print(" - 'w': Increase confidence threshold")
30    print(" - 's': Decrease confidence threshold")
31    print(" - 'd': Increase edge threshold")
32    print(" - 'a': Decrease edge threshold")
33    print(" - 't': Toggle temporal filtering")
34    while pipeline.isRunning():
35        confidenceData = confidenceQueue.get()
36        assert isinstance(confidenceData, dai.ImgFrame)
37        npConfidence = confidenceData.getFrame()
38        colorizedConfidence = cv2.applyColorMap(((npConfidence)).astype(np.uint8), colorMap)
39        cv2.imshow("confidence", colorizedConfidence)
40
41        edgeData = edgeQueue.get()
42        assert isinstance(edgeData, dai.ImgFrame)
43        npEdge = edgeData.getFrame()
44        colorizedEdge = cv2.applyColorMap(((npEdge)).astype(np.uint8), colorMap)
45        cv2.imshow("edge", colorizedEdge)
46
47
48        disparityData = disparityQueue.get()
49        assert isinstance(disparityData, dai.ImgFrame)
50        npDisparity = disparityData.getFrame()
51        maxDisparity = max(maxDisparity, np.max(npDisparity))
52        colorizedDisparity = cv2.applyColorMap(((npDisparity / maxDisparity) * 255).astype(np.uint8), colorMap)
53        cv2.imshow("disparity", colorizedDisparity)
54
55        key = cv2.waitKey(1)
56        if key == ord('q'):
57            pipeline.stop()
58            break
59        if key == ord('w'):
60            currentThreshold = currentConfig.getConfidenceThreshold()
61            currentConfig.setConfidenceThreshold((currentThreshold + 5) % 255)
62            print("Setting confidence threshold to:", currentConfig.getConfidenceThreshold())
63            inputConfigQueue.send(currentConfig)
64        if key == ord('s'):
65            currentThreshold = currentConfig.getConfidenceThreshold()
66            currentConfig.setConfidenceThreshold((currentThreshold - 5) % 255)
67            print("Setting confidence threshold to:", currentConfig.getConfidenceThreshold())
68            inputConfigQueue.send(currentConfig)
69        if key == ord('d'):
70            currentThreshold = currentConfig.getEdgeThreshold()
71            currentConfig.setEdgeThreshold((currentThreshold + 1) % 255)
72            print("Setting edge threshold to:", currentConfig.getEdgeThreshold())
73            inputConfigQueue.send(currentConfig)
74        if key == ord('a'):
75            currentThreshold = currentConfig.getEdgeThreshold()
76            currentConfig.setEdgeThreshold((currentThreshold - 1) % 255)
77            print("Setting edge threshold to:", currentConfig.getEdgeThreshold())
78            inputConfigQueue.send(currentConfig)
79        if key == ord('t'):
80            currentConfig.postProcessing.temporalFilter.enable = not currentConfig.postProcessing.temporalFilter.enable
81            print("Temporal filtering:", "on" if currentConfig.postProcessing.temporalFilter.enable else "off")
82            inputConfigQueue.send(currentConfig)
83
84        if cv2.waitKey(1) == ord('q'):
85            break

Need assistance?

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