spatial-location-calculator.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import cv2
  2. import depthai as dai
  3. stepSize = 0.05
  4. newConfig = False
  5. # Create pipeline
  6. pipeline = dai.Pipeline()
  7. # Define sources and outputs
  8. monoLeft = pipeline.create(dai.node.MonoCamera)
  9. monoRight = pipeline.create(dai.node.MonoCamera)
  10. stereo = pipeline.create(dai.node.StereoDepth)
  11. spatialLocationCalculator = pipeline.create(dai.node.SpatialLocationCalculator)
  12. xoutDepth = pipeline.create(dai.node.XLinkOut)
  13. xoutSpatialData = pipeline.create(dai.node.XLinkOut)
  14. xinSpatialCalcConfig = pipeline.create(dai.node.XLinkIn)
  15. xoutDepth.setStreamName("depth")
  16. xoutSpatialData.setStreamName("spatialData")
  17. xinSpatialCalcConfig.setStreamName("spatialCalcConfig")
  18. # Properties
  19. monoLeft.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
  20. monoLeft.setBoardSocket(dai.CameraBoardSocket.LEFT)
  21. monoRight.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
  22. monoRight.setBoardSocket(dai.CameraBoardSocket.RIGHT)
  23. lrcheck = False
  24. subpixel = False
  25. stereo.initialConfig.setConfidenceThreshold(255)
  26. stereo.setLeftRightCheck(lrcheck)
  27. stereo.setSubpixel(subpixel)
  28. # Config
  29. topLeft = dai.Point2f(0.4, 0.4)
  30. bottomRight = dai.Point2f(0.6, 0.6)
  31. config = dai.SpatialLocationCalculatorConfigData()
  32. config.depthThresholds.lowerThreshold = 100
  33. config.depthThresholds.upperThreshold = 10000
  34. config.roi = dai.Rect(topLeft, bottomRight)
  35. spatialLocationCalculator.setWaitForConfigInput(False)
  36. spatialLocationCalculator.initialConfig.addROI(config)
  37. # Linking
  38. monoLeft.out.link(stereo.left)
  39. monoRight.out.link(stereo.right)
  40. spatialLocationCalculator.passthroughDepth.link(xoutDepth.input)
  41. stereo.depth.link(spatialLocationCalculator.inputDepth)
  42. spatialLocationCalculator.out.link(xoutSpatialData.input)
  43. xinSpatialCalcConfig.out.link(spatialLocationCalculator.inputConfig)
  44. # Connect to device and start pipeline
  45. with dai.Device(pipeline) as device:
  46. # Output queue will be used to get the depth frames from the outputs defined above
  47. depthQueue = device.getOutputQueue(name="depth", maxSize=4, blocking=False)
  48. spatialCalcQueue = device.getOutputQueue(name="spatialData", maxSize=4, blocking=False)
  49. spatialCalcConfigInQueue = device.getInputQueue("spatialCalcConfig")
  50. color = (255, 255, 255)
  51. print("Use WASD keys to move ROI!")
  52. while True:
  53. inDepth = depthQueue.get() # Blocking call, will wait until a new data has arrived
  54. depthFrame = inDepth.getFrame() # depthFrame values are in millimeters
  55. depthFrameColor = cv2.normalize(depthFrame, None, 255, 0, cv2.NORM_INF, cv2.CV_8UC1)
  56. depthFrameColor = cv2.equalizeHist(depthFrameColor)
  57. depthFrameColor = cv2.applyColorMap(depthFrameColor, cv2.COLORMAP_HOT)
  58. spatialData = spatialCalcQueue.get().getSpatialLocations()
  59. for depthData in spatialData:
  60. roi = depthData.config.roi
  61. roi = roi.denormalize(width=depthFrameColor.shape[1], height=depthFrameColor.shape[0])
  62. xmin = int(roi.topLeft().x)
  63. ymin = int(roi.topLeft().y)
  64. xmax = int(roi.bottomRight().x)
  65. ymax = int(roi.bottomRight().y)
  66. depthMin = depthData.depthMin
  67. depthMax = depthData.depthMax
  68. fontType = cv2.FONT_HERSHEY_TRIPLEX
  69. cv2.rectangle(depthFrameColor, (xmin, ymin), (xmax, ymax), color, cv2.FONT_HERSHEY_SCRIPT_SIMPLEX)
  70. cv2.putText(depthFrameColor, f"X: {int(depthData.spatialCoordinates.x)} mm", (xmin + 10, ymin + 20), fontType, 0.5, 255)
  71. cv2.putText(depthFrameColor, f"Y: {int(depthData.spatialCoordinates.y)} mm", (xmin + 10, ymin + 35), fontType, 0.5, 255)
  72. cv2.putText(depthFrameColor, f"Z: {int(depthData.spatialCoordinates.z)} mm", (xmin + 10, ymin + 50), fontType, 0.5, 255)
  73. # Show the frame
  74. cv2.imshow("depth", depthFrameColor)
  75. key = cv2.waitKey(1)
  76. if key == 27:
  77. break
  78. elif key == ord('w'):
  79. if topLeft.y - stepSize >= 0:
  80. topLeft.y -= stepSize
  81. bottomRight.y -= stepSize
  82. newConfig = True
  83. elif key == ord('a'):
  84. if topLeft.x - stepSize >= 0:
  85. topLeft.x -= stepSize
  86. bottomRight.x -= stepSize
  87. newConfig = True
  88. elif key == ord('s'):
  89. if bottomRight.y + stepSize <= 1:
  90. topLeft.y += stepSize
  91. bottomRight.y += stepSize
  92. newConfig = True
  93. elif key == ord('d'):
  94. if bottomRight.x + stepSize <= 1:
  95. topLeft.x += stepSize
  96. bottomRight.x += stepSize
  97. newConfig = True
  98. if newConfig:
  99. config.roi = dai.Rect(topLeft, bottomRight)
  100. cfg = dai.SpatialLocationCalculatorConfig()
  101. cfg.addROI(config)
  102. spatialCalcConfigInQueue.send(cfg)
  103. newConfig = False