-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCameraWorkbench.py
More file actions
executable file
·295 lines (243 loc) · 9.71 KB
/
Copy pathCameraWorkbench.py
File metadata and controls
executable file
·295 lines (243 loc) · 9.71 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
author: Jacob Kosberg
"""
from SaveState import guisave, guirestore
from PyQt4 import QtGui, QtCore, uic
import camera
import CameraSettings
import cv2
import time
import os
import argparse
import sys
# Used for profiling
#from pympler import tracker
# This is the time it takes to switch Amscope cameras. Used for interval
# calculation. For webcams, we set equal to 0 since we don't deactivate
# cameras. (Less risk of hitting USB bandwidth.)
CAMERA_ACTIVATION_TIME_SECONDS = 5
HOME_FOLDER = "C:\Users\europaexpts\Documents\code\CameraWorkbench"
class MainWindow(QtGui.QMainWindow):
"""MainWindow initializes UI objects like buttons, text/check/spin boxes, etc."""
def __init__(self, worker, change_signal):
QtGui.QMainWindow.__init__(self)
self.ui = uic.loadUi('ui/main.ui', self)
self.setWindowTitle("Camera Workbench")
self.setFixedSize(self.size())
self.settings = QtCore.QSettings('ui/main.ini', QtCore.QSettings.IniFormat)
guirestore(self)
self.change_detected = change_signal
self.worker = worker
self.timelapse = TimeLapse(self.worker)
#self.tr = tracker.SummaryTracker()
#self.tr.print_diff()
self.wireUiElements()
self.populateDeviceList()
self.setInitValues()
def populateDeviceList(self):
self.deviceList.clear()
for camera in self.worker.cameras:
item = QtGui.QListWidgetItem(str(camera.deviceNameStr))
if camera.camera.disabled:
item.setForeground(QtGui.QColor(255, 0, 0))
self.deviceList.addItem(item)
# Select and activate the first camera
#if i == 0:
# self.deviceList.setCurrentItem(item)
# self.switchCamera(item)
#i += 1
def update_ui(self):
self.populateDeviceList()
def setInitValues(self):
self.worker.scale = self.previewScaleSpinBox.value()
self.worker.previewEnabled = False #self.previewEnabled.isChecked()
self.worker.setImagesPath(str(self.capturePath.text()))
self.timelapse.interval = self.intervalSpinBox.value()
self.timelapse.intervalEnabled = self.intervalEnabled.isChecked()
def wireUiElements(self):
self.change_detected.connect(self.update_ui)
# Change device to selected device
self.deviceList.itemClicked.connect(self.switchCamera)
# Camera Settings/Parameters Button
self.settingsButton.clicked.connect(lambda: self.worker.camera.show())
# Preview Window
self.previewScaleSpinBox.valueChanged.connect(
lambda: self.worker.setScale(self.previewScaleSpinBox.value()))
self.previewEnabled.stateChanged.connect(
lambda: self.worker.setPreviewEnabled(self.previewEnabled.isChecked()))
# Capture path
self.capturePath.textChanged.connect(
lambda: self.worker.setImagesPath(str(self.capturePath.text())))
# Capture Image, either ALL or Selected Device
self.snapAllButton.clicked.connect(
lambda: self.worker.actionQueue.append(self.worker.captureAll))
self.snapSelectedButton.clicked.connect(
lambda: self.worker.actionQueue.append(self.worker.captureImage))
# Interval value and checkbox
self.intervalSpinBox.valueChanged.connect(
lambda: self.timelapse.setInterval(self.intervalSpinBox.value()))
self.intervalEnabled.stateChanged.connect(lambda: self.toggleTimelapse())
# Reconstruct interval photography
self.reconstructEnabled.stateChanged.connect(
lambda: self.worker.setReconstructEnabled(self.reconstructEnabled.isChecked()))
def toggleTimelapse(self):
self.timelapse.setIntervalEnabled(self.intervalEnabled.isChecked())
if self.intervalEnabled.isChecked():
self.timelapse.running = True
self.timelapse.start()
else:
self.timelapse.running = False
def switchCamera(self, item):
i = int(self.deviceList.indexFromItem(item).row())
self.worker.actionQueue.append(lambda: self.worker.switchCamera(i))
#self.tr.print_diff()
def closeEvent(self, event):
self.worker.running = False
for settings in self.worker.cameras:
settings.closeEvent(event)
guisave(self)
event.accept()
class TimeLapse(QtCore.QThread):
def __init__(self, worker):
QtCore.QThread.__init__(self)
self.worker = worker
self.running = False
def run(self):
while self.running:
self.capture()
interval = self.interval - len(self.worker.cameras)*(CAMERA_ACTIVATION_TIME_SECONDS+1)
start = time.time()
while self.running and time.time() < start + self.interval:
pass
def capture(self):
self.worker.actionQueue.append(self.worker.captureAll)
def setIntervalEnabled(self, enabled):
self.intervalEnabled = enabled
def setInterval(self, interval):
self.interval = interval
class Worker(QtCore.QThread):
"""
QT thread for activating cameras, capturing and scaling images.
self.cameras is actually a list of CameraSettings, which act as
camera managers.
"""
def __init__(self, cameras):
QtCore.QThread.__init__(self)
self.cameras = cameras
self.camera = None
self.running = True
self.scale = 60
self.previewEnabled = False
self.reconstructEnabled = False
self.actionQueue = []
def run(self):
while self.running:
self.idle()
self.kill()
def idle(self):
while self.actionQueue:
self.actionQueue.pop(0)()
self.show_frame()
def createPathIfNotExists(self, path):
if not os.path.exists(path):
os.makedirs(path)
def assertPathNotNull(self, path):
if path in [None, ""]:
raise ValueError("Path cannot be empty!")
def show_frame(self):
title = "Preview"
try:
if self.previewEnabled and self.camera.camera.capture:
self.camera.camera.show_frame(title, scale=self.scale)
else:
cv2.destroyWindow(title)
except AttributeError as e:
print(e)
print "A camera was detached!"
self.kill()
def captureAll(self):
images = []
for i in range(len(self.cameras)):
self.switchCamera(i)
images.append(self.captureImage())
if self.reconstructEnabled:
import reconstructor
outputDir = os.path.join(self.imagesPath, "reconstruction", self.getDateString())
self.createPathIfNotExists(outputDir)
reconstructor.convertPngsToJpgs(images, outputDir)
reconstructor.runCMPMVS(outputDir)
self.camera.camera.deactivate()
def captureImage(self):
cameraSettings = self.camera
print("1")
#cameraSettings.reset(CAMERA_ACTIVATION_TIME_SECONDS)
print("2")
frame = cameraSettings.camera.get_frame()
print("3")
filename = self.getImageFilepath(self.imagesPath, cameraSettings.deviceNameStr)
print("4")
cv2.imwrite(filename, frame)
print("5")
return filename
def getImageFilepath(self, path, deviceName):
"""
Creates file path under 'deviceName' folder in parent images path.
Uses date and time as filename. Ex: '2017-08-08_10-29-57.png'
"""
self.assertPathNotNull(path)
newPath = os.path.join(path, str(deviceName))
self.createPathIfNotExists(newPath)
return os.path.join(newPath, self.getDateString() + ".png")
def getDateString(self):
return time.strftime("%Y-%m-%d_%H-%M-%S")
def setPreviewEnabled(self, enabled):
self.previewEnabled = enabled
def setReconstructEnabled(self, enabled):
self.reconstructEnabled = enabled
def setImagesPath(self, path):
self.imagesPath = path
def setScale(self, scale):
self.scale = scale
def switchCamera(self, index):
if self.camera:
self.camera.camera.deactivate()
self.camera = self.cameras[index]
self.camera.camera.activate()
self.camera.reset(CAMERA_ACTIVATION_TIME_SECONDS)
self.camera.setDeviceSerial()
self.camera.setDeviceId()
def kill(self):
self.running = False
for cam in self.cameras:
cam.camera.close()
class Application(QtGui.QApplication):
change_detected = QtCore.pyqtSignal()
def __init__(self, args):
super(Application, self).__init__(["Camera Workbench"])
if args.use_amscope:
Camera = camera.AmscopeCamera
CameraManager = CameraSettings.AmscopeCameraSettings
else:
Camera = camera.WebCamera
CameraManager = CameraSettings.WebCameraSettings
cams = [CameraManager(Camera(device, fullRes=True), device, change_signal=self.change_detected)
for device in args.devices]
worker = Worker(cams)
worker.start()
mainWindow = MainWindow(worker, self.change_detected)
mainWindow.show()
def main():
parser = argparse.ArgumentParser(description="UI utility for time lapse and HDR imagery.")
parser.add_argument("devices", type=int, nargs="+", help="Device index. (0, 1, 2, ...)")
parser.add_argument('--amscope', dest='use_amscope', action='store_true')
parser.add_argument('--webcam', dest='use_amscope', action='store_false')
args = parser.parse_args()
os.chdir(HOME_FOLDER)
app = Application(args)
app.exec_()
time.sleep(2)
app.quit()
if __name__ == '__main__':
main()