spacial_face_det.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. # Face Detection
  2. # Import required modules
  3. import cv2
  4. import depthai as dai
  5. import time
  6. import blobconverter
  7. # Define Frame
  8. FRAME_SIZE = (640, 400)
  9. # Define NN model name and input size
  10. # If you define the blob make make sure the MODEL_NAME and ZOO_TYPE are None
  11. # DET_INPUT_SIZE = (672, 384)
  12. # model_name = None
  13. # zoo_type = None
  14. # blob_path = "models/face-detection-adas-0001.blob"
  15. DET_INPUT_SIZE = (300, 300)
  16. model_name = "face-detection-retail-0004"
  17. zoo_type = "depthai"
  18. blob_path = None
  19. # DET_INPUT_SIZE = (672, 384)
  20. # model_name = "face-detection-adas-0001"
  21. # zoo_type = "intel"
  22. # blob_path = None
  23. # Start defining a pipeline
  24. pipeline = dai.Pipeline()
  25. # Define a source - RGB camera
  26. cam = pipeline.createColorCamera()
  27. cam.setPreviewSize(FRAME_SIZE[0], FRAME_SIZE[1])
  28. cam.setInterleaved(False)
  29. cam.setResolution(dai.ColorCameraProperties.SensorResolution.THE_1080_P)
  30. # Define mono camera sources for stereo depth
  31. mono_left = pipeline.createMonoCamera()
  32. mono_left.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
  33. mono_left.setBoardSocket(dai.CameraBoardSocket.LEFT)
  34. mono_right = pipeline.createMonoCamera()
  35. mono_right.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
  36. mono_right.setBoardSocket(dai.CameraBoardSocket.RIGHT)
  37. # Create stereo depth node
  38. stereo = pipeline.createStereoDepth()
  39. stereo.setLeftRightCheck(True)
  40. # Linking
  41. mono_left.out.link(stereo.left)
  42. mono_right.out.link(stereo.right)
  43. cam.setBoardSocket(dai.CameraBoardSocket.RGB)
  44. # Convert model from OMZ to blob
  45. if model_name is not None:
  46. blob_path = blobconverter.from_zoo(
  47. name=model_name,
  48. shaves=6,
  49. zoo_type=zoo_type
  50. )
  51. # Define face detection NN node
  52. face_spac_det_nn = pipeline.createMobileNetSpatialDetectionNetwork()
  53. face_spac_det_nn.setConfidenceThreshold(0.75)
  54. face_spac_det_nn.setBlobPath(blob_path)
  55. face_spac_det_nn.setDepthLowerThreshold(100)
  56. face_spac_det_nn.setDepthUpperThreshold(5000)
  57. # Define face detection input config
  58. face_det_manip = pipeline.createImageManip()
  59. face_det_manip.initialConfig.setResize(DET_INPUT_SIZE[0], DET_INPUT_SIZE[1])
  60. face_det_manip.initialConfig.setKeepAspectRatio(False)
  61. # Linking
  62. cam.preview.link(face_det_manip.inputImage)
  63. face_det_manip.out.link(face_spac_det_nn.input)
  64. stereo.depth.link(face_spac_det_nn.inputDepth)
  65. # Create preview output
  66. x_preview_out = pipeline.createXLinkOut()
  67. x_preview_out.setStreamName("preview")
  68. cam.preview.link(x_preview_out.input)
  69. # Create detection output
  70. det_out = pipeline.createXLinkOut()
  71. det_out.setStreamName('det_out')
  72. face_spac_det_nn.out.link(det_out.input)
  73. # Create preview output
  74. disparity_out = pipeline.createXLinkOut()
  75. disparity_out.setStreamName("disparity")
  76. stereo.disparity.link(disparity_out.input)
  77. def display_info(frame, disp_frame, bbox, coordinates, status, status_color, fps):
  78. # Display bounding box
  79. cv2.rectangle(frame, bbox, status_color[status], 2)
  80. # Create background for showing details
  81. cv2.rectangle(frame, (5, 5, 175, 100), (50, 0, 0), -1)
  82. # Display authentication status on the frame
  83. cv2.putText(frame, status, (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.5, status_color[status])
  84. # Display instructions on the frame
  85. cv2.putText(frame, f'FPS: {fps:.2f}', (20, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255))
  86. # Display bbox and depth value on the disparity frame
  87. if coordinates is not None:
  88. cv2.rectangle(disp_frame, bbox, status_color[status], 2)
  89. cv2.rectangle(disp_frame, (5, 5, 185, 50), (50, 0, 0), -1)
  90. _, _, coord_z = coordinates
  91. cv2.putText(disp_frame, f'Depth: {coord_z}mm', (15, 35), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255))
  92. # Frame count
  93. frame_count = 0
  94. # Placeholder fps value
  95. fps = 0
  96. # Used to record the time when we processed last frames
  97. prev_frame_time = 0
  98. # Used to record the time at which we processed current frames
  99. new_frame_time = 0
  100. # Set status colors
  101. status_color = {
  102. 'Face Detected': (0, 255, 0),
  103. 'No Face Detected': (0, 0, 255)
  104. }
  105. # Start pipeline
  106. with dai.Device(pipeline) as device:
  107. # Output queue will be used to get the right camera frames from the outputs defined above
  108. q_cam = device.getOutputQueue(name="preview", maxSize=1, blocking=False)
  109. # Output queue will be used to get nn data from the video frames.
  110. q_det = device.getOutputQueue(name="det_out", maxSize=1, blocking=False)
  111. # Output queue will be used to get disparity map from stereo node.
  112. q_disp = device.getOutputQueue(name="disparity", maxSize=1, blocking=False)
  113. # # Output queue will be used to get nn data from the video frames.
  114. # q_bbox_depth_mapping = device.getOutputQueue(name="bbox_depth_mapping_out", maxSize=4, blocking=False)
  115. while True:
  116. # Get right camera frame
  117. in_cam = q_cam.get()
  118. frame = in_cam.getCvFrame()
  119. # Get disparity frame
  120. in_disp = q_disp.get()
  121. disp_frame = in_disp.getCvFrame()
  122. # Calculate a multiplier for color mapping disparity map
  123. disparityMultiplier = 255 / stereo.getMaxDisparity()
  124. # Colormap disparity for display.
  125. disp_frame = (disp_frame * disparityMultiplier).astype('uint8')
  126. # Apply color map to disparity map
  127. disp_frame = cv2.applyColorMap(disp_frame, cv2.COLORMAP_JET)
  128. bbox = None
  129. coordinates = None
  130. inDet = q_det.tryGet()
  131. if inDet is not None:
  132. detections = inDet.detections
  133. # if face detected
  134. if len(detections) is not 0:
  135. detection = detections[0]
  136. # Correct bounding box
  137. xmin = max(0, detection.xmin)
  138. ymin = max(0, detection.ymin)
  139. xmax = min(detection.xmax, 1)
  140. ymax = min(detection.ymax, 1)
  141. # Calculate coordinates
  142. x = int(xmin*FRAME_SIZE[0])
  143. y = int(ymin*FRAME_SIZE[1])
  144. w = int(xmax*FRAME_SIZE[0]-xmin*FRAME_SIZE[0])
  145. h = int(ymax*FRAME_SIZE[1]-ymin*FRAME_SIZE[1])
  146. bbox = (x, y, w, h)
  147. # Get spacial coordinates
  148. coord_x = detection.spatialCoordinates.x
  149. coord_y = detection.spatialCoordinates.y
  150. coord_z = detection.spatialCoordinates.z
  151. coordinates = (coord_x, coord_y, coord_z)
  152. # Check if a face was detected in the frame
  153. if bbox:
  154. # Face detected
  155. status = 'Face Detected'
  156. else:
  157. # No face detected
  158. status = 'No Face Detected'
  159. # Display info on frame
  160. display_info(frame, disp_frame, bbox, coordinates, status, status_color, fps)
  161. # Calculate average fps
  162. if frame_count % 10 == 0:
  163. # Time when we finish processing last 100 frames
  164. new_frame_time = time.time()
  165. # Fps will be number of frame processed in one second
  166. fps = 1 / ((new_frame_time - prev_frame_time)/10)
  167. prev_frame_time = new_frame_time
  168. # Capture the key pressed
  169. key_pressed = cv2.waitKey(1) & 0xff
  170. # Stop the program if Esc key was pressed
  171. if key_pressed == 27:
  172. break
  173. # Display the final frame
  174. cv2.imshow("Face Cam", frame)
  175. # Display the disparity frame
  176. cv2.imshow("Disparity Map", disp_frame)
  177. # Increment frame count
  178. frame_count += 1
  179. cv2.destroyAllWindows()