DepthAI Tutorials
DepthAI API References

ON THIS PAGE

  • RGB Camera Control
  • Similar samples:
  • Demo
  • Setup
  • Source code
  • Pipeline

RGB Camera Control

This example shows how to control the ColorCamera, such as exposure, sensitivity, white balance, luma/chroma denoise, device-side crop, camera triggers, etc.An output is a full frame along with cropped frame (video) which can be moved with WASD keys.List of all controls:
Command Line
1Uses 'WASD' controls to move the crop window, 'C' to capture a still image, 'T' to
2  trigger autofocus, 'IOKL,.NM' for manual exposure/focus/white-balance:
3Control:  key[dec/inc]  min..max
4exposure time: I   O  1..33000 [us]
5sensitivity iso:   K   L100..1600
6focus: ,   .  0..255 [far..near]
7white balance: N   M   1000..12000 (light color temperature K)
8
9  To go back to auto controls:
10'E' - autoexposure
11'F' - autofocus (continuous)
12'B' - auto white-balance
13
14  Other controls:
15'1' - AWB lock (true / false)
16'2' - AE lock (true / false)
17'3' - Select control: AWB mode
18'4' - Select control: AE compensation
19'5' - Select control: anti-banding/flicker mode
20'6' - Select control: effect mode
21'7' - Select control: brightness
22'8' - Select control: contrast
23'9' - Select control: saturation
24'0' - Select control: sharpness
25'[' - Select control: luma denoise
26']' - Select control: chroma denoise
27
28  For the 'Select control: ...' options, use these keys to modify the value:
29'-' or '_' to decrease
30'+' or '=' to increase

Similar samples:

Demo

Setup

Please run the install script to download all required dependencies. Please note that this script must be ran from git context, so you have to download the depthai-python repository first and then run the script
Command Line
1git clone https://github.com/luxonis/depthai-python.git
2cd depthai-python/examples
3python3 install_requirements.py
For additional information, please follow the installation guide.

Source code

Python
C++

Python

