-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol_camera_zed.py
206 lines (185 loc) · 9.38 KB
/
control_camera_zed.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
########################################################################
#
# Copyright (c) 2022, STEREOLABS.
#
# All rights reserved.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
########################################################################
"""
Live camera sample showing the camera information and video in real time and allows to control the different
settings.
"""
import cv2
import pyzed.sl as sl
# Global variable
camera_settings = sl.VIDEO_SETTINGS.BRIGHTNESS
str_camera_settings = "BRIGHTNESS"
step_camera_settings = 1
led_on = True
selection_rect = sl.Rect()
select_in_progress = False
origin_rect = (-1, -1)
# Function that handles mouse events when interacting with the OpenCV window.
def on_mouse(event, x, y, flags, param):
global select_in_progress, selection_rect, origin_rect
if event == cv2.EVENT_LBUTTONDOWN:
origin_rect = (x, y)
select_in_progress = True
elif event == cv2.EVENT_LBUTTONUP:
select_in_progress = False
elif event == cv2.EVENT_RBUTTONDOWN:
select_in_progress = False
selection_rect = sl.Rect(0, 0, 0, 0)
if select_in_progress:
selection_rect.x = min(x, origin_rect[0])
selection_rect.y = min(y, origin_rect[1])
selection_rect.width = abs(x - origin_rect[0]) + 1
selection_rect.height = abs(y - origin_rect[1]) + 1
def main():
init = sl.InitParameters()
cam = sl.Camera()
status = cam.open(init)
if status != sl.ERROR_CODE.SUCCESS:
print("Camera Open : " + repr(status) + ". Exit program.")
exit()
runtime = sl.RuntimeParameters()
mat = sl.Mat()
win_name = "Camera Control"
cv2.namedWindow(win_name)
cv2.setMouseCallback(win_name, on_mouse)
print_camera_information(cam)
print_help()
switch_camera_settings()
key = ''
while key != 113: # for 'q' key
err = cam.grab(runtime)
if err == sl.ERROR_CODE.SUCCESS: # Check that a new image is successfully acquired
cam.retrieve_image(mat, sl.VIEW.LEFT) # Retrieve left image
cvImage = mat.get_data() # Convert sl.Mat to cv2.Mat
if (not selection_rect.is_empty() and selection_rect.is_contained(sl.Rect(0, 0, cvImage.shape[1],
cvImage.shape[
0]))): # Check if selection rectangle is valid and draw it on the image
cv2.rectangle(cvImage, (selection_rect.x, selection_rect.y),
(selection_rect.width + selection_rect.x, selection_rect.height + selection_rect.y),
(220, 180, 20), 2)
cv2.imshow(win_name, cvImage) # Display image
else:
print("Error during capture : ", err)
break
key = cv2.waitKey(5)
# Change camera settings with keyboard
update_camera_settings(key, cam, runtime, mat)
cv2.destroyAllWindows()
cam.close()
# Display camera information
def print_camera_information(cam):
cam_info = cam.get_camera_information()
print("ZED Model : {0}".format(cam_info.camera_model))
print("ZED Serial Number : {0}".format(cam_info.serial_number))
print("ZED Camera Firmware : {0}/{1}".format(cam_info.camera_configuration.firmware_version,
cam_info.sensors_configuration.firmware_version))
print("ZED Camera Resolution : {0}x{1}".format(round(cam_info.camera_configuration.resolution.width, 2),
cam.get_camera_information().camera_configuration.resolution.height))
print("ZED Camera FPS : {0}".format(int(cam_info.camera_configuration.fps)))
# Print help
def print_help():
print("\n\nCamera controls hotkeys:")
print("* Increase camera settings value: '+'")
print("* Decrease camera settings value: '-'")
print("* Toggle camera settings: 's'")
print("* Toggle camera LED: 'l' (lower L)")
print("* Reset all parameters: 'r'")
print("* Reset exposure ROI to full image 'f'")
print("* Use mouse to select an image area to apply exposure (press 'a')")
print("* Exit : 'q'\n")
# update camera setting on key press
def update_camera_settings(key, cam, runtime, mat):
global led_on
if key == 115: # for 's' key
# Switch camera settings
switch_camera_settings()
elif key == 43: # for '+' key
# Increase camera settings value.
current_value = cam.get_camera_settings(camera_settings)[1]
cam.set_camera_settings(camera_settings, current_value + step_camera_settings)
print(str_camera_settings + ": " + str(current_value + step_camera_settings))
elif key == 45: # for '-' key
# Decrease camera settings value.
current_value = cam.get_camera_settings(camera_settings)[1]
if current_value >= 1:
cam.set_camera_settings(camera_settings, current_value - step_camera_settings)
print(str_camera_settings + ": " + str(current_value - step_camera_settings))
elif key == 114: # for 'r' key
# Reset all camera settings to default.
cam.set_camera_settings(sl.VIDEO_SETTINGS.BRIGHTNESS, -1)
cam.set_camera_settings(sl.VIDEO_SETTINGS.CONTRAST, -1)
cam.set_camera_settings(sl.VIDEO_SETTINGS.HUE, -1)
cam.set_camera_settings(sl.VIDEO_SETTINGS.SATURATION, -1)
cam.set_camera_settings(sl.VIDEO_SETTINGS.SHARPNESS, -1)
cam.set_camera_settings(sl.VIDEO_SETTINGS.GAIN, -1)
cam.set_camera_settings(sl.VIDEO_SETTINGS.EXPOSURE, -1)
cam.set_camera_settings(sl.VIDEO_SETTINGS.WHITEBALANCE_TEMPERATURE, -1)
print("[Sample] Reset all settings to default")
elif key == 108: # for 'l' key
# Turn on or off camera LED.
led_on = not led_on
cam.set_camera_settings(sl.VIDEO_SETTINGS.LED_STATUS, led_on)
elif key == 97: # for 'a' key
# Set exposure region of interest (ROI) on a target area.
print("[Sample] set AEC_AGC_ROI on target [", selection_rect.x, ",", selection_rect.y, ",",
selection_rect.width, ",", selection_rect.height, "]")
cam.set_camera_settings_roi(sl.VIDEO_SETTINGS.AEC_AGC_ROI, selection_rect, sl.SIDE.BOTH)
elif key == 102: # for 'f' key
# Reset exposure ROI to full resolution.
print("[Sample] reset AEC_AGC_ROI to full res")
cam.set_camera_settings_roi(sl.VIDEO_SETTINGS.AEC_AGC_ROI, selection_rect, sl.SIDE.BOTH, True)
# Function to switch between different camera settings (brightness, contrast, etc.).
def switch_camera_settings():
global camera_settings
global str_camera_settings
if camera_settings == sl.VIDEO_SETTINGS.BRIGHTNESS:
camera_settings = sl.VIDEO_SETTINGS.CONTRAST
str_camera_settings = "Contrast"
print("[Sample] Switch to camera settings: CONTRAST")
elif camera_settings == sl.VIDEO_SETTINGS.CONTRAST:
camera_settings = sl.VIDEO_SETTINGS.HUE
str_camera_settings = "Hue"
print("[Sample] Switch to camera settings: HUE")
elif camera_settings == sl.VIDEO_SETTINGS.HUE:
camera_settings = sl.VIDEO_SETTINGS.SATURATION
str_camera_settings = "Saturation"
print("[Sample] Switch to camera settings: SATURATION")
elif camera_settings == sl.VIDEO_SETTINGS.SATURATION:
camera_settings = sl.VIDEO_SETTINGS.SHARPNESS
str_camera_settings = "Sharpness"
print("[Sample] Switch to camera settings: Sharpness")
elif camera_settings == sl.VIDEO_SETTINGS.SHARPNESS:
camera_settings = sl.VIDEO_SETTINGS.GAIN
str_camera_settings = "Gain"
print("[Sample] Switch to camera settings: GAIN")
elif camera_settings == sl.VIDEO_SETTINGS.GAIN:
camera_settings = sl.VIDEO_SETTINGS.EXPOSURE
str_camera_settings = "Exposure"
print("[Sample] Switch to camera settings: EXPOSURE")
elif camera_settings == sl.VIDEO_SETTINGS.EXPOSURE:
camera_settings = sl.VIDEO_SETTINGS.WHITEBALANCE_TEMPERATURE
str_camera_settings = "White Balance"
print("[Sample] Switch to camera settings: WHITEBALANCE")
elif camera_settings == sl.VIDEO_SETTINGS.WHITEBALANCE_TEMPERATURE:
camera_settings = sl.VIDEO_SETTINGS.BRIGHTNESS
str_camera_settings = "Brightness"
print("[Sample] Switch to camera settings: BRIGHTNESS")
if __name__ == "__main__":
main()