forked from Project-Babble/ProjectBabble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera_widget.py
524 lines (488 loc) · 21.8 KB
/
camera_widget.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
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
from collections import deque
from queue import Queue, Empty
from threading import Event, Thread
import FreeSimpleGUI as sg
import cv2
import os
from babble_processor import BabbleProcessor, CamInfoOrigin
from camera import Camera, CameraState, MAX_RESOLUTION
from config import BabbleConfig
from osc import Tab
from utils.misc_utils import (
playSound,
list_camera_names,
get_camera_index_by_name,
bg_color_highlight,
bg_color_clear,
is_valid_int_input
)
from lang_manager import LocaleStringManager as lang
class CameraWidget:
def __init__(self, widget_id: Tab, main_config: BabbleConfig, osc_queue: Queue):
self.gui_camera_addr = f"-CAMERAADDR{widget_id}-"
self.gui_rotation_slider = f"-ROTATIONSLIDER{widget_id}-"
self.gui_roi_button = f"-ROIMODE{widget_id}-"
self.gui_roi_layout = f"-ROILAYOUT{widget_id}-"
self.gui_roi_selection = f"-GRAPH{widget_id}-"
self.gui_tracking_button = f"-TRACKINGMODE{widget_id}-"
self.gui_autoroi = f"-AUTOROI{widget_id}-"
self.gui_save_tracking_button = f"-SAVETRACKINGBUTTON{widget_id}-"
self.gui_tracking_layout = f"-TRACKINGLAYOUT{widget_id}-"
self.gui_tracking_image = f"-IMAGE{widget_id}-"
self.gui_tracking_fps = f"-TRACKINGFPS{widget_id}-"
self.gui_tracking_bps = f"-TRACKINGBPS{widget_id}-"
self.gui_output_graph = f"-OUTPUTGRAPH{widget_id}-"
self.gui_restart_calibration = f"-RESTARTCALIBRATION{widget_id}-"
self.gui_stop_calibration = f"-STOPCALIBRATION{widget_id}-"
self.gui_mode_readout = f"-APPMODE{widget_id}-"
self.gui_roi_message = f"-ROIMESSAGE{widget_id}-"
self.gui_vertical_flip = f"-VERTICALFLIP{widget_id}-"
self.gui_horizontal_flip = f"-HORIZONTALFLIP{widget_id}-"
self.use_calibration = f"-USECALIBRATION{widget_id}-"
self.gui_refresh_button = f"-REFRESHCAMLIST{widget_id}-"
self.osc_queue = osc_queue
self.main_config = main_config
self.cam_id = widget_id
self.settings_config = main_config.settings
self.config = main_config.cam
self.settings = main_config.settings
self.camera_list = list_camera_names()
self.maybe_image = None
if self.cam_id == Tab.CAM:
self.config = main_config.cam
else:
raise RuntimeError(
f'\033[91m[{lang._instance.get_string("log.warn")}] {lang._instance.get_string("error.improperTabValue")}\033[0m'
)
self.cancellation_event = Event()
# Set the event until start is called, otherwise we can block if shutdown is called.
self.cancellation_event.set()
self.capture_event = Event()
self.capture_queue = Queue(maxsize=2)
self.roi_queue = Queue(maxsize=2)
self.image_queue = Queue(maxsize=500) # This is needed to prevent the UI from freezing during widget changes.
self.babble_cnn = BabbleProcessor(
self.config,
self.settings_config,
self.main_config,
self.cancellation_event,
self.capture_event,
self.capture_queue,
self.image_queue,
self.cam_id,
self.osc_queue,
)
self.camera_status_queue = Queue(maxsize=2)
self.camera = Camera(
self.config,
0,
self.cancellation_event,
self.capture_event,
self.camera_status_queue,
self.capture_queue,
self.settings,
)
button_color = "#539e8a"
self.roi_layout = [
[
sg.Button(
lang._instance.get_string("camera.selectEntireFrame"),
key=self.gui_autoroi,
button_color=button_color,
tooltip=lang._instance.get_string(
"camera.selectEntireFrameTooltip"
),
),
],
[
sg.Graph(
(MAX_RESOLUTION, MAX_RESOLUTION),
(0, MAX_RESOLUTION),
(MAX_RESOLUTION, 0),
key=self.gui_roi_selection,
drag_submits=True,
enable_events=True,
background_color=bg_color_highlight,
)
],
]
# Define the window's contents
self.tracking_layout = [
[
sg.Text(
lang._instance.get_string("camera.rotation"),
background_color=bg_color_highlight,
),
sg.Slider(
range=(0, 360),
default_value=self.config.rotation_angle,
orientation="h",
key=self.gui_rotation_slider,
background_color=bg_color_highlight,
tooltip=lang._instance.get_string("camera.rotationTooltip"),
),
],
[
sg.Button(
lang._instance.get_string("camera.startCalibration"),
key=self.gui_restart_calibration,
button_color=button_color,
tooltip=lang._instance.get_string("camera.startCalibrationTooltip"),
disabled=not self.settings_config.use_calibration,
),
sg.Button(
lang._instance.get_string("camera.stopCalibration"),
key=self.gui_stop_calibration,
button_color=button_color,
tooltip=lang._instance.get_string("camera.startCalibrationTooltip"),
disabled=not self.settings_config.use_calibration,
),
],
[
sg.Checkbox(
f'{lang._instance.get_string("camera.enableCalibration")}:',
default=self.settings_config.use_calibration,
key=self.use_calibration,
background_color=bg_color_highlight,
tooltip=lang._instance.get_string(
"camera.enableCalibrationTooltip"
),
enable_events=True,
),
],
[
sg.Text(
f'{lang._instance.get_string("camera.mode")}:',
background_color=bg_color_highlight,
),
sg.Text(
lang._instance.get_string("camera.calibrating"),
key=self.gui_mode_readout,
background_color=button_color,
),
sg.Text(
"", key=self.gui_tracking_fps, background_color=bg_color_highlight
),
sg.Text(
"", key=self.gui_tracking_bps, background_color=bg_color_highlight
),
],
[
sg.Checkbox(
f'{lang._instance.get_string("camera.verticalFlip")}:',
default=self.config.gui_vertical_flip,
key=self.gui_vertical_flip,
background_color=bg_color_highlight,
tooltip=f'{lang._instance.get_string("camera.verticalFlipTooltip")}.',
),
sg.Checkbox(
f'{lang._instance.get_string("camera.horizontalFlip")}:',
default=self.config.gui_horizontal_flip,
key=self.gui_horizontal_flip,
background_color=bg_color_highlight,
tooltip=f'{lang._instance.get_string("camera.horizontalFlipTooltip")}:',
),
],
[sg.Image(filename="", key=self.gui_tracking_image)],
[
sg.Text(
f'{lang._instance.get_string("camera.crop")}:',
key=self.gui_roi_message,
background_color=bg_color_highlight,
visible=False,
),
],
]
self.widget_layout = [
[
sg.Text(
lang._instance.get_string("camera.cameraAddress"),
background_color=bg_color_highlight,
),
sg.InputCombo(
values=self.camera_list,
default_value=self.config.capture_source,
key=self.gui_camera_addr,
tooltip=lang._instance.get_string("camera.cameraAddressTooltip"),
enable_events=True,
size=(20,0),
),
sg.Button(
lang._instance.get_string("camera.refreshCameraList"),
key=self.gui_refresh_button,
button_color=button_color,
),
],
[
sg.Button(
lang._instance.get_string("camera.saveAndRestartTracking"),
key=self.gui_save_tracking_button,
button_color=button_color,
),
],
[
sg.Button(
lang._instance.get_string("camera.trackingMode"),
key=self.gui_tracking_button,
button_color=button_color,
tooltip=f'{lang._instance.get_string("camera.trackingModeTooltip")}.',
),
sg.Button(
lang._instance.get_string("camera.croppingMode"),
key=self.gui_roi_button,
button_color=button_color,
tooltip=f'{lang._instance.get_string("camera.croppingModeToolTip")}.',
),
],
[
sg.Column(
self.tracking_layout,
key=self.gui_tracking_layout,
background_color=bg_color_highlight,
),
sg.Column(
self.roi_layout,
key=self.gui_roi_layout,
background_color=bg_color_highlight,
visible=False,
),
],
]
self.x0, self.y0 = None, None
self.x1, self.y1 = None, None
self.figure = None
self.is_mouse_up = True
self.in_roi_mode = False
self.bps = 0
def _movavg_fps(self, next_fps):
# next_fps is already averaged
return f"{round(next_fps)} FPS {round((1 / next_fps if next_fps else 0) * 1000)} ms"
def _movavg_bps(self, next_bps):
self.bps = round(0.02 * next_bps + 0.98 * self.bps)
return f"{self.bps * 0.001 * 0.001 * 8:.3f} Mbps"
def started(self):
return not self.cancellation_event.is_set()
def start(self):
# If we're already running, bail
if not self.cancellation_event.is_set():
return
self.cancellation_event.clear()
self.babble_cnn_thread = Thread(target=self.babble_cnn.run)
self.babble_cnn_thread.start()
# self.babble_landmark_thread = Thread(target=self.babble_landmark.run)
# self.babble_landmark_thread.start()
self.camera_thread = Thread(target=self.camera.run)
self.camera_thread.start()
def stop(self):
# If we're not running yet, bail
if self.cancellation_event.is_set():
return
self.cancellation_event.set()
self.babble_cnn_thread.join()
# self.babble_landmark_thread.join()
self.camera_thread.join()
def render(self, window, event, values):
if self.image_queue.qsize() > 2:
with self.image_queue.mutex:
self.image_queue.queue.clear()
else:
pass
changed = False
# If anything has changed in our configuration settings, change/update those.
if (
event == self.gui_save_tracking_button
and values[self.gui_camera_addr] != self.config.capture_source
):
value = values[self.gui_camera_addr]
print(
f'\033[94m[{lang._instance.get_string("log.info")}] {lang._instance.get_string("info.newValue")}: {value}\033[0m'
)
try:
self.config.use_ffmpeg = False
# Try storing ints as ints, for those using wired cameras.
# if value not in self.camera_list:
# self.config.capture_source = value
# if "COM" not in value:
ports = ("COM", "/dev/ttyACM")
if any(x in str(value) for x in ports):
self.config.capture_source = value
else:
if is_valid_int_input(value):
self.config.capture_source = int(value)
else:
self.config.capture_source = value
except ValueError:
if value == "":
self.config.capture_source = None
else:
# If http is not in camera address, add it.
self.config.capture_source = value
if "udp" in value:
self.config.use_ffmpeg = True
elif (
"http" not in value
and ".mp4" not in value
and "udp" not in value
and "COM" not in value
and "/dev/ttyACM" not in value
and value not in self.camera_list
): # If http is not in camera address, add it.
self.config.capture_source = (
f"http://{values[self.gui_camera_addr]}/"
)
changed = True
if self.config.rotation_angle != values[self.gui_rotation_slider]:
self.config.rotation_angle = int(values[self.gui_rotation_slider])
changed = True
if self.config.gui_vertical_flip != values[self.gui_vertical_flip]:
self.config.gui_vertical_flip = values[self.gui_vertical_flip]
changed = True
if self.config.gui_horizontal_flip != values[self.gui_horizontal_flip]:
self.config.gui_horizontal_flip = values[self.gui_horizontal_flip]
changed = True
if self.settings_config.use_calibration != values[self.use_calibration]:
self.settings_config.use_calibration = values[self.use_calibration]
changed = True
if changed:
self.main_config.save()
if event == self.gui_tracking_button:
print(
f'\033[94m[{lang._instance.get_string("log.info")}] {lang._instance.get_string("info.moveToTrackingMode")}\033[0m'
)
self.in_roi_mode = False
self.camera.set_output_queue(self.capture_queue)
window[self.gui_roi_layout].update(visible=False)
window[self.gui_tracking_layout].update(visible=True)
if event == self.gui_roi_button:
print(
f'\033[94m[{lang._instance.get_string("log.info")}] {lang._instance.get_string("info.moveToROIMode")}\033[0m'
)
self.in_roi_mode = True
self.camera.set_output_queue(self.roi_queue)
window[self.gui_roi_layout].update(visible=True)
window[self.gui_tracking_layout].update(visible=False)
if event == self.use_calibration:
if self.settings_config.use_calibration == True:
window[self.gui_restart_calibration].update(disabled=False)
window[self.gui_stop_calibration].update(disabled=False)
print(f'[{lang._instance.get_string("log.info")}] {lang._instance.get_string("info.enabled")}')
else:
window[self.gui_restart_calibration].update(disabled=True)
window[self.gui_stop_calibration].update(disabled=True)
print(f'[{lang._instance.get_string("log.info")}] {lang._instance.get_string("info.disabled")}')
if event == "{}+UP".format(self.gui_roi_selection):
# Event for mouse button up in ROI mode
self.is_mouse_up = True
if self.x1 < 0:
self.x1 = 0
if self.y1 < 0:
self.y1 = 0
if abs(self.x0 - self.x1) != 0 and abs(self.y0 - self.y1) != 0:
self.config.roi_window_x = min([self.x0, self.x1])
self.config.roi_window_y = min([self.y0, self.y1])
self.config.roi_window_w = abs(self.x0 - self.x1)
self.config.roi_window_h = abs(self.y0 - self.y1)
self.main_config.save()
if event == self.gui_roi_selection:
# Event for mouse button down or mouse drag in ROI mode
if self.is_mouse_up:
self.is_mouse_up = False
self.x0, self.y0 = values[self.gui_roi_selection]
self.x1, self.y1 = values[self.gui_roi_selection]
if event == self.gui_autoroi:
print(lang._instance.get_string("info.setROI"))
if self.maybe_image is None:
# Skip rendering or use a default/placeholder image
return # Or handle appropriately
output = self.maybe_image[0].shape
self.config.roi_window_x = 0
self.config.roi_window_y = 0
self.config.roi_window_w = output[1]
self.config.roi_window_h = output[0]
self.main_config.save()
if event == self.gui_refresh_button:
print(
f'\033[94m[{lang._instance.get_string("log.info")}] {lang._instance.get_string("info.refreshedCameraList")}\033[0m'
)
self.camera_list = list_camera_names()
#print(self.camera_list)
window[self.gui_camera_addr].update(values=self.camera_list,size=(20,0))
if event == self.gui_restart_calibration:
if (
values[self.use_calibration] == True
): # Don't start recording if the calibration filter is disabled.
self.babble_cnn.calibration_frame_counter = 1500
playSound(os.path.join("Audio", "start.wav"))
if event == self.gui_stop_calibration:
if (
self.babble_cnn.calibration_frame_counter != None
): # Only assign the variable if we are in calibration mode.
self.babble_cnn.calibration_frame_counter = 0
needs_roi_set = self.config.roi_window_h <= 0 or self.config.roi_window_w <= 0
# TODO: Refactor if statements below...
window[self.gui_tracking_fps].update("")
window[self.gui_tracking_bps].update("")
if self.config.capture_source is None or self.config.capture_source == "":
window[self.gui_mode_readout].update(
lang._instance.get_string("camera.waiting")
)
window[self.gui_roi_message].update(visible=False)
elif self.camera.camera_status == CameraState.CONNECTING:
window[self.gui_mode_readout].update(
lang._instance.get_string("camera.connecting")
)
elif self.camera.camera_status == CameraState.DISCONNECTED:
window[self.gui_mode_readout].update(
lang._instance.get_string("camera.reconnecting")
)
elif needs_roi_set:
window[self.gui_mode_readout].update(
lang._instance.get_string("camera.waitingOnMouthCrop")
)
elif self.babble_cnn.calibration_frame_counter != None:
window[self.gui_mode_readout].update(
lang._instance.get_string("camera.calibration")
)
else:
window[self.gui_mode_readout].update(
lang._instance.get_string("camera.tracking")
)
window[self.gui_tracking_fps].update(self._movavg_fps(self.camera.fps))
window[self.gui_tracking_bps].update(self._movavg_bps(self.camera.bps))
if not self.settings_config.gui_disable_camera_preview: #If not hiding the image
if self.in_roi_mode:
try:
if self.roi_queue.empty():
self.capture_event.set()
maybe_image = self.roi_queue.get(block=False)
self.maybe_image = maybe_image
imgbytes = cv2.imencode(".ppm", maybe_image[0])[1].tobytes()
graph = window[self.gui_roi_selection]
if self.figure:
graph.delete_figure(self.figure)
# INCREDIBLY IMPORTANT ERASE. Drawing images does NOT overwrite the buffer, the fucking
# graph keeps every image fed in until you call this. Therefore we have to make sure we
# erase before we redraw, otherwise we'll leak memory *very* quickly.
graph.erase()
graph.draw_image(data=imgbytes, location=(0, 0))
if None not in (self.x0, self.y0, self.x1, self.y1):
self.figure = graph.draw_rectangle(
(self.x0, self.y0), (self.x1, self.y1), line_color="#6f4ca1"
)
except Empty:
pass
else:
if needs_roi_set:
window[self.gui_roi_message].update(visible=True)
return
try:
window[self.gui_roi_message].update(visible=False)
(maybe_image, cam_info) = self.image_queue.get(block=False)
imgbytes = cv2.imencode(".ppm", maybe_image)[1].tobytes()
window[self.gui_tracking_image].update(data=imgbytes)
except Empty:
pass
else: # We are hiding the previews and crop feed.
window[self.gui_roi_layout].update(visible=False)
window[self.gui_tracking_layout].update(visible=False)
self.in_roi_mode = False