-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
1359 lines (1144 loc) · 55.4 KB
/
Copy pathapp.py
File metadata and controls
1359 lines (1144 loc) · 55.4 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
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import math
import platform
import threading
import time
from collections import deque
from dataclasses import dataclass, asdict
from pathlib import Path
from typing import Any, Deque, Dict, List, Optional, Tuple
import sys
import ctypes
import os
# ============================================================================
# AUTO-PATCH FOR PYTHON 3.14+ MEDIAPIPE COMPATIBILITY
# ============================================================================
def _auto_patch_mediapipe():
"""Automatically patch MediaPipe for Python 3.14+ compatibility"""
if sys.version_info < (3, 14):
return # Not needed
try:
import mediapipe
from pathlib import Path
mediapipe_path = Path(mediapipe.__file__).parent
bindings_file = mediapipe_path / "tasks" / "python" / "core" / "mediapipe_c_bindings.py"
if not bindings_file.exists():
return
content = bindings_file.read_text(encoding='utf-8')
# Check if already patched
if "PYTHON314_PATCH_APPLIED" in content:
return
# Apply the patch
original = '_shared_lib.free.argtypes = [ctypes.c_void_p]'
if original in content:
patched = '''# PYTHON314_PATCH_APPLIED
try:
_shared_lib.free.argtypes = [ctypes.c_void_p]
_shared_lib.free.restype = None
except AttributeError:
pass # Python 3.14+ compatibility: 'free' may not be available'''
# Find and replace the full function
lines = content.split('\n')
new_lines = []
i = 0
while i < len(lines):
if '_shared_lib.free.argtypes = [ctypes.c_void_p]' in lines[i]:
# Replace this line and the next
new_lines.append(' try:')
new_lines.append(' _shared_lib.free.argtypes = [ctypes.c_void_p]')
if i + 1 < len(lines) and '_shared_lib.free.restype' in lines[i + 1]:
new_lines.append(' _shared_lib.free.restype = None')
i += 2
else:
i += 1
new_lines.append(' except AttributeError:')
new_lines.append(' pass # PYTHON314_PATCH_APPLIED: Python 3.14+ compatibility')
else:
new_lines.append(lines[i])
i += 1
new_content = '\n'.join(new_lines)
bindings_file.write_text(new_content, encoding='utf-8')
except Exception as e:
print(f"Auto-patch warning (non-fatal): {e}", file=sys.stderr)
# Apply patch on import
_auto_patch_mediapipe()
# ============================================================================
# IMPORTS
# ============================================================================
import cv2
import numpy as np
from PIL import Image, ImageTk
import urllib
try:
import pyautogui
pyautogui.FAILSAFE = False
except Exception:
pyautogui = None
import tkinter as tk
from tkinter import messagebox, ttk
# MediaPipe import
try:
import mediapipe as mp
MEDIAPIPE_AVAILABLE = True
except ImportError as e:
mp = None
MEDIAPIPE_AVAILABLE = False
print(f"Warning: MediaPipe import issue: {e}")
BASE_DIR = Path(__file__).resolve().parent
CONFIG_PATH = BASE_DIR / "config.json"
MODEL_PATH = BASE_DIR / "models" / "hand_landmarker.task"
MODEL_URL = "https://storage.googleapis.com/mediapipe-models/hand_landmarker/hand_landmarker/float16/1/hand_landmarker.task"
DEFAULT_CONFIG: Dict[str, Any] = {
"camera_index": 0,
"camera_width": 960,
"camera_height": 540,
"preview_flip": True,
"detection": {
"max_num_hands": 2,
"model_complexity": 1, # kept for compatibility with older config UI
"min_detection_confidence": 0.82,
"min_tracking_confidence": 0.78,
},
"gesture": {
"stable_frames": 3,
"cooldown_ms": 450,
"swipe_min_dx": 0.13,
"swipe_min_dy": 0.12,
"swipe_min_duration_ms": 80,
"swipe_max_duration_ms": 280,
"pinch_threshold": 0.042,
"open_palm_min_extended": 4,
},
"bindings": {
"thumb_up": {"enabled": True, "type": "key", "value": "space"},
"thumb_down": {"enabled": False, "type": "key", "value": "backspace"},
"fist": {"enabled": True, "type": "key", "value": "f"},
"open_palm": {"enabled": False, "type": "key", "value": "p"},
"swipe_up": {"enabled": True, "type": "key", "value": "up"},
"swipe_down": {"enabled": True, "type": "key", "value": "down"},
"swipe_left": {"enabled": False, "type": "key", "value": "left"},
"swipe_right": {"enabled": False, "type": "key", "value": "right"},
"pinch": {"enabled": False, "type": "key", "value": "enter"},
},
"calibration": {
"neutral_center": {"x": 0.5, "y": 0.5},
"swipe_scale": {"x": 1.0, "y": 1.0},
"recent_center_samples": [],
"recent_swipe_samples": [],
},
}
# MediaPipe Hands connections for a green skeleton overlay.
HAND_CONNECTIONS: Tuple[Tuple[int, int], ...] = (
(0, 1), (1, 2), (2, 3), (3, 4),
(0, 5), (5, 6), (6, 7), (7, 8),
(5, 9), (9, 10), (10, 11), (11, 12),
(9, 13), (13, 14), (14, 15), (15, 16),
(13, 17), (17, 18), (18, 19), (19, 20),
(0, 17),
)
def deep_merge(base: Dict[str, Any], incoming: Dict[str, Any]) -> Dict[str, Any]:
merged = dict(base)
for key, value in incoming.items():
if isinstance(value, dict) and isinstance(merged.get(key), dict):
merged[key] = deep_merge(merged[key], value)
else:
merged[key] = value
return merged
def load_config() -> Dict[str, Any]:
if CONFIG_PATH.exists():
try:
data = json.loads(CONFIG_PATH.read_text(encoding="utf-8"))
return deep_merge(DEFAULT_CONFIG, data)
except Exception:
pass
return json.loads(json.dumps(DEFAULT_CONFIG))
def save_config(cfg: Dict[str, Any]) -> None:
CONFIG_PATH.write_text(json.dumps(cfg, indent=2, ensure_ascii=False), encoding="utf-8")
def normalize_key_sequence(value: str) -> List[str]:
parts = [p.strip().lower() for p in value.replace("+", ",").split(",") if p.strip()]
aliases = {
"ctrl": "ctrl",
"control": "ctrl",
"alt": "alt",
"shift": "shift",
"cmd": "command",
"command": "command",
"win": "win",
"windows": "win",
"esc": "esc",
"escape": "esc",
"spacebar": "space",
"space": "space",
"enter": "enter",
"return": "enter",
"up": "up",
"down": "down",
"left": "left",
"right": "right",
"backspace": "backspace",
"delete": "delete",
"tab": "tab",
"pageup": "pageup",
"pagedown": "pagedown",
}
return [aliases.get(p, p) for p in parts]
def execute_binding(binding: Dict[str, Any]) -> str:
if pyautogui is None:
raise RuntimeError("pyautogui is not installed")
action_type = str(binding.get("type", "key")).lower()
raw_value = str(binding.get("value", "")).strip()
if action_type == "key":
if not raw_value:
raise ValueError("Key action needs a key value")
pyautogui.press(raw_value.lower())
return f"pressed {raw_value}"
if action_type == "hotkey":
keys = normalize_key_sequence(raw_value)
if len(keys) < 2:
raise ValueError("Hotkey needs at least two keys")
pyautogui.hotkey(*keys)
return f"hotkey {keys}"
if action_type == "scroll":
amount = int(raw_value or "0")
if amount == 0:
raise ValueError("Scroll needs a non-zero amount")
pyautogui.scroll(amount)
return f"scrolled {amount}"
raise ValueError(f"Unsupported action type: {action_type}")
def clamp(value: float, low: float, high: float) -> float:
return max(low, min(high, value))
def point_distance(a: Dict[str, float], b: Dict[str, float]) -> float:
return math.hypot(a["x"] - b["x"], a["y"] - b["y"])
def landmarks_center(landmarks) -> Dict[str, float]:
x = sum(p.x for p in landmarks) / len(landmarks)
y = sum(p.y for p in landmarks) / len(landmarks)
return {"x": float(x), "y": float(y)}
def finger_extended(landmarks, tip: int, pip: int) -> bool:
return landmarks[tip].y < landmarks[pip].y
def thumb_extended(landmarks, handedness: str) -> bool:
tip = landmarks[4]
ip = landmarks[3]
wrist = landmarks[0]
index_mcp = landmarks[5]
# If handedness is unstable, the secondary vertical checks still help.
if handedness.lower() == "right":
horizontal = tip.x < ip.x - 0.012
else:
horizontal = tip.x > ip.x + 0.012
spread = abs(tip.y - wrist.y) < abs(index_mcp.y - wrist.y) + 0.18
return horizontal and spread
def classify_gesture(landmarks, handedness: str, cfg: Dict[str, Any]) -> Optional[str]:
gesture_cfg = cfg["gesture"]
pinch_threshold = float(gesture_cfg["pinch_threshold"])
fingers = {
"thumb": thumb_extended(landmarks, handedness),
"index": finger_extended(landmarks, 8, 6),
"middle": finger_extended(landmarks, 12, 10),
"ring": finger_extended(landmarks, 16, 14),
"pinky": finger_extended(landmarks, 20, 18),
}
extended_count = sum(1 for v in fingers.values() if v)
pinch_distance = point_distance(
{"x": landmarks[4].x, "y": landmarks[4].y},
{"x": landmarks[8].x, "y": landmarks[8].y},
)
wrist = landmarks[0]
thumb_tip = landmarks[4]
thumb_ip = landmarks[3]
thumb_mcp = landmarks[2]
if pinch_distance <= pinch_threshold and extended_count <= 2:
return "pinch"
if extended_count <= 1:
return "fist"
if fingers["thumb"] and not fingers["index"] and not fingers["middle"] and not fingers["ring"] and not fingers["pinky"]:
upward = thumb_tip.y < thumb_ip.y and thumb_tip.y < thumb_mcp.y - 0.004
downward = thumb_tip.y > thumb_ip.y and thumb_tip.y > thumb_mcp.y + 0.004
if upward:
return "thumb_up"
if downward:
return "thumb_down"
if extended_count >= int(gesture_cfg["open_palm_min_extended"]):
tip_lift = (
(wrist.y - landmarks[8].y)
+ (wrist.y - landmarks[12].y)
+ (wrist.y - landmarks[16].y)
+ (wrist.y - landmarks[20].y)
) / 4.0
if tip_lift > 0.10:
return "open_palm"
return None
def draw_text_box(frame, text: str, x: int, y: int) -> None:
font = cv2.FONT_HERSHEY_SIMPLEX
scale = 0.6
thickness = 1
(tw, th), baseline = cv2.getTextSize(text, font, scale, thickness)
pad_x, pad_y = 10, 8
x = max(8, min(x, frame.shape[1] - tw - pad_x * 2 - 8))
y = max(30, min(y, frame.shape[0] - 8))
cv2.rectangle(frame, (x, y - th - pad_y * 2), (x + tw + pad_x * 2, y + baseline + pad_y), (12, 20, 16), -1)
cv2.rectangle(frame, (x, y - th - pad_y * 2), (x + tw + pad_x * 2, y + baseline + pad_y), (40, 140, 70), 1)
cv2.putText(frame, text, (x + pad_x, y - pad_y), font, scale, (230, 255, 235), thickness, cv2.LINE_AA)
def _landmark_to_px(landmark, frame) -> Tuple[int, int]:
return int(landmark.x * frame.shape[1]), int(landmark.y * frame.shape[0])
def draw_hand_skeleton(frame: np.ndarray, landmarks) -> None:
points = [_landmark_to_px(p, frame) for p in landmarks]
# connections
for a, b in HAND_CONNECTIONS:
cv2.line(frame, points[a], points[b], (34, 197, 94), 3, cv2.LINE_AA)
# joints
for idx, pt in enumerate(points):
radius = 5 if idx == 0 else 4
cv2.circle(frame, pt, radius, (134, 239, 172), -1, cv2.LINE_AA)
cv2.circle(frame, pt, max(1, radius - 2), (16, 64, 35), -1, cv2.LINE_AA)
def get_category_label(cat: Any) -> str:
for attr in ("category_name", "display_name", "name"):
value = getattr(cat, attr, None)
if value:
return str(value)
return str(cat)
def ensure_model_downloaded() -> Path:
MODEL_PATH.parent.mkdir(parents=True, exist_ok=True)
if MODEL_PATH.exists() and MODEL_PATH.stat().st_size > 0:
return MODEL_PATH
tmp_path = MODEL_PATH.with_suffix(".task.download")
try:
urllib.request.urlretrieve(MODEL_URL, tmp_path)
tmp_path.replace(MODEL_PATH)
except Exception as exc:
if tmp_path.exists():
try:
tmp_path.unlink()
except Exception:
pass
raise RuntimeError(
"Could not download the MediaPipe hand model. "
"Check your internet connection or place hand_landmarker.task in the models folder."
) from exc
return MODEL_PATH
@dataclass
class HandSample:
t: float
x: float
y: float
class ScrollableFrame(ttk.Frame):
def __init__(self, master, *args, **kwargs):
super().__init__(master, *args, **kwargs)
canvas = tk.Canvas(self, highlightthickness=0, bg="#0b1220")
scrollbar = ttk.Scrollbar(self, orient="vertical", command=canvas.yview)
self.inner = ttk.Frame(canvas)
self.inner.bind(
"<Configure>",
lambda e: canvas.configure(scrollregion=canvas.bbox("all")),
)
window = canvas.create_window((0, 0), window=self.inner, anchor="nw")
def _on_canvas_configure(event):
canvas.itemconfigure(window, width=event.width)
canvas.bind("<Configure>", _on_canvas_configure)
canvas.configure(yscrollcommand=scrollbar.set)
canvas.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")
self.canvas = canvas
class GestureApp(tk.Tk):
def __init__(self):
super().__init__()
self.title("InScroll — Desktop Gesture Control")
self.geometry("1450x920")
self.minsize(1280, 820)
self.configure(bg="#08111e")
self.cfg = load_config()
self.preview_running = False
self.stop_event = threading.Event()
self.capture_thread: Optional[threading.Thread] = None
self.video_capture: Optional[cv2.VideoCapture] = None
self.hand_landmarker = None
self.latest_frame_lock = threading.Lock()
self.latest_pil_image: Optional[Image.Image] = None
self.latest_status: Dict[str, Any] = {}
self.current_fps = 0.0
self.events: List[Dict[str, Any]] = []
self.hand_histories: Dict[str, Deque[HandSample]] = {}
self.stable_gesture: Dict[str, Optional[str]] = {}
self.stable_count: Dict[str, int] = {}
self.last_trigger_at: Dict[str, float] = {}
self.frame_ts_ms = 0
self.calibration_state = {
"capture_center": False,
"capture_swipe": False,
"swipe_samples": [],
"center_samples": [],
"countdown": 0.0,
}
self._build_styles()
self._build_layout()
self._load_ui_from_config()
self._schedule_ui_updates()
def _build_styles(self):
style = ttk.Style(self)
if "clam" in style.theme_names():
style.theme_use("clam")
style.configure("TFrame", background="#08111e")
style.configure("Card.TFrame", background="#0d1726", relief="flat")
style.configure("TLabel", background="#08111e", foreground="#e5eefc")
style.configure("Muted.TLabel", background="#08111e", foreground="#8da2bf")
style.configure("Card.TLabel", background="#0d1726", foreground="#e5eefc")
style.configure("Title.TLabel", background="#08111e", foreground="#f5f8ff", font=("Segoe UI", 24, "bold"))
style.configure("Subtitle.TLabel", background="#08111e", foreground="#8da2bf", font=("Segoe UI", 10))
style.configure("Tab.TNotebook", background="#08111e", borderwidth=0)
style.configure("Tab.TNotebook.Tab", padding=(18, 10), background="#0d1726", foreground="#d8e6f8")
style.map("Tab.TNotebook.Tab", background=[("selected", "#1a2740")], foreground=[("selected", "#ffffff")])
style.configure("TButton", padding=(12, 9), background="#1a2740", foreground="#ffffff", borderwidth=0)
style.map("TButton", background=[("active", "#23365a")])
style.configure("Accent.TButton", padding=(12, 10), background="#19c37d", foreground="#07111f", font=("Segoe UI", 10, "bold"))
style.map("Accent.TButton", background=[("active", "#30d48c")])
style.configure("Danger.TButton", padding=(12, 10), background="#4b2230", foreground="#ffdce6")
style.map("Danger.TButton", background=[("active", "#652c40")])
style.configure("TLabelframe", background="#0d1726", foreground="#e5eefc")
style.configure("TLabelframe.Label", background="#0d1726", foreground="#a9b9d1")
style.configure("TCheckbutton", background="#0d1726", foreground="#e5eefc")
style.configure("TRadiobutton", background="#08111e", foreground="#e5eefc")
style.configure("TEntry", fieldbackground="#111c2e", foreground="#e5eefc", insertcolor="#ffffff")
style.configure("TCombobox", fieldbackground="#111c2e", foreground="#e5eefc")
style.configure("Horizontal.TScale", background="#0d1726")
def _build_layout(self):
top = ttk.Frame(self)
top.pack(fill="x", padx=18, pady=(16, 10))
title_wrap = ttk.Frame(top)
title_wrap.pack(side="left", fill="x", expand=True)
ttk.Label(title_wrap, text="InScroll", style="Title.TLabel").pack(anchor="w")
ttk.Label(
title_wrap,
text="Desktop hand gesture control with MediaPipe Tasks, live preview, configuration, and calibration.",
style="Subtitle.TLabel",
).pack(anchor="w", pady=(3, 0))
self.status_pill = tk.Label(
top,
text="Camera idle",
bg="#101a2c",
fg="#d9e8ff",
padx=14,
pady=8,
font=("Segoe UI", 10, "bold"),
bd=0,
relief="flat",
)
self.status_pill.pack(side="right")
self.notebook = ttk.Notebook(self, style="Tab.TNotebook")
self.notebook.pack(fill="both", expand=True, padx=18, pady=(0, 18))
self.preview_tab = ttk.Frame(self.notebook)
self.config_tab = ttk.Frame(self.notebook)
self.calib_tab = ttk.Frame(self.notebook)
self.notebook.add(self.preview_tab, text="Preview")
self.notebook.add(self.config_tab, text="Configurations")
self.notebook.add(self.calib_tab, text="Calibration")
self._build_preview_tab()
self._build_config_tab()
self._build_calibration_tab()
def _build_preview_tab(self):
outer = ttk.Frame(self.preview_tab)
outer.pack(fill="both", expand=True, padx=2, pady=2)
outer.columnconfigure(0, weight=3)
outer.columnconfigure(1, weight=1)
outer.rowconfigure(0, weight=1)
left = ttk.Frame(outer, style="Card.TFrame", padding=14)
left.grid(row=0, column=0, sticky="nsew", padx=(0, 12))
right = ttk.Frame(outer, style="Card.TFrame", padding=14)
right.grid(row=0, column=1, sticky="nsew")
stage_wrap = ttk.Frame(left, style="Card.TFrame")
stage_wrap.pack(fill="both", expand=True)
stage_header = ttk.Frame(stage_wrap, style="Card.TFrame")
stage_header.pack(fill="x", pady=(0, 10))
ttk.Label(stage_header, text="Live Preview", style="Card.TLabel", font=("Segoe UI", 15, "bold")).pack(side="left")
ttk.Label(stage_header, text="green skeleton overlay + mirrored view", style="Muted.TLabel").pack(side="right")
self.preview_canvas = tk.Canvas(stage_wrap, bg="#02070c", highlightthickness=1, highlightbackground="#1f2b44")
self.preview_canvas.pack(fill="both", expand=True)
action_bar = ttk.Frame(left, style="Card.TFrame")
action_bar.pack(fill="x", pady=(12, 0))
self.start_btn = ttk.Button(action_bar, text="Start Camera", style="Accent.TButton", command=self.toggle_camera)
self.start_btn.pack(side="left")
ttk.Button(action_bar, text="Stop", command=self.stop_camera).pack(side="left", padx=(10, 0))
ttk.Button(action_bar, text="Save Config", command=self.save_config).pack(side="left", padx=(10, 0))
ttk.Button(action_bar, text="Reload Config", command=self.reload_config).pack(side="left", padx=(10, 0))
stat_card = ttk.LabelFrame(right, text="Realtime Stats")
stat_card.pack(fill="x")
self.lbl_hand_count = ttk.Label(stat_card, text="Hands: 0", style="Card.TLabel", font=("Segoe UI", 11, "bold"))
self.lbl_hand_count.pack(anchor="w", pady=(6, 2), padx=10)
self.lbl_last_gesture = ttk.Label(stat_card, text="Last gesture: —", style="Card.TLabel")
self.lbl_last_gesture.pack(anchor="w", pady=2, padx=10)
self.lbl_track_status = ttk.Label(stat_card, text="Status: idle", style="Card.TLabel")
self.lbl_track_status.pack(anchor="w", pady=2, padx=10)
self.lbl_fps = ttk.Label(stat_card, text="FPS: 0", style="Card.TLabel")
self.lbl_fps.pack(anchor="w", pady=(2, 10), padx=10)
guide_card = ttk.LabelFrame(right, text="Use Notes")
guide_card.pack(fill="both", expand=True, pady=(12, 0))
notes = [
"Face the laptop camera directly.",
"Keep your palm inside the center guide during calibration.",
"Swipe fast and clean for swipe gestures.",
"The preview stays mirrored so it feels natural on a laptop camera.",
]
for note in notes:
ttk.Label(guide_card, text="• " + note, style="Card.TLabel", wraplength=300, justify="left").pack(anchor="w", padx=10, pady=4)
self.events_card = ttk.LabelFrame(right, text="Event Log")
self.events_card.pack(fill="both", expand=True, pady=(12, 0))
self.events_text = tk.Text(
self.events_card,
height=10,
bg="#0a1322",
fg="#dce9fb",
insertbackground="#ffffff",
bd=0,
highlightthickness=0,
wrap="word",
)
self.events_text.pack(fill="both", expand=True, padx=8, pady=8)
self.events_text.configure(state="disabled")
def _build_config_tab(self):
outer = ttk.Frame(self.config_tab)
outer.pack(fill="both", expand=True, padx=2, pady=2)
outer.columnconfigure(0, weight=1)
outer.columnconfigure(1, weight=1)
outer.rowconfigure(0, weight=1)
left = ttk.Frame(outer, style="Card.TFrame", padding=16)
left.grid(row=0, column=0, sticky="nsew", padx=(0, 12))
right = ttk.Frame(outer, style="Card.TFrame", padding=16)
right.grid(row=0, column=1, sticky="nsew")
camera_box = ttk.LabelFrame(left, text="Camera + Detection")
camera_box.pack(fill="x")
self.var_cam_index = tk.StringVar()
self.var_cam_width = tk.StringVar()
self.var_cam_height = tk.StringVar()
self.var_preview_flip = tk.BooleanVar()
self.var_max_hands = tk.StringVar()
self.var_model_complexity = tk.StringVar()
self.var_min_det = tk.DoubleVar()
self.var_min_track = tk.DoubleVar()
self._add_labeled_entry(camera_box, "Camera index", self.var_cam_index)
self._add_labeled_entry(camera_box, "Frame width", self.var_cam_width)
self._add_labeled_entry(camera_box, "Frame height", self.var_cam_height)
flip_row = ttk.Frame(camera_box)
flip_row.pack(fill="x", padx=10, pady=6)
ttk.Checkbutton(flip_row, text="Mirror preview", variable=self.var_preview_flip).pack(anchor="w")
self._add_labeled_entry(camera_box, "Max hands", self.var_max_hands)
self._add_labeled_entry(camera_box, "Model complexity", self.var_model_complexity)
self._add_labeled_scale(camera_box, "Detection confidence", self.var_min_det, 0.50, 0.95)
self._add_labeled_scale(camera_box, "Tracking confidence", self.var_min_track, 0.50, 0.95)
gesture_box = ttk.LabelFrame(left, text="Gesture Engine")
gesture_box.pack(fill="x", pady=(12, 0))
self.var_stable_frames = tk.StringVar()
self.var_cooldown_ms = tk.StringVar()
self.var_swipe_dx = tk.DoubleVar()
self.var_swipe_dy = tk.DoubleVar()
self.var_swipe_min_ms = tk.StringVar()
self.var_swipe_max_ms = tk.StringVar()
self.var_pinch_threshold = tk.DoubleVar()
self.var_open_palm_min = tk.StringVar()
self._add_labeled_entry(gesture_box, "Stable frames", self.var_stable_frames)
self._add_labeled_entry(gesture_box, "Gesture cooldown ms", self.var_cooldown_ms)
self._add_labeled_scale(gesture_box, "Swipe min dx", self.var_swipe_dx, 0.05, 0.30)
self._add_labeled_scale(gesture_box, "Swipe min dy", self.var_swipe_dy, 0.05, 0.30)
self._add_labeled_entry(gesture_box, "Swipe min duration ms", self.var_swipe_min_ms)
self._add_labeled_entry(gesture_box, "Swipe max duration ms", self.var_swipe_max_ms)
self._add_labeled_scale(gesture_box, "Pinch threshold", self.var_pinch_threshold, 0.02, 0.08)
self._add_labeled_entry(gesture_box, "Open palm min fingers", self.var_open_palm_min)
btns = ttk.Frame(left)
btns.pack(fill="x", pady=(12, 0))
ttk.Button(btns, text="Save Config", style="Accent.TButton", command=self.save_config).pack(side="left")
ttk.Button(btns, text="Apply from File", command=self.reload_config).pack(side="left", padx=(10, 0))
ttk.Button(btns, text="Restore Defaults", style="Danger.TButton", command=self.restore_defaults).pack(side="left", padx=(10, 0))
bindings_box = ttk.LabelFrame(right, text="Gesture Bindings")
bindings_box.pack(fill="both", expand=True)
self.bindings_scroll = ScrollableFrame(bindings_box)
self.bindings_scroll.pack(fill="both", expand=True, padx=6, pady=6)
self.binding_rows: Dict[str, Dict[str, Any]] = {}
self._build_binding_rows(self.bindings_scroll.inner)
def _build_binding_rows(self, parent):
gestures = [
("thumb_up", "Thumb Up"),
("thumb_down", "Thumb Down"),
("fist", "Fist"),
("open_palm", "Open Palm"),
("swipe_up", "Swipe Up"),
("swipe_down", "Swipe Down"),
("swipe_left", "Swipe Left"),
("swipe_right", "Swipe Right"),
("pinch", "Pinch"),
]
for idx, (key, label) in enumerate(gestures):
card = ttk.Frame(parent, style="Card.TFrame", padding=10)
card.grid(row=idx, column=0, sticky="ew", padx=4, pady=5)
parent.grid_columnconfigure(0, weight=1)
var_enabled = tk.BooleanVar()
var_type = tk.StringVar()
var_value = tk.StringVar()
ttk.Label(card, text=label, style="Card.TLabel", font=("Segoe UI", 10, "bold")).grid(row=0, column=0, sticky="w")
ttk.Checkbutton(card, text="Enabled", variable=var_enabled).grid(row=0, column=1, sticky="e")
ttk.Label(card, text="Action type", style="Card.TLabel").grid(row=1, column=0, sticky="w", pady=(8, 2))
combo = ttk.Combobox(card, textvariable=var_type, values=["key", "hotkey", "scroll"], state="readonly", width=12)
combo.grid(row=1, column=1, sticky="ew", pady=(8, 2))
ttk.Label(card, text="Value", style="Card.TLabel").grid(row=2, column=0, sticky="w", pady=(8, 2))
entry = ttk.Entry(card, textvariable=var_value)
entry.grid(row=2, column=1, sticky="ew", pady=(8, 2))
card.grid_columnconfigure(1, weight=1)
self.binding_rows[key] = {
"enabled": var_enabled,
"type": var_type,
"value": var_value,
}
def _add_labeled_entry(self, parent, label: str, variable: tk.Variable):
row = ttk.Frame(parent)
row.pack(fill="x", padx=10, pady=4)
ttk.Label(row, text=label, style="Card.TLabel").pack(anchor="w")
ttk.Entry(row, textvariable=variable).pack(fill="x", pady=(3, 0))
def _add_labeled_scale(self, parent, label: str, variable: tk.DoubleVar, low: float, high: float):
row = ttk.Frame(parent)
row.pack(fill="x", padx=10, pady=4)
top = ttk.Frame(row)
top.pack(fill="x")
ttk.Label(top, text=label, style="Card.TLabel").pack(side="left")
ttk.Label(top, textvariable=variable, style="Muted.TLabel").pack(side="right")
scale = ttk.Scale(row, from_=low, to=high, variable=variable)
scale.pack(fill="x", pady=(4, 0))
def _build_calibration_tab(self):
outer = ttk.Frame(self.calib_tab)
outer.pack(fill="both", expand=True, padx=2, pady=2)
outer.columnconfigure(0, weight=1)
outer.columnconfigure(1, weight=1)
left = ttk.Frame(outer, style="Card.TFrame", padding=16)
left.grid(row=0, column=0, sticky="nsew", padx=(0, 12))
right = ttk.Frame(outer, style="Card.TFrame", padding=16)
right.grid(row=0, column=1, sticky="nsew")
instr = ttk.LabelFrame(left, text="Calibration Workflow")
instr.pack(fill="x")
lines = [
"1. Place your hand at the center of the guide box.",
"2. Click Capture Center while holding a neutral pose.",
"3. Then perform one fast swipe and click Calibrate Swipe.",
"4. Save config to keep the tuned thresholds.",
]
for line in lines:
ttk.Label(instr, text="• " + line, style="Card.TLabel", wraplength=520, justify="left").pack(anchor="w", padx=10, pady=4)
status_box = ttk.LabelFrame(left, text="Calibration State")
status_box.pack(fill="x", pady=(12, 0))
self.calib_status = ttk.Label(status_box, text="Ready", style="Card.TLabel")
self.calib_status.pack(anchor="w", padx=10, pady=(8, 4))
self.center_status = ttk.Label(status_box, text="Neutral center: not captured", style="Card.TLabel")
self.center_status.pack(anchor="w", padx=10, pady=4)
self.swipe_status = ttk.Label(status_box, text="Swipe scale: default", style="Card.TLabel")
self.swipe_status.pack(anchor="w", padx=10, pady=(4, 10))
action = ttk.Frame(left)
action.pack(fill="x", pady=(12, 0))
ttk.Button(action, text="Capture Center", style="Accent.TButton", command=self.capture_center_calibration).pack(side="left")
ttk.Button(action, text="Calibrate Swipe", command=self.capture_swipe_calibration).pack(side="left", padx=(10, 0))
ttk.Button(action, text="Reset Calibration", style="Danger.TButton", command=self.reset_calibration).pack(side="left", padx=(10, 0))
tuning_box = ttk.LabelFrame(right, text="Calibration Values")
tuning_box.pack(fill="x")
self.calib_values = tk.Text(
tuning_box,
height=14,
bg="#0a1322",
fg="#dce9fb",
insertbackground="#ffffff",
bd=0,
highlightthickness=0,
wrap="word",
)
self.calib_values.pack(fill="x", padx=8, pady=8)
live_box = ttk.LabelFrame(right, text="Live Calibration Feedback")
live_box.pack(fill="both", expand=True, pady=(12, 0))
self.live_feedback = tk.Text(
live_box,
height=10,
bg="#0a1322",
fg="#dce9fb",
insertbackground="#ffffff",
bd=0,
highlightthickness=0,
wrap="word",
)
self.live_feedback.pack(fill="both", expand=True, padx=8, pady=8)
def _load_ui_from_config(self):
self.var_cam_index.set(str(self.cfg.get("camera_index", 0)))
self.var_cam_width.set(str(self.cfg.get("camera_width", 960)))
self.var_cam_height.set(str(self.cfg.get("camera_height", 540)))
self.var_preview_flip.set(bool(self.cfg.get("preview_flip", True)))
det = self.cfg.get("detection", {})
gest = self.cfg.get("gesture", {})
self.var_max_hands.set(str(det.get("max_num_hands", 2)))
self.var_model_complexity.set(str(det.get("model_complexity", 1)))
self.var_min_det.set(float(det.get("min_detection_confidence", 0.82)))
self.var_min_track.set(float(det.get("min_tracking_confidence", 0.78)))
self.var_stable_frames.set(str(gest.get("stable_frames", 3)))
self.var_cooldown_ms.set(str(gest.get("cooldown_ms", 450)))
self.var_swipe_dx.set(float(gest.get("swipe_min_dx", 0.13)))
self.var_swipe_dy.set(float(gest.get("swipe_min_dy", 0.12)))
self.var_swipe_min_ms.set(str(gest.get("swipe_min_duration_ms", 80)))
self.var_swipe_max_ms.set(str(gest.get("swipe_max_duration_ms", 280)))
self.var_pinch_threshold.set(float(gest.get("pinch_threshold", 0.042)))
self.var_open_palm_min.set(str(gest.get("open_palm_min_extended", 4)))
for key, row in self.binding_rows.items():
b = self.cfg["bindings"].get(key, {})
row["enabled"].set(bool(b.get("enabled", False)))
row["type"].set(str(b.get("type", "key")))
row["value"].set(str(b.get("value", "")))
self._refresh_calibration_texts()
def _gather_ui_to_config(self) -> Dict[str, Any]:
cfg = json.loads(json.dumps(self.cfg))
cfg["camera_index"] = int(self.var_cam_index.get() or 0)
cfg["camera_width"] = int(self.var_cam_width.get() or 960)
cfg["camera_height"] = int(self.var_cam_height.get() or 540)
cfg["preview_flip"] = bool(self.var_preview_flip.get())
cfg["detection"] = {
"max_num_hands": int(self.var_max_hands.get() or 2),
"model_complexity": int(self.var_model_complexity.get() or 1),
"min_detection_confidence": float(self.var_min_det.get()),
"min_tracking_confidence": float(self.var_min_track.get()),
}
cfg["gesture"] = {
"stable_frames": max(1, int(self.var_stable_frames.get() or 3)),
"cooldown_ms": max(0, int(self.var_cooldown_ms.get() or 450)),
"swipe_min_dx": float(self.var_swipe_dx.get()),
"swipe_min_dy": float(self.var_swipe_dy.get()),
"swipe_min_duration_ms": max(40, int(self.var_swipe_min_ms.get() or 80)),
"swipe_max_duration_ms": max(100, int(self.var_swipe_max_ms.get() or 280)),
"pinch_threshold": float(self.var_pinch_threshold.get()),
"open_palm_min_extended": max(3, int(self.var_open_palm_min.get() or 4)),
}
new_bindings = {}
for key, row in self.binding_rows.items():
new_bindings[key] = {
"enabled": bool(row["enabled"].get()),
"type": row["type"].get() or "key",
"value": row["value"].get() or "",
}
cfg["bindings"] = new_bindings
return cfg
def _refresh_calibration_texts(self):
calib = self.cfg.get("calibration", {})
neutral = calib.get("neutral_center", {"x": 0.5, "y": 0.5})
swipe = calib.get("swipe_scale", {"x": 1.0, "y": 1.0})
self.center_status.configure(text=f"Neutral center: x={neutral.get('x', 0.5):.3f}, y={neutral.get('y', 0.5):.3f}")
self.swipe_status.configure(text=f"Swipe scale: x={swipe.get('x', 1.0):.2f}, y={swipe.get('y', 1.0):.2f}")
text = [
"Current tuning:",
f"- Camera size: {self.cfg.get('camera_width', 960)} x {self.cfg.get('camera_height', 540)}",
f"- Detection confidence: {self.cfg['detection']['min_detection_confidence']:.2f}",
f"- Tracking confidence: {self.cfg['detection']['min_tracking_confidence']:.2f}",
f"- Stable frames: {self.cfg['gesture']['stable_frames']}",
f"- Cooldown ms: {self.cfg['gesture']['cooldown_ms']}",
f"- Swipe threshold dx: {self.cfg['gesture']['swipe_min_dx']:.3f}",
f"- Swipe threshold dy: {self.cfg['gesture']['swipe_min_dy']:.3f}",
f"- Pinch threshold: {self.cfg['gesture']['pinch_threshold']:.3f}",
]
self.calib_values.delete("1.0", "end")
self.calib_values.insert("1.0", "\n".join(text))
def _set_status(self, text: str):
self.status_pill.configure(text=text)
self.latest_status["ui_status"] = text
def toggle_camera(self):
if self.preview_running:
self.stop_camera()
else:
self.start_camera()
def start_camera(self):
if self.preview_running:
return
if not MEDIAPIPE_AVAILABLE:
messagebox.showerror(
"MediaPipe Error",
"MediaPipe is not properly installed.\n\n"
"Install with: pip install -r requirements.txt"
)
self._set_status("MediaPipe not available")
return
self.cfg = self._gather_ui_to_config()
save_config(self.cfg)
self.stop_event.clear()
self.preview_running = True
self.start_btn.configure(text="Camera Running")
self._set_status("Starting camera...")
self.capture_thread = threading.Thread(target=self._camera_loop, daemon=True)
self.capture_thread.start()
def stop_camera(self):
self.stop_event.set()
self.preview_running = False
self.start_btn.configure(text="Start Camera")
self._set_status("Camera stopped")
if self.video_capture is not None:
try:
self.video_capture.release()
except Exception:
pass
self.video_capture = None
def restore_defaults(self):
if not messagebox.askyesno("Restore Defaults", "Restore all settings to defaults?"):
return
self.cfg = json.loads(json.dumps(DEFAULT_CONFIG))
save_config(self.cfg)
self._load_ui_from_config()
self._set_status("Defaults restored")
def reload_config(self):
self.cfg = load_config()
self._load_ui_from_config()
self._set_status("Config reloaded")
def save_config(self):
self.cfg = self._gather_ui_to_config()
save_config(self.cfg)
self._refresh_calibration_texts()
self._set_status("Config saved")
def reset_calibration(self):
self.cfg["calibration"] = json.loads(json.dumps(DEFAULT_CONFIG["calibration"]))
save_config(self.cfg)
self._refresh_calibration_texts()
self._set_status("Calibration reset")
def capture_center_calibration(self):
self.calibration_state["capture_center"] = True
self.calibration_state["center_samples"] = []
self.calib_status.configure(text="Capture center: hold a neutral hand pose for 1.5s")
self._set_status("Capturing neutral center...")
def capture_swipe_calibration(self):
self.calibration_state["capture_swipe"] = True
self.calibration_state["swipe_samples"] = []
self.calibration_state["countdown"] = 2.0
self.calib_status.configure(text="Capture swipe: do one fast swipe now")
self._set_status("Capturing swipe sample...")
def _record_event(self, gesture: str, hand: str, status: str, action: str = "", error: str = ""):
entry = {
"ts": time.time(),
"gesture": gesture,
"hand": hand,
"status": status,
"action": action,
"error": error,
}
self.events.insert(0, entry)
self.events = self.events[:30]
self._refresh_event_log()
def _refresh_event_log(self):
lines = []
for item in self.events[:20]:
stamp = time.strftime("%H:%M:%S", time.localtime(item["ts"]))
if item["status"] == "triggered":
lines.append(f"[{stamp}] {item['hand']} • {item['gesture']} -> {item.get('action', '')}")
elif item["status"] == "ignored":
lines.append(f"[{stamp}] {item['hand']} • {item['gesture']} (ignored: {item.get('action', '')})")
else:
lines.append(f"[{stamp}] {item['hand']} • {item['gesture']} (error: {item.get('error', '')})")
self.events_text.configure(state="normal")
self.events_text.delete("1.0", "end")
self.events_text.insert("1.0", "\n".join(lines))
self.events_text.configure(state="disabled")
def _create_landmarker(self):
"""Create MediaPipe hand landmarker with error handling for Python 3.14+"""
ensure_model_downloaded()
BaseOptions = mp.tasks.BaseOptions
HandLandmarker = mp.tasks.vision.HandLandmarker
HandLandmarkerOptions = mp.tasks.vision.HandLandmarkerOptions
RunningMode = mp.tasks.vision.RunningMode
det = self.cfg["detection"]
options = HandLandmarkerOptions(
base_options=BaseOptions(model_asset_path=str(MODEL_PATH)),
running_mode=RunningMode.VIDEO,
num_hands=int(det.get("max_num_hands", 2)),
min_hand_detection_confidence=float(det.get("min_detection_confidence", 0.82)),
min_hand_presence_confidence=float(det.get("min_tracking_confidence", 0.78)),
min_tracking_confidence=float(det.get("min_tracking_confidence", 0.78)),
)
return HandLandmarker.create_from_options(options)
def _camera_loop(self):
cfg = load_config()
self.cfg = cfg
width = int(cfg.get("camera_width", 960))
height = int(cfg.get("camera_height", 540))
camera_index = int(cfg.get("camera_index", 0))