stereo-view.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. if __name__ == '__main__':
  22. # Define a pipeline
  23. pipeline = dai.Pipeline()
  24. # Set up left and right cameras
  25. monoLeft = getMonoCamera(pipeline, isLeft = True)
  26. monoRight = getMonoCamera(pipeline, isLeft = False)
  27. # Set output Xlink for left camera
  28. xoutLeft = pipeline.createXLinkOut()
  29. xoutLeft.setStreamName("left")
  30. # Set output Xlink for right camera
  31. xoutRight = pipeline.createXLinkOut()
  32. xoutRight.setStreamName("right")
  33. # Attach cameras to output Xlink
  34. monoLeft.out.link(xoutLeft.input)
  35. monoRight.out.link(xoutRight.input)
  36. # Pipeline is defined, now we can connect to the device
  37. with dai.Device(pipeline) as device:
  38. # Get output queues.
  39. leftQueue = device.getOutputQueue(name="left", maxSize=1)
  40. rightQueue = device.getOutputQueue(name="right", maxSize=1)
  41. # Set display window name
  42. cv2.namedWindow("Stereo Pair")
  43. # Variable use to toggle between side by side view and one frame view.
  44. sideBySide = True
  45. while True:
  46. # Get left frame
  47. leftFrame = getFrame(leftQueue)
  48. # Get right frame
  49. rightFrame = getFrame(rightQueue)
  50. if sideBySide:
  51. # Show side by side view
  52. imOut = np.hstack((leftFrame, rightFrame))
  53. else :
  54. # Show overlapping frames
  55. imOut = np.uint8(leftFrame/2 + rightFrame/2)
  56. # Display output image
  57. cv2.imshow("Stereo Pair", imOut)
  58. # Check for keyboard input
  59. key = cv2.waitKey(1)
  60. if key == ord('q'):
  61. # Quit when q is pressed
  62. break
  63. elif key == ord('t'):
  64. # Toggle display when t is pressed
  65. sideBySide = not sideBySide