-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCameraSettings.py
More file actions
executable file
·178 lines (140 loc) · 5.91 KB
/
CameraSettings.py
File metadata and controls
executable file
·178 lines (140 loc) · 5.91 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
author: Jacob Kosberg
"""
from SaveState import guisave, guirestore, guidebug
from PyQt4 import QtGui, QtCore, uic
from camera import AmscopeCamera, WebCamera
import time
class AbstractCameraSettings(QtGui.QWidget):
def __init__(self, camera, device, change_signal):
self.change_detected = change_signal
self.setWindowTitle("Camera Settings")
self.camera = camera
self.deviceId = device
self.settingsFuncs = [self.setBrightness, self.setContrast,
self.setExposure, self.setRotation, self.setGain]
self.setFixedSize(self.size())
def setDeviceName(self):
deviceName = str(self.deviceName.text())
self.deviceNameStr = deviceName if deviceName else self.deviceId
def wireUiElements(self):
self.saveButton.clicked.connect(lambda: self.save())
self.connectObjs((self.brightnessSlider, self.brightnessSpinBox), self.setBrightness)
self.connectObjs((self.contrastSlider, self.contrastSpinBox), self.setContrast)
self.connectObjs((self.exposureSlider, self.exposureSpinBox), self.setExposure)
self.connectObjs((self.gainSlider, self.gainSpinBox), self.setGain)
self.connectObjs((self.rotationSlider, self.rotationSpinBox), self.setRotation)
self.deviceName.textChanged.connect(self.setDeviceName)
self.wireSpecialUi()
def wireSpecialUi(self):
raise NotImplementedError
def connectObjs(self, objTuple, setFunction):
"""
Mutually connect two objects in a tuple so their values stay equal.
Used to wire up sliders to spin boxes for camera settings.
"""
first, second = objTuple
first.valueChanged.connect(
lambda: self.changeValue(first, second, setFunction))
second.valueChanged.connect(
lambda: self.changeValue(second, first, setFunction))
def changeValue(self, fromObj, toObj, setFunction):
toObj.setValue(fromObj.value())
try:
setFunction()
except AttributeError as e:
# FIXME shouldn't give this error on boot if connectObjs() doesn't call setFunction()
print("Capture object is empty; normal if booting.")
def setBrightness(self):
self.camera.set_brightness(self.brightnessSpinBox.value())
def setContrast(self):
self.camera.set_contrast(self.contrastSpinBox.value())
def setExposure(self):
self.camera.set_exposure(self.exposureSpinBox.value())
def setGain(self):
self.camera.set_gain(self.gainSpinBox.value())
def setRotation(self):
self.camera.set_rotation(self.rotationSpinBox.value())
def applySettings(self):
for func in self.settingsFuncs:
func()
guidebug(self)
def save(self):
guisave(self)
self.change_detected.emit()
def reset(self, waitTime):
guirestore(self)
self.wait(waitTime)
self.applySettings()
def closeEvent(self, event):
guisave(self)
event.accept()
class WebCameraSettings(AbstractCameraSettings):
def __init__(self, camera, device, change_signal):
QtGui.QWidget.__init__(self)
AbstractCameraSettings.__init__(self, camera, device, change_signal)
ui_path = "ui/parameters"
self.ui = uic.loadUi(ui_path + '.ui', self)
self.settings = QtCore.QSettings(
ui_path + '_' +str(self.deviceId) + '.ini',
QtCore.QSettings.IniFormat)
self.setDeviceName()
self.wireUiElements()
time.sleep(5)
guirestore(self)
def reset(self, waitTime):
pass
def setDeviceSerial(self):
# OpenCV does not support vendor details of VideoCapture objects
pass
def setDeviceId(self):
# No deviceId label in UI
pass
def wireSpecialUi(self):
# could implement zoom, position, etc.
pass
def wait(self, waitTime):
#time.sleep(waitTime)
pass
class AmscopeCameraSettings(AbstractCameraSettings):
def __init__(self, camera, device, change_signal):
QtGui.QWidget.__init__(self)
AbstractCameraSettings.__init__(self, camera, device, change_signal)
ui_path = "ui/amscope_parameters"
self.ui = uic.loadUi(ui_path + '.ui', self)
self.serial = self.initDeviceSerial()
self.settings = QtCore.QSettings(
ui_path + '_' +str(self.serial) + '.ini',
QtCore.QSettings.IniFormat)
self.setDeviceName()
self.wireUiElements()
guirestore(self)
def initDeviceSerial(self):
self.camera.activate()
serial = str(self.camera.get_serial())
self.camera.deactivate()
return serial
def setDeviceSerial(self):
self.serialLabel.setText(self.serial)
def setDeviceId(self):
self.deviceIdLabel.setText(str(self.deviceId))
def wait(self, waitTime):
time.sleep(waitTime)
def wireSpecialUi(self):
self.settingsFuncs.extend([self.setTempTint, self.setHue,
self.setGamma, self.setSaturation])
self.connectObjs((self.gammaSlider, self.gammaSpinBox), self.setGamma)
self.connectObjs((self.saturationSlider, self.saturationSpinBox), self.setSaturation)
self.connectObjs((self.tempSlider, self.tempSpinBox), self.setTempTint)
self.connectObjs((self.tintSlider, self.tintSpinBox), self.setTempTint)
self.connectObjs((self.hueSlider, self.hueSpinBox), self.setHue)
def setTempTint(self):
self.camera.capture.set_temperature_tint(self.tempSpinBox.value(), self.tintSpinBox.value())
def setHue(self):
self.camera.set_parameter("hue", self.hueSpinBox.value())
def setGamma(self):
self.camera.set_parameter("gamma", self.gammaSpinBox.value())
def setSaturation(self):
self.camera.set_parameter("saturation", self.saturationSpinBox.value())