forked from stereolabs/zed-python-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlive_camera.py
169 lines (149 loc) · 6.82 KB
/
live_camera.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
########################################################################
#
# Copyright (c) 2017, 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
camera_settings = sl.CAMERA_SETTINGS.CAMERA_SETTINGS_BRIGHTNESS
str_camera_settings = "BRIGHTNESS"
step_camera_settings = 1
def main():
print("Running...")
init = sl.InitParameters()
cam = sl.Camera()
if not cam.is_opened():
print("Opening ZED Camera...")
status = cam.open(init)
if status != sl.ERROR_CODE.SUCCESS:
print(repr(status))
exit()
runtime = sl.RuntimeParameters()
mat = sl.Mat()
print_camera_information(cam)
print_help()
key = ''
while key != 113: # for 'q' key
err = cam.grab(runtime)
if err == sl.ERROR_CODE.SUCCESS:
cam.retrieve_image(mat, sl.VIEW.VIEW_LEFT)
cv2.imshow("ZED", mat.get_data())
key = cv2.waitKey(5)
settings(key, cam, runtime, mat)
else:
key = cv2.waitKey(5)
cv2.destroyAllWindows()
cam.close()
print("\nFINISH")
def print_camera_information(cam):
print("Resolution: {0}, {1}.".format(round(cam.get_resolution().width, 2), cam.get_resolution().height))
print("Camera FPS: {0}.".format(cam.get_camera_fps()))
print("Firmware: {0}.".format(cam.get_camera_information().firmware_version))
print("Serial number: {0}.\n".format(cam.get_camera_information().serial_number))
def print_help():
print("Help for camera setting controls")
print(" Increase camera settings value: +")
print(" Decrease camera settings value: -")
print(" Switch camera settings: s")
print(" Reset all parameters: r")
print(" Record a video: z")
print(" Quit: q\n")
def settings(key, cam, runtime, mat):
if key == 115: # for 's' key
switch_camera_settings()
elif key == 43: # for '+' key
current_value = cam.get_camera_settings(camera_settings)
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
current_value = cam.get_camera_settings(camera_settings)
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
cam.set_camera_settings(sl.CAMERA_SETTINGS.CAMERA_SETTINGS_BRIGHTNESS, -1, True)
cam.set_camera_settings(sl.CAMERA_SETTINGS.CAMERA_SETTINGS_CONTRAST, -1, True)
cam.set_camera_settings(sl.CAMERA_SETTINGS.CAMERA_SETTINGS_HUE, -1, True)
cam.set_camera_settings(sl.CAMERA_SETTINGS.CAMERA_SETTINGS_SATURATION, -1, True)
cam.set_camera_settings(sl.CAMERA_SETTINGS.CAMERA_SETTINGS_GAIN, -1, True)
cam.set_camera_settings(sl.CAMERA_SETTINGS.CAMERA_SETTINGS_EXPOSURE, -1, True)
cam.set_camera_settings(sl.CAMERA_SETTINGS.CAMERA_SETTINGS_WHITEBALANCE, -1, True)
print("Camera settings: reset")
elif key == 122: # for 'z' key
record(cam, runtime, mat)
def switch_camera_settings():
global camera_settings
global str_camera_settings
if camera_settings == sl.CAMERA_SETTINGS.CAMERA_SETTINGS_BRIGHTNESS:
camera_settings = sl.CAMERA_SETTINGS.CAMERA_SETTINGS_CONTRAST
str_camera_settings = "Contrast"
print("Camera settings: CONTRAST")
elif camera_settings == sl.CAMERA_SETTINGS.CAMERA_SETTINGS_CONTRAST:
camera_settings = sl.CAMERA_SETTINGS.CAMERA_SETTINGS_HUE
str_camera_settings = "Hue"
print("Camera settings: HUE")
elif camera_settings == sl.CAMERA_SETTINGS.CAMERA_SETTINGS_HUE:
camera_settings = sl.CAMERA_SETTINGS.CAMERA_SETTINGS_SATURATION
str_camera_settings = "Saturation"
print("Camera settings: SATURATION")
elif camera_settings == sl.CAMERA_SETTINGS.CAMERA_SETTINGS_SATURATION:
camera_settings = sl.CAMERA_SETTINGS.CAMERA_SETTINGS_GAIN
str_camera_settings = "Gain"
print("Camera settings: GAIN")
elif camera_settings == sl.CAMERA_SETTINGS.CAMERA_SETTINGS_GAIN:
camera_settings = sl.CAMERA_SETTINGS.CAMERA_SETTINGS_EXPOSURE
str_camera_settings = "Exposure"
print("Camera settings: EXPOSURE")
elif camera_settings == sl.CAMERA_SETTINGS.CAMERA_SETTINGS_EXPOSURE:
camera_settings = sl.CAMERA_SETTINGS.CAMERA_SETTINGS_WHITEBALANCE
str_camera_settings = "White Balance"
print("Camera settings: WHITEBALANCE")
elif camera_settings == sl.CAMERA_SETTINGS.CAMERA_SETTINGS_WHITEBALANCE:
camera_settings = sl.CAMERA_SETTINGS.CAMERA_SETTINGS_BRIGHTNESS
str_camera_settings = "Brightness"
print("Camera settings: BRIGHTNESS")
def record(cam, runtime, mat):
vid = sl.ERROR_CODE.ERROR_CODE_FAILURE
out = False
while vid != sl.ERROR_CODE.SUCCESS and not out:
filepath = input("Enter filepath name: ")
vid = cam.enable_recording(filepath)
print(repr(vid))
if vid == sl.ERROR_CODE.SUCCESS:
print("Recording started...")
out = True
print("Hit spacebar to stop recording: ")
key = False
while key != 32: # for spacebar
err = cam.grab(runtime)
if err == sl.ERROR_CODE.SUCCESS:
cam.retrieve_image(mat)
cv2.imshow("ZED", mat.get_data())
key = cv2.waitKey(5)
cam.record()
else:
print("Help: you must enter the filepath + filename + SVO extension.")
print("Recording not started.")
cam.disable_recording()
print("Recording finished.")
cv2.destroyAllWindows()
if __name__ == "__main__":
main()