Skip to content

Instantly share code, notes, and snippets.

@ibaiGorordo
Last active August 5, 2022 04:04
Show Gist options
  • Save ibaiGorordo/375922c48bc3d05e2668e15077ccad98 to your computer and use it in GitHub Desktop.
Save ibaiGorordo/375922c48bc3d05e2668e15077ccad98 to your computer and use it in GitHub Desktop.
Perform stereo depth estimation using the depthai API on images from the host computer in Python.
import depthai as dai
import cv2
import numpy as np
from imread_from_url import imread_from_url
def add_host_depth(pipeline):
monoLeftNode = pipeline.create(dai.node.XLinkIn)
monoRightNode = pipeline.create(dai.node.XLinkIn)
depthNode = create_depth_node(pipeline)
monoLeftNode.out.link(depthNode.left)
monoRightNode.out.link(depthNode.right)
monoLeftNode.setStreamName('inLeft')
monoRightNode.setStreamName('inRight')
xinStereoDepthConfig = pipeline.create(dai.node.XLinkIn)
xinStereoDepthConfig.out.link(depthNode.inputConfig)
xinStereoDepthConfig.setStreamName("stereoDepthConfig")
xoutStereoCfg = pipeline.create(dai.node.XLinkOut)
depthNode.outConfig.link(xoutStereoCfg.input)
xoutStereoCfg.setStreamName('stereoConfig')
def create_depth_node(pipeline):
depthNode = pipeline.create(dai.node.StereoDepth)
depthNode.setDefaultProfilePreset(dai.node.StereoDepth.PresetMode.HIGH_DENSITY)
depthNode.setRectifyEdgeFillColor(0) # Black, to better see the cutout
depthNode.initialConfig.setMedianFilter(dai.MedianFilter.KERNEL_7x7)
depthNode.setLeftRightCheck(True)
depthNode.setExtendedDisparity(True)
depthNode.setRectification(False)
depthNode.setInputResolution(width, height)
depthOutNode = pipeline.create(dai.node.XLinkOut)
depthNode.disparity.link(depthOutNode.input)
depthOutNode.setStreamName("disparity")
return depthNode
def img_to_daiFrame(img, side=dai.CameraBoardSocket.RIGHT):
if img.shape[2] > 1:
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
data = cv2.resize(img, (width, height), interpolation = cv2.INTER_AREA)
data = data.reshape(height*width)
imgFrame = dai.ImgFrame()
imgFrame.setData(data)
imgFrame.setInstanceNum(side)
imgFrame.setType(dai.ImgFrame.Type.RAW8)
imgFrame.setWidth(width)
imgFrame.setHeight(height)
return imgFrame
if __name__ == '__main__':
height, width = 800, 1280
# Create pipeline
pipeline = dai.Pipeline()
add_host_depth(pipeline)
with dai.Device(pipeline) as device:
stereoDepthConfigInQueue = device.getInputQueue("stereoDepthConfig")
currentConfig = device.getOutputQueue("stereoConfig", 8, blocking=False)
qLeft = device.getInputQueue("inLeft")
qRight = device.getInputQueue("inRight")
qDepth = device.getOutputQueue(name="disparity", maxSize=4, blocking=False)
left_img = imread_from_url("https://vision.middlebury.edu/stereo/data/scenes2003/newdata/cones/im2.png")
right_img = imread_from_url("https://vision.middlebury.edu/stereo/data/scenes2003/newdata/cones/im6.png")
imgFrameLeft = img_to_daiFrame(left_img, dai.CameraBoardSocket.LEFT)
imgFrameRight = img_to_daiFrame(right_img, dai.CameraBoardSocket.RIGHT)
qLeft.send(imgFrameLeft)
qRight.send(imgFrameRight)
config = currentConfig.get()
maxDisp = config.getMaxDisparity()
print(f"Max disparity: {maxDisp}")
disp_map = qDepth.get().getCvFrame()
color_disp = cv2.applyColorMap((disp_map / maxDisp * 255).astype(np.uint8), cv2.COLORMAP_JET)
cv2.imshow("disparity", color_disp)
cv2.waitKey(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment