-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmainwindow.py
More file actions
442 lines (343 loc) · 17.7 KB
/
mainwindow.py
File metadata and controls
442 lines (343 loc) · 17.7 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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
from threading import Lock
from PySide6.QtCore import QStandardPaths, QDir, QTimer, QEvent, QFileInfo, Qt, QCoreApplication
from PySide6.QtGui import QAction, QKeySequence, QCloseEvent
from PySide6.QtWidgets import QMainWindow, QMessageBox, QLabel, QApplication, QFileDialog, QToolBar
import imagingcontrol4 as ic4
from resourceselector import ResourceSelector
GOT_PHOTO_EVENT = QEvent.Type(QEvent.Type.User + 1)
DEVICE_LOST_EVENT = QEvent.Type(QEvent.Type.User + 2)
class GotPhotoEvent(QEvent):
def __init__(self, buffer: ic4.ImageBuffer):
QEvent.__init__(self, GOT_PHOTO_EVENT)
self.image_buffer = buffer
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
# Make sure the %appdata%/demoapp directory exists
appdata_directory = QStandardPaths.writableLocation(QStandardPaths.AppDataLocation)
QDir(appdata_directory).mkpath(".")
self.save_pictures_directory = QStandardPaths.writableLocation(QStandardPaths.PicturesLocation)
self.save_videos_directory = QStandardPaths.writableLocation(QStandardPaths.MoviesLocation)
self.device_file = appdata_directory + "/device.json"
self.codec_config_file = appdata_directory + "/codecconfig.json"
self.shoot_photo_mutex = Lock()
self.shoot_photo = False
self.capture_to_video = False
self.video_capture_pause = False
self.grabber = ic4.Grabber()
self.grabber.event_add_device_lost(lambda g: QApplication.postEvent(self, QEvent(DEVICE_LOST_EVENT)))
class Listener(ic4.QueueSinkListener):
def sink_connected(self, sink: ic4.QueueSink, image_type: ic4.ImageType, min_buffers_required: int) -> bool:
# Allocate more buffers than suggested, because we temporarily take some buffers
# out of circulation when saving an image or video files.
sink.alloc_and_queue_buffers(min_buffers_required + 2)
return True
def sink_disconnected(self, sink: ic4.QueueSink):
pass
def frames_queued(listener, sink: ic4.QueueSink):
buf = sink.pop_output_buffer()
# Connect the buffer's chunk data to the device's property map
# This allows for properties backed by chunk data to be updated
self.device_property_map.connect_chunkdata(buf)
with self.shoot_photo_mutex:
if self.shoot_photo:
self.shoot_photo = False
# Send an event to the main thread with a reference to
# the main thread of our GUI.
QApplication.postEvent(self, GotPhotoEvent(buf))
if self.capture_to_video and not self.video_capture_pause:
try:
self.video_writer.add_frame(buf)
except ic4.IC4Exception as ex:
pass
self.sink = ic4.QueueSink(Listener())
self.property_dialog = None
self.video_writer = ic4.VideoWriter(ic4.VideoWriterType.MP4_H264)
self.createUI()
try:
self.display = self.video_widget.as_display()
self.display.set_render_position(ic4.DisplayRenderPosition.STRETCH_CENTER)
except Exception as e:
QMessageBox.critical(self, "", f"{e}", QMessageBox.StandardButton.Ok)
if QFileInfo.exists(self.device_file):
try:
self.grabber.device_open_from_state_file(self.device_file)
self.onDeviceOpened()
except ic4.IC4Exception as e:
QMessageBox.information(self, "", f"Loading last used device failed: {e}", QMessageBox.StandardButton.Ok)
if QFileInfo.exists(self.codec_config_file):
try:
self.video_writer.property_map.deserialize_from_file(self.codec_config_file)
except ic4.IC4Exception as e:
QMessageBox.information(self, "", f"Loading last codec configuration failed: {e}", QMessageBox.StandardButton.Ok)
self.updateControls()
def createUI(self):
self.resize(1024, 768)
selector = ResourceSelector()
self.device_select_act = QAction(selector.loadIcon("images/camera.png"), "&Select", self)
self.device_select_act.setStatusTip("Select a video capture device")
self.device_select_act.setShortcut(QKeySequence.Open)
self.device_select_act.triggered.connect(self.onSelectDevice)
self.device_properties_act = QAction(selector.loadIcon("images/imgset.png"), "&Properties", self)
self.device_properties_act.setStatusTip("Show device property dialog")
self.device_properties_act.triggered.connect(self.onDeviceProperties)
self.device_driver_properties_act = QAction("&Driver Properties", self)
self.device_driver_properties_act.setStatusTip("Show device driver property dialog")
self.device_driver_properties_act.triggered.connect(self.onDeviceDriverProperties)
self.trigger_mode_act = QAction(selector.loadIcon("images/triggermode.png"), "&Trigger Mode", self)
self.trigger_mode_act.setStatusTip("Enable and disable trigger mode")
self.trigger_mode_act.setCheckable(True)
self.trigger_mode_act.triggered.connect(self.onToggleTriggerMode)
self.start_live_act = QAction(selector.loadIcon("images/livestream.png"), "&Live Stream", self)
self.start_live_act.setStatusTip("Start and stop the live stream")
self.start_live_act.setCheckable(True)
self.start_live_act.triggered.connect(self.startStopStream)
self.shoot_photo_act = QAction(selector.loadIcon("images/photo.png"), "&Shoot Photo", self)
self.shoot_photo_act.setStatusTip("Shoot and save a photo")
self.shoot_photo_act.triggered.connect(self.onShootPhoto)
self.record_start_act = QAction(selector.loadIcon("images/recordstart.png"), "&Capture Video", self)
self.record_start_act.setToolTip("Capture vidoeo into MP4 file")
self.record_start_act.setCheckable(True)
self.record_start_act.triggered.connect(self.onStartStopCaptureVideo)
self.record_pause_act = QAction(selector.loadIcon("images/recordpause.png"), "&Pause Capture Video", self)
self.record_pause_act.setStatusTip("Pause video capture")
self.record_pause_act.setCheckable(True)
self.record_pause_act.triggered.connect(self.onPauseCaptureVideo)
self.record_stop_act = QAction(selector.loadIcon("images/recordstop.png"), "&Stop Capture Video", self)
self.record_stop_act.setStatusTip("Stop video capture")
self.record_stop_act.triggered.connect(self.onStopCaptureVideo)
self.codec_property_act = QAction(selector.loadIcon("images/gear.png"), "&Codec Properties", self)
self.codec_property_act.setStatusTip("Configure the video codec")
self.codec_property_act.triggered.connect(self.onCodecProperties)
self.close_device_act = QAction("Close", self)
self.close_device_act.setStatusTip("Close the currently opened device")
self.close_device_act.setShortcuts(QKeySequence.Close)
self.close_device_act.triggered.connect(self.onCloseDevice)
exit_act = QAction("E&xit", self)
exit_act.setShortcut(QKeySequence.Quit)
exit_act.setStatusTip("Exit program")
exit_act.triggered.connect(self.close)
file_menu = self.menuBar().addMenu("&File")
file_menu.addAction(exit_act)
device_menu = self.menuBar().addMenu("&Device")
device_menu.addAction(self.device_select_act)
device_menu.addAction(self.device_properties_act)
device_menu.addAction(self.device_driver_properties_act)
device_menu.addAction(self.trigger_mode_act)
device_menu.addAction(self.start_live_act)
device_menu.addSeparator()
device_menu.addAction(self.close_device_act)
capture_menu = self.menuBar().addMenu("&Capture")
capture_menu.addAction(self.shoot_photo_act)
capture_menu.addAction(self.record_start_act)
capture_menu.addAction(self.record_pause_act)
capture_menu.addAction(self.record_stop_act)
capture_menu.addAction(self.codec_property_act)
toolbar = QToolBar(self)
self.addToolBar(Qt.TopToolBarArea, toolbar)
toolbar.addAction(self.device_select_act)
toolbar.addAction(self.device_properties_act)
toolbar.addSeparator()
toolbar.addAction(self.trigger_mode_act)
toolbar.addSeparator()
toolbar.addAction(self.start_live_act)
toolbar.addSeparator()
toolbar.addAction(self.shoot_photo_act)
toolbar.addSeparator()
toolbar.addAction(self.record_start_act)
toolbar.addAction(self.record_pause_act)
toolbar.addAction(self.record_stop_act)
toolbar.addAction(self.codec_property_act)
self.video_widget = ic4.pyside6.DisplayWidget()
self.video_widget.setMinimumSize(640, 480)
self.setCentralWidget(self.video_widget)
self.statusBar().showMessage("Ready")
self.statistics_label = QLabel("", self.statusBar())
self.statusBar().addPermanentWidget(self.statistics_label)
self.statusBar().addPermanentWidget(QLabel(" "))
self.camera_label = QLabel(self.statusBar())
self.statusBar().addPermanentWidget(self.camera_label)
self.update_statistics_timer = QTimer()
self.update_statistics_timer.timeout.connect(self.onUpdateStatisticsTimer)
self.update_statistics_timer.start()
def onCloseDevice(self):
if self.grabber.is_streaming:
self.startStopStream()
try:
self.grabber.device_close()
except:
pass
self.device_property_map = None
self.display.display_buffer(None)
self.updateControls()
def closeEvent(self, ev: QCloseEvent):
if self.grabber.is_streaming:
self.grabber.stream_stop()
if self.grabber.is_device_valid:
self.grabber.device_save_state_to_file(self.device_file)
def customEvent(self, ev: QEvent):
if ev.type() == DEVICE_LOST_EVENT:
self.onDeviceLost()
elif ev.type() == GOT_PHOTO_EVENT:
self.savePhoto(ev.image_buffer)
def onSelectDevice(self):
dlg = ic4.pyside6.DeviceSelectionDialog(self.grabber, parent=self)
if dlg.exec() == 1:
if not self.property_dialog is None:
self.property_dialog.update_grabber(self.grabber)
self.onDeviceOpened()
self.updateControls()
def onDeviceProperties(self):
if self.property_dialog is None:
self.property_dialog = ic4.pyside6.PropertyDialog(self.grabber, parent=self, title="Device Properties")
# set default vis
self.property_dialog.show()
def onDeviceDriverProperties(self):
dlg = ic4.pyside6.PropertyDialog(self.grabber.driver_property_map, parent=self, title="Device Driver Properties")
# set default vis
dlg.exec()
self.updateControls()
def onToggleTriggerMode(self):
try:
self.device_property_map.set_value(ic4.PropId.TRIGGER_MODE, self.trigger_mode_act.isChecked())
except ic4.IC4Exception as e:
QMessageBox.critical(self, "", f"{e}", QMessageBox.StandardButton.Ok)
def onShootPhoto(self):
with self.shoot_photo_mutex:
self.shoot_photo = True
def onUpdateStatisticsTimer(self):
if not self.grabber.is_device_valid:
return
try:
stats = self.grabber.stream_statistics
text = f"Frames Delivered: {stats.sink_delivered} Dropped: {stats.device_transmission_error}/{stats.device_underrun}/{stats.transform_underrun}/{stats.sink_underrun}"
self.statistics_label.setText(text)
tooltip = (
f"Frames Delivered: {stats.sink_delivered}"
f"Frames Dropped:"
f" Device Transmission Error: {stats.device_transmission_error}"
f" Device Underrun: {stats.device_underrun}"
f" Transform Underrun: {stats.transform_underrun}"
f" Sink Underrun: {stats.sink_underrun}"
)
self.statistics_label.setToolTip(tooltip)
except ic4.IC4Exception:
pass
def onDeviceLost(self):
QMessageBox.warning(self, "", f"The video capture device is lost!", QMessageBox.StandardButton.Ok)
# stop video
self.updateCameraLabel()
self.updateControls()
def onDeviceOpened(self):
self.device_property_map = self.grabber.device_property_map
trigger_mode = self.device_property_map.find(ic4.PropId.TRIGGER_MODE)
trigger_mode.event_add_notification(self.updateTriggerControl)
self.updateCameraLabel()
# if start_stream_on_open
self.startStopStream()
def updateTriggerControl(self, p: ic4.Property):
if not self.grabber.is_device_valid:
self.trigger_mode_act.setChecked(False)
self.trigger_mode_act.setEnabled(False)
else:
try:
self.trigger_mode_act.setChecked(self.device_property_map.get_value_str(ic4.PropId.TRIGGER_MODE) == "On")
self.trigger_mode_act.setEnabled(True)
except ic4.IC4Exception:
self.trigger_mode_act.setChecked(False)
self.trigger_mode_act.setEnabled(False)
def updateControls(self):
if not self.grabber.is_device_open:
self.statistics_label.clear()
self.device_properties_act.setEnabled(self.grabber.is_device_valid)
self.device_driver_properties_act.setEnabled(self.grabber.is_device_valid)
self.start_live_act.setEnabled(self.grabber.is_device_valid)
self.start_live_act.setChecked(self.grabber.is_streaming)
self.shoot_photo_act.setEnabled(self.grabber.is_streaming)
self.record_stop_act.setEnabled(self.capture_to_video)
self.record_pause_act.setChecked(self.video_capture_pause)
self.record_start_act.setChecked(self.capture_to_video)
self.close_device_act.setEnabled(self.grabber.is_device_open)
self.updateTriggerControl(None)
def updateCameraLabel(self):
try:
info = self.grabber.device_info
self.camera_label.setText(f"{info.model_name} {info.serial}")
except ic4.IC4Exception:
self.camera_label.setText("No Device")
def onPauseCaptureVideo(self):
self.video_capture_pause = self.record_pause_act.isChecked()
def onStartStopCaptureVideo(self):
if self.capture_to_video:
self.stopCapturevideo()
return
filters = [
"MP4 Video Files (*.mp4)"
]
dialog = QFileDialog(self, "Capture Video")
dialog.setNameFilters(filters)
dialog.setFileMode(QFileDialog.FileMode.AnyFile)
dialog.setAcceptMode(QFileDialog.AcceptMode.AcceptSave)
dialog.setDirectory(self.save_videos_directory)
if dialog.exec():
full_path = dialog.selectedFiles()[0]
self.save_videos_directory = QFileInfo(full_path).absolutePath()
fps = float(25)
try:
fps = self.device_property_map.get_value_float(ic4.PropId.ACQUISITION_FRAME_RATE)
except:
pass
try:
self.video_writer.begin_file(full_path, self.sink.output_image_type, fps)
except ic4.IC4Exception as e:
QMessageBox.critical(self, "", f"{e}", QMessageBox.StandardButton.Ok)
self.capture_to_video = True
self.updateControls()
def onStopCaptureVideo(self):
self.capture_to_video = False
self.video_writer.finish_file()
self.updateControls()
def onCodecProperties(self):
dlg = ic4.pyside6.PropertyDialog(self.video_writer.property_map, self, "Codec Settings")
# set default vis
if dlg.exec() == 1:
self.video_writer.property_map.serialize_to_file(self.codec_config_file)
def startStopStream(self):
try:
if self.grabber.is_device_valid:
if self.grabber.is_streaming:
self.grabber.stream_stop()
if self.capture_to_video:
self.onStopCaptureVideo()
else:
self.grabber.stream_setup(self.sink, self.display)
except ic4.IC4Exception as e:
QMessageBox.critical(self, "", f"{e}", QMessageBox.StandardButton.Ok)
self.updateControls()
def savePhoto(self, image_buffer: ic4.ImageBuffer):
filters = [
"Bitmap(*.bmp)",
"JPEG (*.jpg)",
"Portable Network Graphics (*.png)",
"TIFF (*.tif)"
]
dialog = QFileDialog(self, "Save Photo")
dialog.setNameFilters(filters)
dialog.setFileMode(QFileDialog.FileMode.AnyFile)
dialog.setAcceptMode(QFileDialog.AcceptMode.AcceptSave)
dialog.setDirectory(self.save_pictures_directory)
if dialog.exec():
selected_filter = dialog.selectedNameFilter()
full_path = dialog.selectedFiles()[0]
self.save_pictures_directory = QFileInfo(full_path).absolutePath()
try:
if selected_filter == filters[0]:
image_buffer.save_as_bmp(full_path)
elif selected_filter == filters[1]:
image_buffer.save_as_jpeg(full_path)
elif selected_filter == filters[2]:
image_buffer.save_as_png(full_path)
else:
image_buffer.save_as_tiff(full_path)
except ic4.IC4Exception as e:
QMessageBox.critical(self, "", f"{e}", QMessageBox.StandardButton.Ok)