depth.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import cv2
  2. import depthai as dai
  3. import numpy as np
  4. def getFrame(queue):
  5. # Get frame from queue
  6. frame = queue.get()
  7. # Convert frame to OpenCV format and return
  8. return frame.getCvFrame()
  9. def getMonoCamera(pipeline, isLeft):
  10. # Configure mono camera
  11. mono = pipeline.createMonoCamera()
  12. # Set Camera Resolution
  13. mono.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
  14. if isLeft:
  15. # Get left camera
  16. mono.setBoardSocket(dai.CameraBoardSocket.LEFT)
  17. else :
  18. # Get right camera
  19. mono.setBoardSocket(dai.CameraBoardSocket.RIGHT)
  20. return mono
  21. def getStereoPair(pipeline, monoLeft, monoRight):
  22. # Configure stereo pair for depth estimation
  23. stereo = pipeline.createStereoDepth()
  24. # Checks occluded pixels and marks them as invalid
  25. stereo.setLeftRightCheck(True)
  26. # Configure left and right cameras to work as a stereo pair
  27. monoLeft.out.link(stereo.left)
  28. monoRight.out.link(stereo.right)
  29. return stereo
  30. def mouseCallback(event,x,y,flags,param):
  31. global mouseX, mouseY
  32. if event == cv2.EVENT_LBUTTONDOWN:
  33. mouseX = x
  34. mouseY = y
  35. if __name__ == '__main__':
  36. mouseX = 0
  37. mouseY = 640
  38. # Start defining a pipeline
  39. pipeline = dai.Pipeline()
  40. # Set up left and right cameras
  41. monoLeft = getMonoCamera(pipeline, isLeft = True)
  42. monoRight = getMonoCamera(pipeline, isLeft = False)
  43. # Combine left and right cameras to form a stereo pair
  44. stereo = getStereoPair(pipeline, monoLeft, monoRight)
  45. # Set XlinkOut for disparity, rectifiedLeft, and rectifiedRight
  46. xoutDisp = pipeline.createXLinkOut()
  47. xoutDisp.setStreamName("disparity")
  48. xoutRectifiedLeft = pipeline.createXLinkOut()
  49. xoutRectifiedLeft.setStreamName("rectifiedLeft")
  50. xoutRectifiedRight = pipeline.createXLinkOut()
  51. xoutRectifiedRight.setStreamName("rectifiedRight")
  52. stereo.disparity.link(xoutDisp.input)
  53. stereo.rectifiedLeft.link(xoutRectifiedLeft.input)
  54. stereo.rectifiedRight.link(xoutRectifiedRight.input)
  55. # Pipeline is defined, now we can connect to the device
  56. with dai.Device(pipeline) as device:
  57. # Output queues will be used to get the rgb frames and nn data from the outputs defined above
  58. disparityQueue = device.getOutputQueue(name="disparity", maxSize=1, blocking=False)
  59. rectifiedLeftQueue = device.getOutputQueue(name="rectifiedLeft", maxSize=1, blocking=False)
  60. rectifiedRightQueue = device.getOutputQueue(name="rectifiedRight", maxSize=1, blocking=False)
  61. # Calculate a multiplier for colormapping disparity map
  62. disparityMultiplier = 255 / stereo.getMaxDisparity()
  63. cv2.namedWindow("Stereo Pair")
  64. cv2.setMouseCallback("Stereo Pair", mouseCallback)
  65. # Variable use to toggle between side by side view and one frame view.
  66. sideBySide = False
  67. while True:
  68. # Get disparity map
  69. disparity = getFrame(disparityQueue)
  70. # Colormap disparity for display
  71. disparity = (disparity * disparityMultiplier).astype(np.uint8)
  72. disparity = cv2.applyColorMap(disparity, cv2.COLORMAP_JET)
  73. # Get left and right rectified frame
  74. leftFrame = getFrame(rectifiedLeftQueue);
  75. rightFrame = getFrame(rectifiedRightQueue)
  76. if sideBySide:
  77. # Show side by side view
  78. imOut = np.hstack((leftFrame, rightFrame))
  79. else :
  80. # Show overlapping frames
  81. imOut = np.uint8(leftFrame/2 + rightFrame/2)
  82. imOut = cv2.cvtColor(imOut,cv2.COLOR_GRAY2RGB)
  83. imOut = cv2.line(imOut, (mouseX, mouseY), (1280, mouseY), (0, 0, 255), 2)
  84. imOut = cv2.circle(imOut, (mouseX, mouseY), 2, (255, 255, 128), 2)
  85. cv2.imshow("Stereo Pair", imOut)
  86. cv2.imshow("Disparity", disparity)
  87. # Check for keyboard input
  88. key = cv2.waitKey(1)
  89. if key == ord('q'):
  90. # Quit when q is pressed
  91. break
  92. elif key == ord('t'):
  93. # Toggle display when t is pressed
  94. sideBySide = not sideBySide