|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import cv2 |
| 4 | +import depthai as dai |
| 5 | +import numpy as np |
| 6 | + |
| 7 | +# Create pipeline |
| 8 | +pipeline = dai.Pipeline() |
| 9 | + |
| 10 | +# Define sources and outputs |
| 11 | +monoLeft = pipeline.create(dai.node.MonoCamera) |
| 12 | +monoRight = pipeline.create(dai.node.MonoCamera) |
| 13 | +stereo = pipeline.create(dai.node.StereoDepth) |
| 14 | +xout = pipeline.create(dai.node.XLinkOut) |
| 15 | +xoutLeft = pipeline.create(dai.node.XLinkOut) |
| 16 | + |
| 17 | +xout.setStreamName("disparity") |
| 18 | +xoutLeft.setStreamName("left") |
| 19 | + |
| 20 | +# Properties |
| 21 | +monoLeft.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P) |
| 22 | +monoLeft.setCamera("left") |
| 23 | +monoRight.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P) |
| 24 | +monoRight.setCamera("right") |
| 25 | + |
| 26 | +stereo.enableDistortionCorrection(True) |
| 27 | + |
| 28 | +# Linking |
| 29 | +monoLeft.out.link(stereo.left) |
| 30 | +monoRight.out.link(stereo.right) |
| 31 | +stereo.disparity.link(xout.input) |
| 32 | +stereo.rectifiedLeft.link(xoutLeft.input) |
| 33 | + |
| 34 | +cvColorMap = cv2.applyColorMap(np.arange(256, dtype=np.uint8), cv2.COLORMAP_JET) |
| 35 | +cvColorMap[0] = [0, 0, 0] |
| 36 | + |
| 37 | +# Connect to device and start pipeline |
| 38 | +with dai.Device(pipeline) as device: |
| 39 | + |
| 40 | + try: |
| 41 | + calibration = device.readCalibration() |
| 42 | + except: |
| 43 | + print("Device is not calibrated!") |
| 44 | + exit() |
| 45 | + |
| 46 | + # Output queue will be used to get the disparity frames from the outputs defined above |
| 47 | + q = device.getOutputQueue(name="disparity", maxSize=4, blocking=False) |
| 48 | + qLeft = device.getOutputQueue(name="left", maxSize=4, blocking=False) |
| 49 | + |
| 50 | + while True: |
| 51 | + inDisparity = q.get() # blocking call, will wait until a new data has arrived |
| 52 | + frame = inDisparity.getFrame() |
| 53 | + # Normalization for better visualization |
| 54 | + frame = (frame * (255 / stereo.initialConfig.getMaxDisparity())).astype(np.uint8) |
| 55 | + |
| 56 | + cv2.imshow("disparity", frame) |
| 57 | + |
| 58 | + frame = cv2.applyColorMap(frame, cvColorMap) |
| 59 | + cv2.imshow("disparity_color", frame) |
| 60 | + |
| 61 | + inLeft = qLeft.get() |
| 62 | + frame = inLeft.getCvFrame() |
| 63 | + cv2.imshow("rectified left", frame) |
| 64 | + |
| 65 | + key = cv2.waitKey(1) |
| 66 | + if key == ord('q'): |
| 67 | + break |
| 68 | + elif key == ord('u'): |
| 69 | + randomDistortionCoeffs = np.random.rand(14) |
| 70 | + calibration.setDistortionCoefficients(dai.CameraBoardSocket.LEFT, randomDistortionCoeffs) |
| 71 | + try: |
| 72 | + device.setCalibration(calibration) |
| 73 | + except: |
| 74 | + print("Failed to update calibration!") |
| 75 | + try: |
| 76 | + updatedCalib = device.getCalibration() |
| 77 | + distortionCoeffs = updatedCalib.getDistortionCoefficients(dai.CameraBoardSocket.LEFT) |
| 78 | + print("Updated distortion coefficients: ", distortionCoeffs) |
| 79 | + except: |
| 80 | + pass |
| 81 | + |
0 commit comments