Guest User

Untitled

a guest
Oct 13th, 2023
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import cv2
  4. import math
  5. import depthai as dai
  6. import contextlib
  7. import argparse
  8. from datetime import timedelta
  9.  
  10. parser = argparse.ArgumentParser(epilog='Press C to capture a set of frames.')
  11. parser.add_argument('-f', '--fps', type=float, default=30,
  12. help='Camera sensor FPS, applied to all cams')
  13.  
  14. args = parser.parse_args()
  15.  
  16. cam_socket_opts = {
  17. 'rgb' : dai.CameraBoardSocket.RGB,
  18. 'left' : dai.CameraBoardSocket.LEFT,
  19. 'right': dai.CameraBoardSocket.RIGHT,
  20. 'camd': dai.CameraBoardSocket.CAM_D,
  21. }
  22.  
  23. def create_pipeline():
  24. # Start defining a pipeline
  25. pipeline = dai.Pipeline()
  26. def create(c, output=False):
  27. xout = pipeline.create(dai.node.XLinkOut)
  28. xout.setStreamName(c)
  29.  
  30. cam = pipeline.create(dai.node.ColorCamera)
  31. cam.setResolution(dai.ColorCameraProperties.SensorResolution.THE_800_P)
  32. cam.isp.link(xout.input)
  33. # cam.initialControl.setManualExposure(1000, 800)
  34. if output:
  35. cam.initialControl.setFrameSyncMode(dai.CameraControl.FrameSyncMode.OUTPUT)
  36. else:
  37. cam.initialControl.setFrameSyncMode(dai.CameraControl.FrameSyncMode.INPUT)
  38. cam.setBoardSocket(cam_socket_opts[c])
  39. cam.setFps(args.fps)
  40.  
  41. create('left', output=True)
  42. create('right')
  43. create('rgb')
  44. create('camd')
  45. return pipeline
  46.  
  47.  
  48. # https://docs.python.org/3/library/contextlib.html#contextlib.ExitStack
  49. with contextlib.ExitStack() as stack:
  50. device_infos = dai.Device.getAllAvailableDevices()
  51.  
  52. if len(device_infos) == 0: raise RuntimeError("No devices found!")
  53. else: print("Found", len(device_infos), "devices")
  54. queues = []
  55. for device_info in device_infos:
  56. # for ip in ips:
  57. # device_info = dai.DeviceInfo(ip)
  58. # Note: the pipeline isn't set here, as we don't know yet what device it is.
  59. # The extra arguments passed are required by the existing overload variants
  60. openvino_version = dai.OpenVINO.Version.VERSION_2021_4
  61. usb2_mode = False
  62.  
  63. config = dai.Device.Config()
  64. # For FFC-4P R6 (revision 6) or higher, we changed FSIN_MODE_SELECT from GPIO6 to GPIO38!
  65. config.board.gpio[6] = dai.BoardConfig.GPIO(dai.BoardConfig.GPIO.OUTPUT,
  66. dai.BoardConfig.GPIO.Level.HIGH)
  67.  
  68. device = stack.enter_context(dai.Device(config=config))
  69.  
  70. cam_list = {'left', 'right', 'rgb', 'camd'}
  71. # cam_list = {'left', 'right'}
  72. # cam_list = {'right', 'camd'}
  73.  
  74. print('Starting pipeline for', device.getMxId())
  75. # Get a customized pipeline based on identified device type
  76. device.startPipeline(create_pipeline())
  77.  
  78. # Output queue will be used to get the rgb frames from the output defined above
  79. for cam in cam_list:
  80. queues.append({
  81. 'queue': device.getOutputQueue(name=cam, maxSize=4, blocking=False),
  82. 'msgs': [], # Frame msgs
  83. 'mx': device.getMxId(),
  84. 'cam': cam
  85. })
  86.  
  87.  
  88. def check_sync(queues, timestamp):
  89. matching_frames = []
  90. for q in queues:
  91. for i, msg in enumerate(q['msgs']):
  92. time_diff = abs(msg.getTimestamp() - timestamp)
  93. # So below 17ms @ 30 FPS => frames are in sync
  94. if time_diff <= timedelta(milliseconds=math.ceil(500 / args.fps)):
  95. matching_frames.append(i)
  96. break
  97.  
  98. if len(matching_frames) == len(queues):
  99. # We have all frames synced. Remove the excess ones
  100. for i, q in enumerate(queues):
  101. q['msgs'] = q['msgs'][matching_frames[i]:]
  102. return True
  103. else:
  104. return False
  105.  
  106. while True:
  107. for q in queues:
  108. new_msg = q['queue'].tryGet()
  109. if new_msg is not None:
  110. print(f"[{q['mx']}] Got {q['cam']} frame, seq: {new_msg.getSequenceNum()} TS: {new_msg.getTimestamp()}")
  111. q['msgs'].append(new_msg)
  112. if check_sync(queues, new_msg.getTimestamp()):
  113. for q in queues:
  114. imgFrame: dai.ImgFrame = q['msgs'].pop(0)
  115. frame = imgFrame.getCvFrame()
  116. # If monochrome, convert to RGB
  117. if frame.ndim == 2:
  118. frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)
  119. # Write text
  120. frame = cv2.putText(frame, f"TS {imgFrame.getTimestamp(dai.CameraExposureOffset.MIDDLE)}", (10, 30), cv2.FONT_HERSHEY_TRIPLEX, 1.0, color=(127, 255, 0))
  121. frame = cv2.putText(frame, f"Seq {imgFrame.getSequenceNum()}, Exposure: {imgFrame.getExposureTime().total_seconds() * 1000:.2f}ms", (10, 80), cv2.FONT_HERSHEY_TRIPLEX, 1.0, color=(127, 255, 0))
  122. cv2.imshow(f"{q['cam']} - {q['mx']}", frame)
  123. if cv2.waitKey(1) == ord('q'):
  124. break
  125.  
Advertisement
Add Comment
Please, Sign In to add comment