color-camera-control.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/usr/bin/env python3
  2. """
  3. This example shows usage of Camera Control message as well as ColorCamera configInput to change crop x and y
  4. Uses 'WASD' controls to move the crop window, 'C' to capture a still image, 'T' to trigger autofocus, 'IOKL,.'
  5. for manual exposure/focus:
  6. Control: key[dec/inc] min..max
  7. exposure time: I O 1..33000 [us]
  8. sensitivity iso: K L 100..1600
  9. focus: , . 0..255 [far..near]
  10. To go back to auto controls:
  11. 'E' - autoexposure
  12. 'F' - autofocus (continuous)
  13. """
  14. import depthai as dai
  15. import cv2
  16. # Step size ('W','A','S','D' controls)
  17. STEP_SIZE = 8
  18. # Manual exposure/focus set step
  19. EXP_STEP = 500 # us
  20. ISO_STEP = 50
  21. LENS_STEP = 3
  22. def clamp(num, v0, v1):
  23. return max(v0, min(num, v1))
  24. # Create pipeline
  25. pipeline = dai.Pipeline()
  26. # Define sources and outputs
  27. camRgb = pipeline.createColorCamera()
  28. videoEncoder = pipeline.createVideoEncoder()
  29. stillEncoder = pipeline.createVideoEncoder()
  30. controlIn = pipeline.createXLinkIn()
  31. configIn = pipeline.createXLinkIn()
  32. videoMjpegOut = pipeline.createXLinkOut()
  33. stillMjpegOut = pipeline.createXLinkOut()
  34. previewOut = pipeline.createXLinkOut()
  35. controlIn.setStreamName('control')
  36. configIn.setStreamName('config')
  37. videoMjpegOut.setStreamName('video')
  38. stillMjpegOut.setStreamName('still')
  39. previewOut.setStreamName('preview')
  40. # Properties
  41. camRgb.setVideoSize(640, 360)
  42. camRgb.setPreviewSize(300, 300)
  43. videoEncoder.setDefaultProfilePreset(camRgb.getVideoSize(), camRgb.getFps(), dai.VideoEncoderProperties.Profile.MJPEG)
  44. stillEncoder.setDefaultProfilePreset(camRgb.getStillSize(), 1, dai.VideoEncoderProperties.Profile.MJPEG)
  45. # Linking
  46. camRgb.video.link(videoEncoder.input)
  47. camRgb.still.link(stillEncoder.input)
  48. camRgb.preview.link(previewOut.input)
  49. controlIn.out.link(camRgb.inputControl)
  50. configIn.out.link(camRgb.inputConfig)
  51. videoEncoder.bitstream.link(videoMjpegOut.input)
  52. stillEncoder.bitstream.link(stillMjpegOut.input)
  53. # Connect to device and start pipeline
  54. with dai.Device(pipeline) as device:
  55. # Get data queues
  56. controlQueue = device.getInputQueue('control')
  57. configQueue = device.getInputQueue('config')
  58. previewQueue = device.getOutputQueue('preview')
  59. videoQueue = device.getOutputQueue('video')
  60. stillQueue = device.getOutputQueue('still')
  61. # Max cropX & cropY
  62. maxCropX = (camRgb.getResolutionWidth() - camRgb.getVideoWidth()) / camRgb.getResolutionWidth()
  63. maxCropY = (camRgb.getResolutionHeight() - camRgb.getVideoHeight()) / camRgb.getResolutionHeight()
  64. # Default crop
  65. cropX = 0
  66. cropY = 0
  67. sendCamConfig = True
  68. # Defaults and limits for manual focus/exposure controls
  69. lensPos = 150
  70. lensMin = 0
  71. lensMax = 255
  72. expTime = 20000
  73. expMin = 1
  74. expMax = 33000
  75. sensIso = 800
  76. sensMin = 100
  77. sensMax = 1600
  78. while True:
  79. previewFrames = previewQueue.tryGetAll()
  80. for previewFrame in previewFrames:
  81. cv2.imshow('preview', previewFrame.getData().reshape(previewFrame.getWidth(), previewFrame.getHeight(), 3))
  82. videoFrames = videoQueue.tryGetAll()
  83. for videoFrame in videoFrames:
  84. # Decode JPEG
  85. frame = cv2.imdecode(videoFrame.getData(), cv2.IMREAD_UNCHANGED)
  86. # Display
  87. cv2.imshow('video', frame)
  88. # Send new cfg to camera
  89. if sendCamConfig:
  90. cfg = dai.ImageManipConfig()
  91. cfg.setCropRect(cropX, cropY, 0, 0)
  92. configQueue.send(cfg)
  93. print('Sending new crop - x: ', cropX, ' y: ', cropY)
  94. sendCamConfig = False
  95. stillFrames = stillQueue.tryGetAll()
  96. for stillFrame in stillFrames:
  97. # Decode JPEG
  98. frame = cv2.imdecode(stillFrame.getData(), cv2.IMREAD_UNCHANGED)
  99. # Display
  100. cv2.imshow('still', frame)
  101. # Update screen (1ms pooling rate)
  102. key = cv2.waitKey(1)
  103. if key == 27:
  104. break
  105. elif key == ord('c'):
  106. ctrl = dai.CameraControl()
  107. ctrl.setCaptureStill(True)
  108. controlQueue.send(ctrl)
  109. elif key == ord('t'):
  110. print("Autofocus trigger (and disable continuous)")
  111. ctrl = dai.CameraControl()
  112. ctrl.setAutoFocusMode(dai.CameraControl.AutoFocusMode.AUTO)
  113. ctrl.setAutoFocusTrigger()
  114. controlQueue.send(ctrl)
  115. elif key == ord('f'):
  116. print("Autofocus enable, continuous")
  117. ctrl = dai.CameraControl()
  118. ctrl.setAutoFocusMode(dai.CameraControl.AutoFocusMode.CONTINUOUS_VIDEO)
  119. controlQueue.send(ctrl)
  120. elif key == ord('e'):
  121. print("Autoexposure enable")
  122. ctrl = dai.CameraControl()
  123. ctrl.setAutoExposureEnable()
  124. controlQueue.send(ctrl)
  125. elif key in [ord(','), ord('.')]:
  126. if key == ord(','): lensPos -= LENS_STEP
  127. if key == ord('.'): lensPos += LENS_STEP
  128. lensPos = clamp(lensPos, lensMin, lensMax)
  129. print("Setting manual focus, lens position: ", lensPos)
  130. ctrl = dai.CameraControl()
  131. ctrl.setManualFocus(lensPos)
  132. controlQueue.send(ctrl)
  133. elif key in [ord('i'), ord('o'), ord('k'), ord('l')]:
  134. if key == ord('i'): expTime -= EXP_STEP
  135. if key == ord('o'): expTime += EXP_STEP
  136. if key == ord('k'): sensIso -= ISO_STEP
  137. if key == ord('l'): sensIso += ISO_STEP
  138. expTime = clamp(expTime, expMin, expMax)
  139. sensIso = clamp(sensIso, sensMin, sensMax)
  140. print("Setting manual exposure, time: ", expTime, "iso: ", sensIso)
  141. ctrl = dai.CameraControl()
  142. ctrl.setManualExposure(expTime, sensIso)
  143. controlQueue.send(ctrl)
  144. elif key in [ord('w'), ord('a'), ord('s'), ord('d')]:
  145. if key == ord('a'):
  146. cropX = cropX - (maxCropX / camRgb.getResolutionWidth()) * STEP_SIZE
  147. if cropX < 0: cropX = maxCropX
  148. elif key == ord('d'):
  149. cropX = cropX + (maxCropX / camRgb.getResolutionWidth()) * STEP_SIZE
  150. if cropX > maxCropX: cropX = 0
  151. elif key == ord('w'):
  152. cropY = cropY - (maxCropY / camRgb.getResolutionHeight()) * STEP_SIZE
  153. if cropY < 0: cropY = maxCropY
  154. elif key == ord('s'):
  155. cropY = cropY + (maxCropY / camRgb.getResolutionHeight()) * STEP_SIZE
  156. if cropY > maxCropY: cropY = 0
  157. sendCamConfig = True