-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.pyw
592 lines (484 loc) · 20 KB
/
main.pyw
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
# Standard library imports
import collections
import tkinter as tk
import traceback
# Third-party imports
import ctkchart
import customtkinter as ctk
from CTkColorPicker import AskColor
# Local imports
from src.app_types import Size
from src.component_loader import ComponentLoader
from src.config import (
APP_TITLE, APP_FONT, APP_FONT_KEY, APP_FONT_SIZE,
APP_GEOMETRY, APP_GEOMETRY_KEY,
LOADING_WINDOW_WIDTH, LOADING_WINDOW_HEIGHT, LOADING_WINDOW_GEOMETRY,
REFRESH_DELAY_MS,
STRABISMUS_RANGE_MIN, STRABISMUS_RANGE_MAX,
CHART_BUFFER_SIZE,
MAIN_WINDOW_POSITION_KEY,
THRESHOLD_KNOB_STEP, THRESHOLD_KNOB_STEP_PRECISE,
)
from src.main_model import MainModel
from src.overlay import OverlayWindow
from src.widgets.imageknobex import ImageKnobEx
from src.color_settings import ColorSettingsWindow
from src.app_state import AppState
from src.settings import Settings
# Configure matplotlib to use Agg backend which doesn't require GUI
import matplotlib
matplotlib.use('Agg')
class App(ctk.CTk):
"""Main application window"""
def __init__(self):
super().__init__()
self.resizable(False, False)
# Set window title
self.title(APP_TITLE)
# Set loading window dimensions
self.geometry(LOADING_WINDOW_GEOMETRY)
# Minimum window dimensions
self.minsize(LOADING_WINDOW_WIDTH, LOADING_WINDOW_HEIGHT)
# Initialize attributes
self.modules = None
self.cap = None
self.mp_face_mesh = None
self.face_detected = False
self.overlay = None
self.chart = None
self.chart_data = None
self.chart_threshold_data = None
self.chart_line = None
self.chart_threshold_line = None
self.threshold_knob = None
self.app_geometry = None
self.video_frame = None
self.video_label = None
self.threshold_inner_frame1 = None
self.threshold_inner_frame2 = None
self.threshold_entry = None
self.eye_distance_entry = None
self.model = None
# Application state initialization
self.app_state = AppState()
# Set appearance mode
ctk.set_appearance_mode("Light" if self.app_state.light_theme.get() else "Dark")
# Create component loader
self.loader = ComponentLoader(self)
# Start loading process
self.loader.start_loading(self._on_components_loaded)
def _on_components_loaded(self, modules, cap, mp_face_mesh):
"""Callback called after all components are loaded"""
self.modules = modules
self.cap = cap
self.mp_face_mesh = mp_face_mesh
self.app_state.threshold_value.trace_add("write", self._update_threshold_by_entry)
# Create main model
self.model = MainModel(self, modules, cap, mp_face_mesh, REFRESH_DELAY_MS)
# Initialize window
self._initialize_main_ui()
# Create overlay
self.overlay = OverlayWindow()
self.overlay.set_color_hex(self.app_state.overlay_color.get())
self.overlay.set_opacity(self.app_state.overlay_opacity.get())
# Create main UI components
self._create_main_ui()
# Create chart
self.chart_data = collections.deque(
[0.] * CHART_BUFFER_SIZE,
maxlen=CHART_BUFFER_SIZE
)
self.chart_threshold_data = collections.deque(
[0.] * CHART_BUFFER_SIZE,
maxlen=CHART_BUFFER_SIZE
)
# Create chart lines
self.chart_line = ctkchart.CTkLine(
master=self.chart,
fill="enabled",
)
self.chart_threshold_line = ctkchart.CTkLine(
master=self.chart,
color="red",
)
# Set threshold knob value
self.threshold_knob.set(self.app_state.threshold_value.get())
# Start processing
self.model.start()
# Start checking for results
self.check_results()
def _initialize_main_ui(self):
"""Initialize main interface after loading"""
# Set main window dimensions
self.geometry(APP_GEOMETRY)
# Get saved window position
if MAIN_WINDOW_POSITION_KEY in Settings.all():
window_position = Settings.get(MAIN_WINDOW_POSITION_KEY)
if window_position:
x, y = window_position
self.geometry(f"+{x}+{y}")
def _create_main_ui(self):
"""Create main application interface"""
is_dark_mode = ctk.get_appearance_mode().lower() == "dark"
app_font = Settings.get(APP_FONT_KEY, APP_FONT)
app_font_small = (app_font, APP_FONT_SIZE)
# Create video frame
self.video_frame = ctk.CTkFrame(self)
# Create video label
self.video_label = ctk.CTkLabel(self.video_frame, text="")
# Create threshold frame
threshold_frame = ctk.CTkFrame(self)
self.threshold_inner_frame1 = ctk.CTkFrame(
master=threshold_frame,
fg_color=ctk.ThemeManager.theme["CTkFrame"]["fg_color"][1 if is_dark_mode else 0],
)
chart_label = ctk.CTkLabel(
self.threshold_inner_frame1,
text="Eye Distance",
font=app_font_small
)
self.eye_distance_entry = ctk.CTkEntry(
self.threshold_inner_frame1,
width=60,
height=26,
border_width=0,
fg_color=ctk.ThemeManager.theme["CTk"]["fg_color"][1 if is_dark_mode else 0],
text_color="#768df1", # Default color of CTkLine
textvariable=self.app_state.eye_distance,
font=app_font_small,
justify=tk.CENTER,
state=ctk.DISABLED
)
# Create line chart widget
self.chart = ctkchart.CTkLineChart(
master=threshold_frame,
width=200,
height=60,
axis_size=0,
axis_color=ctk.ThemeManager.theme["CTk"]["fg_color"][1 if is_dark_mode else 0],
bg_color=ctk.ThemeManager.theme["CTk"]["fg_color"][1 if is_dark_mode else 0],
fg_color=ctk.ThemeManager.theme["CTk"]["fg_color"][1 if is_dark_mode else 0],
x_axis_data="",
x_axis_values=tuple([0] * CHART_BUFFER_SIZE),
x_axis_label_count=0,
x_axis_section_count=0,
x_space=0,
y_axis_data="",
y_axis_values=(0, 100), # range from 0 to 100%
y_axis_label_count=0,
y_axis_section_count=0,
y_space=0,
)
self.threshold_inner_frame2 = ctk.CTkFrame(
master=threshold_frame,
fg_color=ctk.ThemeManager.theme["CTkFrame"]["fg_color"][1 if is_dark_mode else 0],
)
threshold_label = ctk.CTkLabel(
self.threshold_inner_frame2,
text="Alert Threshold",
font=app_font_small
)
self.threshold_entry = ctk.CTkEntry(
self.threshold_inner_frame2,
width=60,
height=26,
border_width=0,
fg_color=ctk.ThemeManager.theme["CTk"]["fg_color"][1 if is_dark_mode else 0],
text_color="red",
textvariable=self.app_state.threshold_value,
font=app_font_small,
justify=tk.CENTER,
)
# Use mouse wheel to adjust Alert Threshold. Hold Ctrl for fine-tuning
# Knob control
self.threshold_knob = ImageKnobEx(
threshold_frame,
image="assets/knob4.png",
scale_image="assets/knob4_scale.png" if is_dark_mode else "assets/knob4_scale_light.png",
start=STRABISMUS_RANGE_MIN,
end=STRABISMUS_RANGE_MAX,
scroll_steps=THRESHOLD_KNOB_STEP,
scroll_steps_precise=THRESHOLD_KNOB_STEP_PRECISE,
start_angle=60, end_angle=-300,
radius=180,
text=None,
variable=self.app_state.threshold_value,
# command=self._update_threshold_by_knob
)
threshold_knob_label = ctk.CTkLabel(
threshold_frame,
text="Use mouse wheel to adjust\nAlert Threshold.\nHold Ctrl for fine-tuning",
font=app_font_small,
text_color=ctk.ThemeManager.theme["CTkButton"]["text_color_disabled"][1],
wraplength=200,
)
# Create misc frame
misc_frame = ctk.CTkTabview(self, width=200)
options_tab = misc_frame.add("Options")
more_tab = misc_frame.add("More")
self._create_options_tab(options_tab, app_font_small)
self._create_more_tab(more_tab, app_font_small, is_dark_mode)
# Pack controls
self.video_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=(5, 0), pady=5)
self.video_label.pack(expand=True, fill=tk.BOTH)
# Pack threshold controls
threshold_frame.pack(fill='x', padx=5, pady=(5, 0))
self.threshold_inner_frame1.pack(fill='x', padx=5, pady=5)
self.threshold_inner_frame1.grid_columnconfigure((0, 1), weight=1)
chart_label.grid(row=0, column=0, sticky='w')
self.eye_distance_entry.grid(row=0, column=1, sticky='e')
self.chart.pack(pady=0, padx=5, anchor="center")
self.threshold_inner_frame2.pack(fill='x', padx=5, pady=5)
self.threshold_inner_frame2.grid_columnconfigure((0, 1), weight=1)
threshold_label.grid(row=0, column=0, sticky='w')
self.threshold_entry.grid(row=0, column=1, sticky='e')
self.threshold_knob.pack(pady=(0, 5), anchor="center")
threshold_knob_label.pack(pady=(0, 10), anchor="center")
# Pack misc controls
misc_frame.pack(fill='x', padx=5, pady=5)
def _create_options_tab(self, parent, font):
# Create show camera switch
show_camera_switch = ctk.CTkSwitch(
master=parent,
text="Camera Image",
font=font,
variable=self.app_state.show_camera,
)
# Create mirror effect switch
mirror_effect_switch = ctk.CTkSwitch(
master=parent,
text="Mirror Effect",
font=font,
variable=self.app_state.mirror_effect,
)
theme_switch = ctk.CTkSwitch(
master=parent,
text="Light Theme",
font=font,
variable=self.app_state.light_theme,
command=self._on_theme_toggle
)
# Grid controls in options_tab
# parent.grid_rowconfigure(2, weight=1) # Эта строка будет растягиваться
show_camera_switch.grid(row=0, column=0, padx=5, pady=5, sticky="w")
mirror_effect_switch.grid(row=1, column=0, padx=5, pady=5, sticky="w")
theme_switch.grid(row=2, column=0, padx=5, pady=5, sticky="w")
def _create_more_tab(self, parent, font, is_dark_mode):
# Create controls container
controls_frame = ctk.CTkFrame(parent)
# controls_frame.grid_columnconfigure((0, 1), weight=1)
# controls_frame.grid_columnconfigure((0, 1, 2), weight=1)
# Create fullscreen alert frame
fullscreen_alert_switch = ctk.CTkSwitch(
master=controls_frame,
text="Fullscreen Alert",
font=font,
variable=self.app_state.fullscreen_alert,
)
# Color controls
overlay_color_label = ctk.CTkLabel(
master=controls_frame,
text="Color:",
font=font,
)
overlay_color_entry = ctk.CTkEntry(
master=controls_frame,
width=60,
height=26,
border_width=0,
fg_color=ctk.ThemeManager.theme["CTk"]["fg_color"][1 if is_dark_mode else 0],
textvariable=self.app_state.overlay_color,
font=font,
justify=tk.CENTER
)
overlay_color_button = ctk.CTkButton(
master=controls_frame,
text="...",
font=font,
width=24,
height=24,
fg_color=self.app_state.overlay_color.get(),
command=self._on_color_picker_click
)
# Opacity controls
overlay_opacity_label = ctk.CTkLabel(
master=controls_frame,
text="Opacity:",
font=font,
)
overlay_opacity_entry = ctk.CTkEntry(
master=controls_frame,
width=60,
height=26,
border_width=0,
fg_color=ctk.ThemeManager.theme["CTk"]["fg_color"][1 if is_dark_mode else 0],
textvariable=self.app_state.overlay_opacity,
font=font,
justify=tk.CENTER
)
overlay_opacity_slider = ctk.CTkSlider(
master=controls_frame,
from_=0,
to=255,
width=100, # фиксированная ширина
variable=self.app_state.overlay_opacity,
command=self._on_opacity_change
)
color_settings_btn = ctk.CTkButton(
parent,
text="Color Settings",
font=font,
command=lambda: ColorSettingsWindow(self, self.model.image_processor).focus()
)
# Pack the container
controls_frame.pack(fill="x", padx=0, pady=0)
# Grid controls
fullscreen_alert_switch.grid(row=0, column=0, columnspan=3, padx=5, pady=5, sticky="w")
overlay_color_label.grid(row=1, column=0, padx=5, pady=5, sticky="w")
overlay_color_entry.grid(row=1, column=1, padx=5, pady=5, sticky="ew")
overlay_color_button.grid(row=1, column=2, padx=5, pady=5, sticky="w")
overlay_opacity_label.grid(row=2, column=0, padx=5, pady=5, sticky="w")
overlay_opacity_entry.grid(row=2, column=1, padx=5, pady=5, sticky="ew")
overlay_opacity_slider.grid(row=2, column=2, padx=5, pady=5, sticky="ew")
# Bottom button
color_settings_btn.pack(side="bottom", padx=5, pady=5)
def update_video(self, results):
"""Update UI with processed results"""
try:
# Get required modules for display
cv = self.modules['cv2']
Image = self.modules['PIL']
mesh_results = results['mesh_results']
# Process results
self.face_detected = bool(mesh_results.multi_face_landmarks)
eye_distance_raw = results['normalized_eye_distance']
self.app_state.eye_distance.set(self.format_eye_distance(eye_distance_raw))
strabismus_detected = eye_distance_raw > results['threshold_value']
# Show/hide overlay based on strabismus detection
self.overlay.show(strabismus_detected and self.app_state.fullscreen_alert.get())
# Calculate eye distance percentage
eye_distance_percent = (
(eye_distance_raw - STRABISMUS_RANGE_MIN) /
(STRABISMUS_RANGE_MAX - STRABISMUS_RANGE_MIN)
)
# Create a line for the line chart
self.chart_data.append(100 * eye_distance_percent)
# Calculate and append threshold percentage
threshold_percent = (
(self.app_state.threshold_value.get() - STRABISMUS_RANGE_MIN) /
(STRABISMUS_RANGE_MAX - STRABISMUS_RANGE_MIN)
)
self.chart_threshold_data.append(100 * threshold_percent)
self.chart.show_data(line=self.chart_line, data=list(self.chart_data))
self.chart.show_data(line=self.chart_threshold_line, data=list(self.chart_threshold_data))
# Get processed frame
frame = results['frame']
# Get video frame dimensions
video_width = self.video_frame.winfo_width()
video_height = self.video_frame.winfo_height()
if video_width > 1 and video_height > 1: # Check if dimensions are valid
# Convert frame to PIL format
image = Image.fromarray(cv.cvtColor(frame, cv.COLOR_BGR2RGB))
# Resize image to match video frame dimensions
image = image.resize((video_width, video_height))
# Convert to CTk format
ctk_image = ctk.CTkImage(image, size=(video_width, video_height))
# Update video label
self.video_label.configure(image=ctk_image)
self.video_label.image = ctk_image
except Exception as e:
print(f"Error updating video: {e}")
traceback.print_exc()
def on_closing(self):
"""Clean up resources and handle window close"""
# Hide main window first
self.withdraw()
# Stop processing thread
if self.model:
self.model.stop()
# Close overlay window
if self.overlay:
self.overlay.close()
# Release camera
if self.cap:
self.cap.release()
# Destroy main window
self.quit()
def _on_window_configure(self, event):
"""Save new window size when it changes"""
# Check if event came from main window
if event.widget is not self:
return
# Check if size actually changed
new_geometry = Size(event.width, event.height)
if new_geometry == self.app_geometry:
return
# Update saved size
self.app_geometry = new_geometry
# Save to settings
Settings.set(APP_GEOMETRY_KEY, str(new_geometry))
def _update_threshold_by_entry(self, *args): # pylint: disable=unused-argument
"""Update threshold knob value from entry field.
Args:
*args: Variable arguments from StringVar trace callback (unused but required)
"""
if self.threshold_knob:
value = max(
STRABISMUS_RANGE_MIN,
min(
STRABISMUS_RANGE_MAX,
float(self.app_state.threshold_value.get())))
self.threshold_knob.set(value)
def _on_color_picker_click(self):
"""Handle color picker button click"""
pick_color = AskColor(initial_color=self.overlay.get_color_hex())
color = pick_color.get()
if color: # If color was selected (not cancelled)
self.overlay.set_color_hex(color)
# Update button color
self.app_state.overlay_color.set(color)
def _on_opacity_change(self, value):
"""Handle opacity slider change"""
opacity = int(float(value))
self.overlay.set_opacity(opacity)
self.overlay.show()
def _on_theme_toggle(self):
"""Handle theme change"""
is_dark_mode = not self.app_state.light_theme.get()
ctk.set_appearance_mode("Dark" if is_dark_mode else "Light")
fg_color_ctk = ctk.ThemeManager.theme["CTk"]["fg_color"][1 if is_dark_mode else 0]
fg_color_ctkframe = ctk.ThemeManager.theme["CTkFrame"]["fg_color"][1 if is_dark_mode else 0]
self.threshold_inner_frame1.configure(fg_color=fg_color_ctkframe)
self.eye_distance_entry.configure(fg_color=fg_color_ctk)
self.chart.configure(
axis_color=fg_color_ctk,
bg_color=fg_color_ctk,
fg_color=fg_color_ctk)
self.threshold_inner_frame2.configure(fg_color=fg_color_ctkframe)
self.threshold_entry.configure(fg_color=fg_color_ctk)
self.threshold_knob.configure(
scale_image="assets/knob4_scale.png" if is_dark_mode else "assets/knob4_scale_light.png",
bg=fg_color_ctkframe)
def check_results(self):
"""Check for processed results in main thread"""
try:
# Check for results and update UI
results = self.model.get_next_result()
if results is not None:
self.update_video(results)
except Exception as e:
print(f"Error checking results: {e}")
traceback.print_exc()
finally:
# Schedule next check
self.after(REFRESH_DELAY_MS, self.check_results)
@staticmethod
def format_eye_distance(eye_distance):
"""Format eye distance for display"""
if eye_distance is None:
return "N/A"
return f"{eye_distance:.3f}"
if __name__ == "__main__":
app = App()
app.protocol("WM_DELETE_WINDOW", app.on_closing)
app.mainloop()