Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #!/usr/bin/env python3
  2.  
  3. from pathlib import Path
  4. import sys
  5. import cv2
  6. import depthai as dai
  7.  
  8. '''
  9. Spatial detection network demo.
  10.    Performs inference on RGB camera and retrieves spatial location coordinates: x,y,z relative to the center of depth map.
  11. '''
  12.  
  13. # Get argument first
  14. nnBlobPath = str((Path(__file__).parent / Path('../models/mobilenet-ssd_openvino_2021.4_6shave.blob')).resolve().absolute())
  15. if len(sys.argv) > 1:
  16.     nnBlobPath = sys.argv[1]
  17.  
  18. if not Path(nnBlobPath).exists():
  19.     import sys
  20.     raise FileNotFoundError(f'Required file/s not found, please run "{sys.executable} install_requirements.py"')
  21.  
  22. # MobilenetSSD label texts
  23. labelMap = ["background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow",
  24.             "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]
  25.  
  26. syncNN = True
  27.  
  28. # Create pipeline
  29. pipeline = dai.Pipeline()
  30.  
  31. # Define sources and outputs
  32. camRgb = pipeline.createColorCamera()
  33. spatialDetectionNetwork = pipeline.create(dai.node.MobileNetSpatialDetectionNetwork)
  34. monoLeft = pipeline.create(dai.node.MonoCamera)
  35. monoRight = pipeline.create(dai.node.MonoCamera)
  36. stereo = pipeline.create(dai.node.StereoDepth)
  37.  
  38. xoutRgb = pipeline.create(dai.node.XLinkOut)
  39. xoutNN = pipeline.create(dai.node.XLinkOut)
  40. xoutBoundingBoxDepthMapping = pipeline.create(dai.node.XLinkOut)
  41. xoutDepth = pipeline.create(dai.node.XLinkOut)
  42.  
  43. xoutRgb.setStreamName("rgb")
  44. xoutNN.setStreamName("detections")
  45. xoutBoundingBoxDepthMapping.setStreamName("boundingBoxDepthMapping")
  46. xoutDepth.setStreamName("depth")
  47.  
  48. # Properties
  49. camRgb.setPreviewSize(300, 300)
  50. camRgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_1080_P)
  51. camRgb.setInterleaved(False)
  52. camRgb.setColorOrder(dai.ColorCameraProperties.ColorOrder.BGR)
  53. camRgb.setImageOrientation(dai.CameraImageOrientation.ROTATE_180_DEG)
  54.  
  55. monoLeft.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
  56. monoLeft.setBoardSocket(dai.CameraBoardSocket.LEFT)
  57. monoRight.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
  58. monoRight.setBoardSocket(dai.CameraBoardSocket.RIGHT)
  59.  
  60. # Setting node configs
  61. stereo.setDefaultProfilePreset(dai.node.StereoDepth.PresetMode.HIGH_DENSITY)
  62. # Align depth map to the perspective of RGB camera, on which inference is done
  63. stereo.setDepthAlign(dai.CameraBoardSocket.RGB)
  64. stereo.setOutputSize(monoLeft.getResolutionWidth(), monoLeft.getResolutionHeight())
  65.  
  66. rotate_stereo_manip = pipeline.createImageManip()
  67. rotate_stereo_manip.initialConfig.setVerticalFlip(True)
  68. rotate_stereo_manip.initialConfig.setHorizontalFlip(True)
  69. rotate_stereo_manip.setFrameType(dai.ImgFrame.Type.RAW16)
  70. stereo.depth.link(rotate_stereo_manip.inputImage)
  71.  
  72. spatialDetectionNetwork.setBlobPath(nnBlobPath)
  73. spatialDetectionNetwork.setConfidenceThreshold(0.5)
  74. spatialDetectionNetwork.input.setBlocking(False)
  75. spatialDetectionNetwork.setBoundingBoxScaleFactor(0.5)
  76. spatialDetectionNetwork.setDepthLowerThreshold(100)
  77. spatialDetectionNetwork.setDepthUpperThreshold(5000)
  78.  
  79. # Linking
  80. monoLeft.out.link(stereo.left)
  81. monoRight.out.link(stereo.right)
  82.  
  83. camRgb.preview.link(spatialDetectionNetwork.input)
  84. if syncNN:
  85.     spatialDetectionNetwork.passthrough.link(xoutRgb.input)
  86. else:
  87.     camRgb.preview.link(xoutRgb.input)
  88.  
  89. spatialDetectionNetwork.out.link(xoutNN.input)
  90. spatialDetectionNetwork.boundingBoxMapping.link(xoutBoundingBoxDepthMapping.input)
  91.  
  92. rotate_stereo_manip.out.link(spatialDetectionNetwork.inputDepth)
  93. spatialDetectionNetwork.passthroughDepth.link(xoutDepth.input)
  94.  
  95. # Connect to device and start pipeline
  96. with dai.Device(pipeline) as device:
  97.  
  98.     # Output queues will be used to get the rgb frames and nn data from the outputs defined above
  99.     previewQueue = device.getOutputQueue(name="rgb", maxSize=4, blocking=False)
  100.     detectionNNQueue = device.getOutputQueue(name="detections", maxSize=4, blocking=False)
  101.     xoutBoundingBoxDepthMapping = device.getOutputQueue(name="boundingBoxDepthMapping", maxSize=4, blocking=False)
  102.     depthQueue = device.getOutputQueue(name="depth", maxSize=4, blocking=False)
  103.  
  104.     while True:
  105.         inPreview = previewQueue.get()
  106.         inDet = detectionNNQueue.get()
  107.         depth = depthQueue.get()
  108.  
  109.         frame = inPreview.getCvFrame()
  110.         depthFrame = depth.getFrame() # depthFrame values are in millimeters
  111.  
  112.         depthFrameColor = cv2.normalize(depthFrame, None, 255, 0, cv2.NORM_INF, cv2.CV_8UC1)
  113.         depthFrameColor = cv2.equalizeHist(depthFrameColor)
  114.         depthFrameColor = cv2.applyColorMap(depthFrameColor, cv2.COLORMAP_HOT)
  115.  
  116.         detections = inDet.detections
  117.         if len(detections) != 0:
  118.             boundingBoxMapping = xoutBoundingBoxDepthMapping.get()
  119.             roiDatas = boundingBoxMapping.getConfigData()
  120.  
  121.             for roiData in roiDatas:
  122.                 roi = roiData.roi
  123.                 roi = roi.denormalize(depthFrameColor.shape[1], depthFrameColor.shape[0])
  124.                 topLeft = roi.topLeft()
  125.                 bottomRight = roi.bottomRight()
  126.                 xmin = int(topLeft.x)
  127.                 ymin = int(topLeft.y)
  128.                 xmax = int(bottomRight.x)
  129.                 ymax = int(bottomRight.y)
  130.  
  131.                 cv2.rectangle(depthFrameColor, (xmin, ymin), (xmax, ymax), 255, cv2.FONT_HERSHEY_SCRIPT_SIMPLEX)
  132.  
  133.         # If the frame is available, draw bounding boxes on it and show the frame
  134.         height = frame.shape[0]
  135.         width  = frame.shape[1]
  136.         for detection in detections:
  137.             # Denormalize bounding box
  138.             x1 = int(detection.xmin * width)
  139.             x2 = int(detection.xmax * width)
  140.             y1 = int(detection.ymin * height)
  141.             y2 = int(detection.ymax * height)
  142.             try:
  143.                 label = labelMap[detection.label]
  144.             except:
  145.                 label = detection.label
  146.             cv2.putText(frame, str(label), (x1 + 10, y1 + 20), cv2.FONT_HERSHEY_TRIPLEX, 0.5, 255)
  147.             cv2.putText(frame, "{:.2f}".format(detection.confidence*100), (x1 + 10, y1 + 35), cv2.FONT_HERSHEY_TRIPLEX, 0.5, 255)
  148.             cv2.putText(frame, f"X: {int(detection.spatialCoordinates.x)} mm", (x1 + 10, y1 + 50), cv2.FONT_HERSHEY_TRIPLEX, 0.5, 255)
  149.             cv2.putText(frame, f"Y: {int(detection.spatialCoordinates.y)} mm", (x1 + 10, y1 + 65), cv2.FONT_HERSHEY_TRIPLEX, 0.5, 255)
  150.             cv2.putText(frame, f"Z: {int(detection.spatialCoordinates.z)} mm", (x1 + 10, y1 + 80), cv2.FONT_HERSHEY_TRIPLEX, 0.5, 255)
  151.  
  152.             cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 0), cv2.FONT_HERSHEY_SIMPLEX)
  153.  
  154.         cv2.imshow("depth", depthFrameColor)
  155.         cv2.imshow("preview", frame)
  156.  
  157.         if cv2.waitKey(1) == ord('q'):
  158.             break
  159.