Python
GitHub
1#!/usr/bin/env python3
2
3"""
4This example shows usage of Camera Control message as well as ColorCamera configInput to change crop x and y
5Uses 'WASD' controls to move the crop window, 'C' to capture a still image, 'T' to trigger autofocus, 'IOKL,.NM'
6for manual exposure/focus/white-balance:
7  Control:      key[dec/inc]  min..max
8  exposure time:     I   O      1..33000 [us]
9  sensitivity iso:   K   L    100..1600
10  focus:             ,   .      0..255 [far..near]
11  white balance:     N   M   1000..12000 (light color temperature K)
12
13To go back to auto controls:
14  'E' - autoexposure
15  'F' - autofocus (continuous)
16  'B' - auto white-balance
17
18Other controls:
19    '1' - AWB lock (true / false)
20    '2' - AE lock (true / false)
21    '3' - Select control: AWB mode
22    '4' - Select control: AE compensation
23    '5' - Select control: anti-banding/flicker mode
24    '6' - Select control: effect mode
25    '7' - Select control: brightness
26    '8' - Select control: contrast
27    '9' - Select control: saturation
28    '0' - Select control: sharpness
29    '[' - Select control: luma denoise
30    ']' - Select control: chroma denoise
31
32For the 'Select control: ...' options, use these keys to modify the value:
33  '-' or '_' to decrease
34  '+' or '=' to increase
35
36'/' to toggle showing camera settings: exposure, ISO, lens position, color temperature
37"""
38
39import depthai as dai
40import cv2
41from itertools import cycle
42
43# Step size ('W','A','S','D' controls)
44STEP_SIZE = 8
45# Manual exposure/focus/white-balance set step
46EXP_STEP = 500  # us
47ISO_STEP = 50
48LENS_STEP = 3
49WB_STEP = 200
50
51def clamp(num, v0, v1):
52    return max(v0, min(num, v1))
53
54# Create pipeline
55pipeline = dai.Pipeline()
56
57# Define sources and outputs
58camRgb = pipeline.create(dai.node.ColorCamera)
59camRgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_1080_P)
60camRgb.setIspScale(2,3) # 1080P -> 720P
61stillEncoder = pipeline.create(dai.node.VideoEncoder)
62
63controlIn = pipeline.create(dai.node.XLinkIn)
64configIn = pipeline.create(dai.node.XLinkIn)
65ispOut = pipeline.create(dai.node.XLinkOut)
66videoOut = pipeline.create(dai.node.XLinkOut)
67stillMjpegOut = pipeline.create(dai.node.XLinkOut)
68
69controlIn.setStreamName('control')
70configIn.setStreamName('config')
71ispOut.setStreamName('isp')
72videoOut.setStreamName('video')
73stillMjpegOut.setStreamName('still')
74
75# Properties
76camRgb.setVideoSize(640,360)
77stillEncoder.setDefaultProfilePreset(1, dai.VideoEncoderProperties.Profile.MJPEG)
78
79# Linking
80camRgb.isp.link(ispOut.input)
81camRgb.still.link(stillEncoder.input)
82camRgb.video.link(videoOut.input)
83controlIn.out.link(camRgb.inputControl)
84configIn.out.link(camRgb.inputConfig)
85stillEncoder.bitstream.link(stillMjpegOut.input)
86
87# Connect to device and start pipeline
88with dai.Device(pipeline) as device:
89
90    # Get data queues
91    controlQueue = device.getInputQueue('control')
92    configQueue = device.getInputQueue('config')
93    ispQueue = device.getOutputQueue('isp')
94    videoQueue = device.getOutputQueue('video')
95    stillQueue = device.getOutputQueue('still')
96
97    # Max cropX & cropY
98    maxCropX = (camRgb.getIspWidth() - camRgb.getVideoWidth()) / camRgb.getIspWidth()
99    maxCropY = (camRgb.getIspHeight() - camRgb.getVideoHeight()) / camRgb.getIspHeight()
100    print(maxCropX, maxCropY, camRgb.getIspWidth(), camRgb.getVideoHeight())
101
102    # Default crop
103    cropX = 0
104    cropY = 0
105    sendCamConfig = True
106
107    # Defaults and limits for manual focus/exposure controls
108    lensPos = 150
109    expTime = 20000
110    sensIso = 800    
111    wbManual = 4000
112    ae_comp = 0
113    ae_lock = False
114    awb_lock = False
115    saturation = 0
116    contrast = 0
117    brightness = 0
118    sharpness = 0
119    luma_denoise = 0
120    chroma_denoise = 0
121    control = 'none'
122    show = False
123
124    awb_mode = cycle([item for name, item in vars(dai.CameraControl.AutoWhiteBalanceMode).items() if name.isupper()])
125    anti_banding_mode = cycle([item for name, item in vars(dai.CameraControl.AntiBandingMode).items() if name.isupper()])
126    effect_mode = cycle([item for name, item in vars(dai.CameraControl.EffectMode).items() if name.isupper()])
127
128    while True:
129        vidFrames = videoQueue.tryGetAll()
130        for vidFrame in vidFrames:
131            cv2.imshow('video', vidFrame.getCvFrame())
132
133        ispFrames = ispQueue.tryGetAll()
134        for ispFrame in ispFrames:
135            if show:
136                txt = f"[{ispFrame.getSequenceNum()}] "
137                txt += f"Exposure: {ispFrame.getExposureTime().total_seconds()*1000:.3f} ms, "
138                txt += f"ISO: {ispFrame.getSensitivity()}, "
139                txt += f"Lens position: {ispFrame.getLensPosition()}, "
140                txt += f"Color temp: {ispFrame.getColorTemperature()} K"
141                print(txt)
142            cv2.imshow('isp', ispFrame.getCvFrame())
143
144            # Send new cfg to camera
145            if sendCamConfig:
146                cfg = dai.ImageManipConfig()
147                cfg.setCropRect(cropX, cropY, 0, 0)
148                configQueue.send(cfg)
149                print('Sending new crop - x: ', cropX, ' y: ', cropY)
150                sendCamConfig = False
151
152        stillFrames = stillQueue.tryGetAll()
153        for stillFrame in stillFrames:
154            # Decode JPEG
155            frame = cv2.imdecode(stillFrame.getData(), cv2.IMREAD_UNCHANGED)
156            # Display
157            cv2.imshow('still', frame)
158
159        # Update screen (1ms pooling rate)
160        key = cv2.waitKey(1)
161        if key == ord('q'):
162            break
163        elif key == ord('/'):
164            show = not show
165            if not show: print("Printing camera settings: OFF")
166        elif key == ord('c'):
167            ctrl = dai.CameraControl()
168            ctrl.setCaptureStill(True)
169            controlQueue.send(ctrl)
170        elif key == ord('t'):
171            print("Autofocus trigger (and disable continuous)")
172            ctrl = dai.CameraControl()
173            ctrl.setAutoFocusMode(dai.CameraControl.AutoFocusMode.AUTO)
174            ctrl.setAutoFocusTrigger()
175            controlQueue.send(ctrl)
176        elif key == ord('f'):
177            print("Autofocus enable, continuous")
178            ctrl = dai.CameraControl()
179            ctrl.setAutoFocusMode(dai.CameraControl.AutoFocusMode.CONTINUOUS_VIDEO)
180            controlQueue.send(ctrl)
181        elif key == ord('e'):
182            print("Autoexposure enable")
183            ctrl = dai.CameraControl()
184            ctrl.setAutoExposureEnable()
185            controlQueue.send(ctrl)
186        elif key == ord('b'):
187            print("Auto white-balance enable")
188            ctrl = dai.CameraControl()
189            ctrl.setAutoWhiteBalanceMode(dai.CameraControl.AutoWhiteBalanceMode.AUTO)
190            controlQueue.send(ctrl)
191        elif key in [ord(','), ord('.')]:
192            if key == ord(','): lensPos -= LENS_STEP
193            if key == ord('.'): lensPos += LENS_STEP
194            lensPos = clamp(lensPos, 0, 255)
195            print("Setting manual focus, lens position: ", lensPos)
196            ctrl = dai.CameraControl()
197            ctrl.setManualFocus(lensPos)
198            controlQueue.send(ctrl)
199        elif key in [ord('i'), ord('o'), ord('k'), ord('l')]:
200            if key == ord('i'): expTime -= EXP_STEP
201            if key == ord('o'): expTime += EXP_STEP
202            if key == ord('k'): sensIso -= ISO_STEP
203            if key == ord('l'): sensIso += ISO_STEP
204            expTime = clamp(expTime, 1, 33000)
205            sensIso = clamp(sensIso, 100, 1600)
206            print("Setting manual exposure, time: ", expTime, "iso: ", sensIso)
207            ctrl = dai.CameraControl()
208            ctrl.setManualExposure(expTime, sensIso)
209            controlQueue.send(ctrl)
210        elif key in [ord('n'), ord('m')]:
211            if key == ord('n'): wbManual -= WB_STEP
212            if key == ord('m'): wbManual += WB_STEP
213            wbManual = clamp(wbManual, 1000, 12000)
214            print("Setting manual white balance, temperature: ", wbManual, "K")
215            ctrl = dai.CameraControl()
216            ctrl.setManualWhiteBalance(wbManual)
217            controlQueue.send(ctrl)
218        elif key in [ord('w'), ord('a'), ord('s'), ord('d')]:
219            if key == ord('a'):
220                cropX = cropX - (maxCropX / camRgb.getResolutionWidth()) * STEP_SIZE
221                if cropX < 0: cropX = 0
222            elif key == ord('d'):
223                cropX = cropX + (maxCropX / camRgb.getResolutionWidth()) * STEP_SIZE
224                if cropX > maxCropX: cropX = maxCropX
225            elif key == ord('w'):
226                cropY = cropY - (maxCropY / camRgb.getResolutionHeight()) * STEP_SIZE
227                if cropY < 0: cropY = 0
228            elif key == ord('s'):
229                cropY = cropY + (maxCropY / camRgb.getResolutionHeight()) * STEP_SIZE
230                if cropY > maxCropY: cropY = maxCropY
231            sendCamConfig = True
232        elif key == ord('1'):
233            awb_lock = not awb_lock
234            print("Auto white balance lock:", awb_lock)
235            ctrl = dai.CameraControl()
236            ctrl.setAutoWhiteBalanceLock(awb_lock)
237            controlQueue.send(ctrl)
238        elif key == ord('2'):
239            ae_lock = not ae_lock
240            print("Auto exposure lock:", ae_lock)
241            ctrl = dai.CameraControl()
242            ctrl.setAutoExposureLock(ae_lock)
243            controlQueue.send(ctrl)
244        elif key >= 0 and chr(key) in '34567890[]':
245            if   key == ord('3'): control = 'awb_mode'
246            elif key == ord('4'): control = 'ae_comp'
247            elif key == ord('5'): control = 'anti_banding_mode'
248            elif key == ord('6'): control = 'effect_mode'
249            elif key == ord('7'): control = 'brightness'
250            elif key == ord('8'): control = 'contrast'
251            elif key == ord('9'): control = 'saturation'
252            elif key == ord('0'): control = 'sharpness'
253            elif key == ord('['): control = 'luma_denoise'
254            elif key == ord(']'): control = 'chroma_denoise'
255            print("Selected control:", control)
256        elif key in [ord('-'), ord('_'), ord('+'), ord('=')]:
257            change = 0
258            if key in [ord('-'), ord('_')]: change = -1
259            if key in [ord('+'), ord('=')]: change = 1
260            ctrl = dai.CameraControl()
261            if control == 'none':
262                print("Please select a control first using keys 3..9 0 [ ]")
263            elif control == 'ae_comp':
264                ae_comp = clamp(ae_comp + change, -9, 9)
265                print("Auto exposure compensation:", ae_comp)
266                ctrl.setAutoExposureCompensation(ae_comp)
267            elif control == 'anti_banding_mode':
268                abm = next(anti_banding_mode)
269                print("Anti-banding mode:", abm)
270                ctrl.setAntiBandingMode(abm)
271            elif control == 'awb_mode':
272                awb = next(awb_mode)
273                print("Auto white balance mode:", awb)
274                ctrl.setAutoWhiteBalanceMode(awb)
275            elif control == 'effect_mode':
276                eff = next(effect_mode)
277                print("Effect mode:", eff)
278                ctrl.setEffectMode(eff)
279            elif control == 'brightness':
280                brightness = clamp(brightness + change, -10, 10)
281                print("Brightness:", brightness)
282                ctrl.setBrightness(brightness)
283            elif control == 'contrast':
284                contrast = clamp(contrast + change, -10, 10)
285                print("Contrast:", contrast)
286                ctrl.setContrast(contrast)
287            elif control == 'saturation':
288                saturation = clamp(saturation + change, -10, 10)
289                print("Saturation:", saturation)
290                ctrl.setSaturation(saturation)
291            elif control == 'sharpness':
292                sharpness = clamp(sharpness + change, 0, 4)
293                print("Sharpness:", sharpness)
294                ctrl.setSharpness(sharpness)
295            elif control == 'luma_denoise':
296                luma_denoise = clamp(luma_denoise + change, 0, 4)
297                print("Luma denoise:", luma_denoise)
298                ctrl.setLumaDenoise(luma_denoise)
299            elif control == 'chroma_denoise':
300                chroma_denoise = clamp(chroma_denoise + change, 0, 4)
301                print("Chroma denoise:", chroma_denoise)
302                ctrl.setChromaDenoise(chroma_denoise)
303            controlQueue.send(ctrl)

Pipeline

Need assistance?

Head over to Discussion Forum for technical support or any other questions you might have.