Last active
May 27, 2021 04:38
-
-
Save alex-luxonis/d1c5c76f74fd1fab793a82ad917f9ad0 to your computer and use it in GitHub Desktop.
Gen2 StereoDepth example - absolute depth output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import cv2 | |
import depthai as dai | |
import numpy as np | |
pipeline = dai.Pipeline() | |
left = pipeline.createMonoCamera() | |
left.setBoardSocket(dai.CameraBoardSocket.LEFT) | |
right = pipeline.createMonoCamera() | |
right.setBoardSocket(dai.CameraBoardSocket.RIGHT) | |
for cam in [left, right]: | |
#cam.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P) | |
cam.setResolution(dai.MonoCameraProperties.SensorResolution.THE_720_P) | |
stereo = pipeline.createStereoDepth() | |
stereo.setConfidenceThreshold(200) | |
stereo.setLeftRightCheck(0) | |
stereo.setExtendedDisparity(0) | |
stereo.setSubpixel(0) | |
left.out.link(stereo.left) | |
right.out.link(stereo.right) | |
xout = pipeline.createXLinkOut() | |
xout.setStreamName("depth") | |
stereo.depth.link(xout.input) | |
with dai.Device(pipeline) as device: | |
device.startPipeline() | |
q = device.getOutputQueue(name="depth", maxSize=4, blocking=False) | |
while True: | |
pkt = q.get() | |
frame = pkt.getFrame() | |
# At this point `frame` contains u16 depth data in millimeters. | |
# Invalid values are set to 0 | |
cv2.imshow("depth_u16_mm", frame) | |
# To easily view the depth pixels values within the OpenCV imshow view, | |
# divide by 10 and cap to 255, effectively converting to centimeters, | |
# with a limit of 2.55 meters. | |
frame_cm = (frame // 10).clip(0, 255).astype(np.uint8) | |
cv2.imshow("depth_cm_up_to_2.55m", frame_cm) | |
if cv2.waitKey(1) == ord('q'): | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment