forked from meantux/GugusseRoller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCameraSettings.py
More file actions
388 lines (327 loc) · 13 KB
/
CameraSettings.py
File metadata and controls
388 lines (327 loc) · 13 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
from PyQt5.QtWidgets import QSlider, QComboBox, QLabel, QPushButton, QCheckBox
from PyQt5.QtCore import Qt
from libcamera import controls, Transform
from picamera2 import Preview
from picamera2.previews.qt import QGlPicamera2
defaultValues = {
"Exposure": "Manual",
"ExposureMicroseconds": 30000,
"ExposureCompensationStops": 0.0,
"ISO": 100,
"RedGain": "2.1",
"BlueGain": "2.1",
"WhileBalanceMode": "Auto",
}
def SetMissingsToDefault(settings):
for key in defaultValues:
if key not in settings:
settings[key] = defaultValues[key]
class AutoExposureWidget(QComboBox):
def __init__(self, win):
QComboBox.__init__(self)
self.label = QLabel("Exposure")
self.addItems(["Manual", "CentreWeighted", "Spot", "Matrix"])
self.win = win
SetMissingsToDefault(win.settings)
mode = win.settings["Exposure"]
self.setCurrentText(mode)
self.currentTextChanged.connect(self.handle)
def handle(self, text):
self.syncCamera()
def syncCamera(self):
choice = self.currentText()
self.win.settings["Exposure"] = choice
if choice == "Manual":
self.win.picam2.set_controls({"AeEnable": False})
self.win.ExposureDual.changeMode()
self.win.Iso.setEnabled(True)
self.win.Iso.syncCamera()
else:
self.win.picam2.set_controls({"AeEnable": True})
self.win.ExposureDual.changeMode()
self.win.Iso.setEnabled(False)
self.win.ExposureDual.syncCamera()
if choice == "CentreWeighted":
self.win.picam2.set_controls(
{"AeMeteringMode": controls.AeMeteringModeEnum.CentreWeighted}
)
elif choice == "Spot":
self.win.picam2.set_controls(
{"AeMeteringMode": controls.AeMeteringModeEnum.Spot}
)
elif choice == "Matrix":
self.win.picam2.set_controls(
{"AeMeteringMode": controls.AeMeteringModeEnum.CentreWeighted}
)
def getLabel(self):
return self.label
class previewWindowWidget(QGlPicamera2):
def __init__(self, win):
QGlPicamera2.__init__(self, win.picam2)
self.zoomed = False
self.win = win
def mousePressEvent(self, event):
if self.win.runStop.isCapturing():
self.win.out.append("Zoom is disabled while capturing")
return
pos = event.pos()
x = pos.x()
y = pos.y()
winw = self.width()
winh = self.height()
cfg = self.win.picam2.getConfig()
sensorw = cfg["main"]["size"][0]
sensorh = cfg["main"]["size"][1]
if self.zoomed:
self.win.picam2.set_controls({"ScalerCrop": (0, 0, sensorw, sensorh)})
self.zoomed = False
return
ratioSensor = sensorw / sensorh
ratioWin = winw / winh
leftBlackPixels = 0
upperBlackPixels = 0
if ratioSensor > ratioWin:
upperBlackPixels = int((winh - (winw / ratioSensor)) / 2)
winh = int(winw / ratioSensor)
elif ratioSensor < ratioWin:
leftBlackPixels = int((winw - (winh * ratioSensor)) / 2)
winw = int(winh * ratioSensor)
scalew = sensorw / winw
scaleh = sensorh / winh
clickx = (x - leftBlackPixels) * scalew
clicky = (y - upperBlackPixels) * scaleh
# Make sure the crop doesn't exceed image borders
x1 = int(max(0, min(clickx - (winw // 2), sensorw - winw)))
y1 = int(max(0, min(clicky - (winh // 2), sensorh - winh)))
self.win.picam2.set_controls({"ScalerCrop": (x1, y1, winw, winh)})
self.zoomed = True
class ExposureDualWidget(QSlider):
def __init__(self, win):
QSlider.__init__(self, Qt.Horizontal)
self.win = win
self.label = QLabel(".........")
self.changeMode()
self.valueChanged.connect(self.handle)
def changeMode(self):
exposureMode = self.win.settings["Exposure"]
if exposureMode == "Manual":
min = self.win.picam2.video_configuration.controls.FrameDurationLimits[0]
max = self.win.picam2.video_configuration.controls.FrameDurationLimits[1]
value = self.win.settings["ExposureMicroseconds"]
else:
min = int(self.win.picam2.camera_controls.ExposureValue[0] * 2)
max = int(self.win.picam2.camera_controls.ExposureValue[1] * 2)
realValue = self.win.settings["ExposureCompensationStops"]
value = int(realValue * 2.0)
self.setRange(min, max)
self.handle(value)
def handle(self, value):
exposureMode = self.win.settings["Exposure"]
if exposureMode == "Manual":
self.label.setText(f"{value//1000:4d}ms")
self.win.settings["ExposureMicroseconds"] = value
else:
realValue = float(value) / 2.0
if realValue >= 0.0:
self.label.setText(f"+{realValue:3.1f} stops")
else:
self.label.setText(f"{realValue:4.1f} stops")
self.win.settings["ExposureCompensationStops"] = realValue
self.syncCamera()
self.setValue(value)
def syncCamera(self):
exposureMode = self.win.settings["Exposure"]
if exposureMode == "Manual":
self.win.picam2.set_controls({"ExposureTime": self.value()})
else:
realValue = float(self.value()) / 2.0
self.win.picam2.set_controls({"ExposureValue": realValue})
def getLabel(self):
return self.label
# For Sharpness / Brigthness / Contrast / Saturation
class GenericCameraAdjustmentWidget(QSlider):
def __init__(self, win, name, customMin=None, customMax=None):
QSlider.__init__(self, Qt.Horizontal)
self.win = win
self.name = name
if customMin != None:
self.min = customMin
else:
self.min = win.picam2.camera_controls[name][0]
if customMax != None:
self.max = customMax
else:
self.max = win.picam2.camera_controls[name][1]
self.setRange(0, 100)
if name not in win.settings:
win.settings[name] = win.picam2.camera_controls[name][2]
self.setValue(self.registerValue2SliderValue(win.settings[name]))
self.label = QLabel(f"{name}: {win.settings[name]:5.02f}")
self.valueChanged.connect(self.handle)
def registerValue2SliderValue(self, rval):
sval = int(100.0 * (rval - self.min) / (self.max - self.min))
return sval
def sliderValue2RegisterValue(self, sval):
rval = self.min + (sval / 100.0 * (self.max - self.min))
return rval
def handle(self, value):
registerValue = self.sliderValue2RegisterValue(value)
self.label.setText(f"{self.name}: {registerValue:5.02f}")
self.win.picam2.set_controls({self.name: registerValue})
self.win.settings[self.name] = registerValue
def getLabel(self):
return self.label
def syncCamera(self):
self.handle(self.value())
class IsoWidget(QSlider):
def __init__(self, win):
QSlider.__init__(self, Qt.Horizontal)
min = int(win.picam2.camera_controls["AnalogueGain"][0] * 100.0)
max = int(win.picam2.camera_controls["AnalogueGain"][1] * 100.0)
if "ISO" not in win.settings:
win.settings["ISO"] = 100
isoValue = win.settings["ISO"]
self.label = QLabel(f"ISO:{isoValue:5d}")
self.win = win
self.setRange(min, max)
self.setTickInterval(50)
self.setSingleStep(50)
self.setValue(isoValue)
self.valueChanged.connect(self.handle)
def handle(self, value):
value = ((value + 25) // 50) * 50
self.setValue(value)
self.label.setText(f"ISO:{value:5d}")
self.win.settings["ISO"] = value
self.setValue(value)
self.syncCamera()
def syncCamera(self):
registerValue = float(self.value()) / 100.0
self.win.picam2.set_controls({"AnalogueGain": registerValue})
def getLabel(self):
return self.label
class ColorGainWidget(QSlider):
def __init__(self, win, index):
QSlider.__init__(self, Qt.Horizontal)
## min-max hard-coded because of way too much range for nothing.
# min=int(win.picam2.camera_controls["ColourGains"][0]*1000.0)
# max=int(win.picam2.camera_controls["ColourGains"][1]*1000.0)
min = 0
max = 5000
self.index = index
if index == 0:
self.color = "Red"
else:
self.color = "Blue"
self.win = win
self.setRange(min, max)
self.registerValue = self.win.settings[f"{self.color}Gain"]
self.label = QLabel(f"{self.color} Gain {self.registerValue:6.3f}")
sliderValue = int(self.registerValue * 1000.0)
self.setValue(sliderValue)
self.valueChanged.connect(self.handle)
def handle(self, value):
self.registerValue = float(value) / 1000.0
self.label.setText(f"{self.color} Gain {self.registerValue:6.3f}")
self.win.settings[f"{self.color}Gain"] = self.registerValue
self.syncCamera()
def syncCamera(self):
if self.index == 0:
values = (self.registerValue, self.win.BlueGain.registerValue)
else:
values = (self.win.RedGain.registerValue, self.registerValue)
self.win.picam2.set_controls({"ColourGains": values})
def getLabel(self):
return self.label
class WhiteBalanceModeWidget(QComboBox):
def __init__(self, win):
QComboBox.__init__(self)
self.label = QLabel("WB Mode")
self.addItems(
[
"Manual",
"Auto",
"Tungsten",
"Fluorescent",
"Indoor",
"Daylight",
"Cloudy",
]
)
self.win = win
mode = win.settings["WhiteBalanceMode"]
self.setCurrentText(mode)
self.currentTextChanged.connect(self.handle)
def handle(self, text):
if text != self.currentText():
self.setCurrentText(text)
self.syncCamera()
def syncCamera(self):
choice = self.currentText()
self.win.settings["WhiteBalanceMode"] = choice
if choice == "Manual":
self.win.picam2.set_controls({"AwbEnable": False})
self.win.Freeze.setEnabled(False)
self.win.RedGain.setEnabled(True)
self.win.BlueGain.setEnabled(True)
self.win.RedGain.syncCamera()
else:
self.win.picam2.set_controls({"AwbEnable": True})
self.win.Freeze.setEnabled(True)
self.win.RedGain.setEnabled(False)
self.win.BlueGain.setEnabled(False)
if choice == "Auto":
self.win.picam2.set_controls({"AwbMode": controls.AwbModeEnum.Auto})
elif choice == "Tungsten":
self.win.picam2.set_controls({"AwbMode": controls.AwbModeEnum.Tungsten})
elif choice == "Fluorescent":
self.win.picam2.set_controls(
{"AwbMode": controls.AwbModeEnum.Fluorescent}
)
elif choice == "Indoor":
self.win.picam2.set_controls({"AwbMode": controls.AwbModeEnum.Indoor})
elif choice == "Daylight":
self.win.picam2.set_controls({"AwbMode": controls.AwbModeEnum.Daylight})
elif choice == "Cloudy":
self.win.picam2.set_controls({"AwbMode": controls.AwbModeEnum.Cloudy})
def getLabel(self):
return self.label
class FreezeWidget(QPushButton):
def __init__(self, win):
QPushButton.__init__(self, "Freeze")
self.win = win
self.setEnabled(False) # Will be enable if WB's not Manual
self.clicked.connect(self.handle)
self.win.picam2.camWidget.done_signal.connect(self.handleMetadata)
def handle(self):
self.setEnabled(False)
self.win.picam2.capture_metadata(
signal_function=self.win.picam2.camWidget.signal_done
)
def handleMetadata(self, job):
metadata = self.win.picam2.wait(job)
newValues = metadata["ColourGains"]
self.win.RedGain.setValue(int(newValues[0] * 1000.0))
self.win.BlueGain.setValue(int(newValues[1] * 1000.0))
self.win.settings["RedGain"] = newValues[0]
self.win.settings["BlueGain"] = newValues[1]
self.win.WBMode.handle("Manual")
class FlipWidget(QCheckBox):
def __init__(self, win, which):
QCheckBox.__init__(self, which)
self.win = win
self.which = which
if which in self.win.settings:
current = self.win.settings[which]
self.setChecked(current)
else:
self.setChecked(False)
self.win.settings[which] = False
self.stateChanged.connect(self.handle)
def handle(self):
self.win.settings[self.which] = self.isChecked()
self.win.out.append("Change requires save and restart")
self.syncCamera()
def syncCamera(self):
pass
# we don't know how to do that yet.