Skip to content

Instantly share code, notes, and snippets.

@ShingoHattori
Created January 10, 2021 05:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShingoHattori/f92f6da0c20d9183284c4c00d5e522d5 to your computer and use it in GitHub Desktop.
Save ShingoHattori/f92f6da0c20d9183284c4c00d5e522d5 to your computer and use it in GitHub Desktop.
Try to run both Depth and RGB on OAK-D(Gen2) at the same time.
#!/usr/bin/env python3
import cv2
import depthai as dai
import numpy as np
# Start defining a pipeline
pipeline = dai.Pipeline()
# Define a source
cam_rgb = pipeline.createColorCamera()
cam_rgb.setPreviewSize(300, 300)
cam_rgb.setCamId(0)
cam_rgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_1080_P)
cam_rgb.setInterleaved(False)
cam_left = pipeline.createMonoCamera()
cam_left.setResolution(dai.MonoCameraProperties.SensorResolution.THE_720_P)
cam_left.setCamId(1)
cam_right = pipeline.createMonoCamera()
cam_right.setResolution(dai.MonoCameraProperties.SensorResolution.THE_720_P)
cam_right.setCamId(2)
# Create a node that will produce the depth map
depth = pipeline.createStereoDepth()
depth.setConfidenceThreshold(200)
cam_left.out.link(depth.left)
cam_right.out.link(depth.right)
# Create output
depthout = pipeline.createXLinkOut()
depthout.setStreamName("disparity")
depth.disparity.link(depthout.input)
rgbout = pipeline.createXLinkOut()
rgbout.setStreamName('rgb')
cam_rgb.preview.link(rgbout.input)
# Pipeline defined, now the device is assigned and pipeline is started
device = dai.Device(pipeline)
device.startPipeline()
# Output queue will be used to get the disparity frames and rgb frames from the outputs defined above
q_depth = device.getOutputQueue(name="disparity", maxSize=4, overwrite=True)
q_rgb = device.getOutputQueue(name="rgb", maxSize=4, overwrite=True)
f_color = None
f_depth = None
while True:
in_depth = q_depth.tryGet()
in_rgb = q_rgb.tryGet()
if in_rgb is not None:
shape = (3, in_rgb.getHeight(), in_rgb.getWidth())
f_color = in_rgb.getData().reshape(shape).transpose(1, 2, 0).astype(np.uint8)
f_color = np.ascontiguousarray(f_color)
if f_color is not None:
cv2.imshow("color", f_color)
if in_depth is not None:
f_depth = in_depth.getData().reshape((in_depth.getHeight(), in_depth.getWidth())).astype(np.uint8)
f_depth = np.ascontiguousarray(f_depth)
f_depth = cv2.applyColorMap(f_depth, cv2.COLORMAP_JET)
if f_depth is not None:
cv2.imshow("disparity", f_depth)
if cv2.waitKey(1) == ord('q'):
break
@ShingoHattori
Copy link
Author

Add code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment