-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera_assistant.py
More file actions
566 lines (480 loc) Β· 25.2 KB
/
Copy pathcamera_assistant.py
File metadata and controls
566 lines (480 loc) Β· 25.2 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
#!/usr/bin/env python3
"""Camera Assistant β Tkinter GUI with real-time CV:
β’ Face detection β’ Eye detection
β’ ONNX Hand / Fingers β’ Finger counting
β’ Full body detect β’ Smile detection
β’ Edge / Motion β’ Gesture recognition
Hand detection uses MediaPipe-derived ONNX models (palm detection β hand landmark)
for accurate 21-keypoint hand tracking with finger counting.
Uses onnxruntime + OpenCV. No MediaPipe dependency.
"""
from __future__ import annotations
import os
# Suppress OpenCV noisy logs (MUST be set BEFORE cv2 import)
os.environ["OPENCV_LOG_LEVEL"] = "ERROR"
import tkinter as tk
from tkinter import ttk, messagebox
import cv2
import numpy as np
from PIL import Image, ImageTk
import threading
import time
from typing import Optional
# ββ ONNX hand detector βββββββββββββββββββββββββββββββββββββββββββββββββββββ
try:
from hand_onnx import ONNXHandDetector
_HAS_ONNX_HAND = True
except ImportError:
_HAS_ONNX_HAND = False
# ββ colour palette ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
BG = "#1e1e2e"
FG = "#cdd6f4"
SURFACE = "#2a2a3e"
DARK = "#11111b"
GREEN = "#a6e3a1"
RED = "#f38ba8"
AMBER = "#fab387"
BLUE = "#89b4fa"
# Convert hex RRGGBB β BGR tuple for OpenCV
def _bgr(hex_color: str) -> tuple:
"""'#a6e3a1' β (161, 227, 166)"""
h = hex_color.lstrip("#")
return (int(h[4:6], 16), int(h[2:4], 16), int(h[0:2], 16))
ACCENTS = {"face": _bgr(BLUE), "eye": _bgr(AMBER), "hand": _bgr(GREEN),
"body": _bgr("#cba6f7"), "smile": _bgr("#f9e2af"), "edge": _bgr("#94e2d5")}
# Hex versions for Tkinter (tkinter doesn't understand BGR tuples)
HEX_COLORS = {"face": BLUE, "eye": AMBER, "hand": GREEN,
"body": "#cba6f7", "smile": "#f9e2af", "edge": "#94e2d5"}
# OpenCV BGR constants for drawing code that uses bare colour names
_BGR_GREEN = _bgr(GREEN)
_BGR_AMBER = _bgr(AMBER)
_BGR_DARK = _bgr(DARK)
# ββ Haar cascade paths (OpenCV built-in) βββββββββββββββββββββββββββββββββββ
CASCADE_DIR = cv2.data.haarcascades
CASCADES = {
"face": os.path.join(CASCADE_DIR, "haarcascade_frontalface_default.xml"),
"eye": os.path.join(CASCADE_DIR, "haarcascade_eye.xml"),
"smile": os.path.join(CASCADE_DIR, "haarcascade_smile.xml"),
"profile": os.path.join(CASCADE_DIR, "haarcascade_profileface.xml"),
"fullbody": os.path.join(CASCADE_DIR, "haarcascade_fullbody.xml"),
"upperbody": os.path.join(CASCADE_DIR, "haarcascade_upperbody.xml"),
}
class CameraAssistant:
"""Tkinter camera app with computer-vision overlays."""
def __init__(self, root: tk.Tk) -> None:
self.root = root
self.root.title("π· Camera Assistant β CV")
self.root.geometry("960x720")
self.root.minsize(640, 480)
self.root.configure(bg=BG)
# ββ state ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
self.capture: Optional[cv2.VideoCapture] = None
self.running: bool = False
self.thread: Optional[threading.Thread] = None
self.available_cameras: list[str] = []
# Feature toggles
self.features = {
"face": tk.BooleanVar(value=True),
"eye": tk.BooleanVar(value=False),
"smile": tk.BooleanVar(value=False),
"hand": tk.BooleanVar(value=True), # YCrCb + contour
"body": tk.BooleanVar(value=False),
"edge": tk.BooleanVar(value=False), # Canny edge
}
# ββ classifiers ββββββββββββββββββββββββββββββββββββββββββββββββ
self.classifiers = {}
for name, path in CASCADES.items():
self.classifiers[name] = cv2.CascadeClassifier(path)
# ββ ONNX hand detector βββββββββββββββββββββββββββββββββββββββββ
self._hand_detector = None
self._hand_detector_label = "cv2" # fallback
if _HAS_ONNX_HAND:
try:
self._hand_detector = ONNXHandDetector(
palm_model="models/palm_detection_full_inf_post_192x192.onnx",
landmark_model="models/hand_landmark_sparse_Nx3x224x224.onnx",
score_threshold=0.6,
landmark_threshold=0.3,
)
self._hand_detector_label = "onnx"
except Exception as exc:
print(f"[camera-assistant] ONNX hand detector init failed: {exc}")
self._hand_detector_label = "cv2"
# Background subtractor for edge detection (if enabled)
self._bg_subtractor = cv2.createBackgroundSubtractorMOG2(
history=200, varThreshold=36, detectShadows=False
)
# ββ build UI βββββββββββββββββββββββββββββββββββββββββββββββββββ
self._build_ui()
self._scan_cameras()
self.root.protocol("WM_DELETE_WINDOW", self._on_close)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# UI
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _make_toggle(self, parent: tk.Widget, name: str, var: tk.BooleanVar,
colour: str) -> tk.Checkbutton:
cb = tk.Checkbutton(
parent, text=name.capitalize(), variable=var,
fg=colour, bg=SURFACE, selectcolor=DARK,
activebackground=SURFACE, activeforeground=colour,
font=("Segoe UI", 9, "bold"),
cursor="hand2",
)
cb.pack(side=tk.LEFT, padx=2)
return cb
def _build_ui(self) -> None:
# --- top bar ---
top = tk.Frame(self.root, bg=SURFACE, padx=10, pady=6)
top.pack(fill=tk.X)
tk.Label(top, text="Camera:", fg=FG, bg=SURFACE,
font=("Segoe UI", 10, "bold")).pack(side=tk.LEFT)
self.cam_combo = ttk.Combobox(
top, state="readonly", width=16, font=("Segoe UI", 10),
)
self.cam_combo.pack(side=tk.LEFT, padx=6)
self.cam_combo.bind("<<ComboboxSelected>>", self._on_select)
self.scan_btn = tk.Button(
top, text="β³", command=self._scan_cameras,
bg="#45475a", fg=FG, relief=tk.FLAT, padx=8, font=("Segoe UI", 10),
cursor="hand2",
)
self.scan_btn.pack(side=tk.LEFT, padx=(0, 10))
self.start_btn = tk.Button(
top, text="βΆ Start", command=self._toggle,
bg=GREEN, fg=BG, relief=tk.FLAT, padx=14, pady=2,
font=("Segoe UI", 10, "bold"), cursor="hand2",
)
self.start_btn.pack(side=tk.LEFT)
# feature toggles (packed right)
tog_frame = tk.Frame(top, bg=SURFACE)
tog_frame.pack(side=tk.RIGHT)
for name, var in self.features.items():
c = HEX_COLORS.get(name, FG)
self._make_toggle(tog_frame, name, var, c)
# status
self.status_lbl = tk.Label(
top, text="βΈ Stopped", fg="#a6adc8", bg=SURFACE,
font=("Segoe UI", 9),
)
self.status_lbl.pack(side=tk.RIGHT, padx=(10, 0))
# --- video ---
vframe = tk.Frame(self.root, bg=DARK)
vframe.pack(fill=tk.BOTH, expand=True, padx=8, pady=(0, 8))
self.video_lbl = tk.Label(vframe, bg=DARK)
self.video_lbl.pack(fill=tk.BOTH, expand=True)
# bottom info bar
self.info_lbl = tk.Label(
self.root, text="", fg="#a6adc8", bg=BG,
font=("Segoe UI", 9), anchor=tk.W, padx=10,
)
self.info_lbl.pack(fill=tk.X)
self._show_placeholder()
def _show_placeholder(self) -> None:
w = max(self.video_lbl.winfo_width(), 640)
h = max(self.video_lbl.winfo_height(), 480)
img = Image.new("RGB", (w, h), (17, 17, 27))
self._tk_img = ImageTk.PhotoImage(img)
self.video_lbl.configure(image=self._tk_img)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Camera scan
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _scan_cameras(self) -> None:
self.available_cameras.clear()
self.cam_combo["values"] = ()
self.status_lbl.configure(text="β³ Scanningβ¦")
import glob
existing = sorted(glob.glob("/dev/video*"))
def scan():
found: list[str] = []
# Open by DEVICE PATH β avoids V4L2 index warnings for
# metadata channels (/dev/video1, etc.) and works with
# devices that expect a specific device node.
for dev in existing:
cap = cv2.VideoCapture(dev, cv2.CAP_V4L2)
if cap.isOpened():
ok, _ = cap.read()
if ok:
found.append(dev)
cap.release()
# Fallback β scan indices if nothing found by path
if not found:
for i in range(6):
cap = cv2.VideoCapture(i, cv2.CAP_V4L2)
if cap.isOpened():
ok, _ = cap.read()
if ok:
found.append(f"/dev/video{i}")
cap.release()
self.root.after(0, self._scan_done, found)
threading.Thread(target=scan, daemon=True).start()
def _scan_done(self, found: list[str]) -> None:
self.available_cameras = found
labels = [dev for dev in found] or ["(none)"]
self.cam_combo["values"] = labels
if found:
self.cam_combo.current(0)
self.status_lbl.configure(text=f"β
{len(found)} camera(s)")
else:
self.status_lbl.configure(text="β No camera")
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Start / Stop
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _toggle(self) -> None:
if self.running:
self._stop()
else:
self._start()
def _start(self) -> None:
if not self.available_cameras:
messagebox.showwarning("No Camera", "No cameras found.")
return
idx = self.cam_combo.current()
if idx < 0 or idx >= len(self.available_cameras):
return
dev_path = self.available_cameras[idx]
self.capture = cv2.VideoCapture(dev_path, cv2.CAP_V4L2)
if not self.capture or not self.capture.isOpened():
messagebox.showerror("Error", f"Failed {dev_path}")
return
# Reset background subtractor for new session
self._bg_subtractor = cv2.createBackgroundSubtractorMOG2(
history=200, varThreshold=36, detectShadows=False
)
self.running = True
self.start_btn.configure(text="β Stop", bg=RED)
self.status_lbl.configure(text=f"π· LIVE β {dev_path}")
self.cam_combo.configure(state="disabled")
self.scan_btn.configure(state="disabled")
self.thread = threading.Thread(target=self._loop, daemon=True)
self.thread.start()
def _stop(self) -> None:
self.running = False
if self.thread:
self.thread.join(timeout=1)
self.thread = None
if self.capture:
self.capture.release()
self.capture = None
self.start_btn.configure(text="βΆ Start", bg=GREEN)
self.status_lbl.configure(text="βΈ Stopped")
self.cam_combo.configure(state="readonly")
self.scan_btn.configure(state="normal")
self._show_placeholder()
self.info_lbl.configure(text="")
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# CV processing
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _process_hands(self, frame: cv2.Mat, parts: list[str]) -> None:
"""ONNX hand detection β palm detection + hand landmark.
Shows hand bounding box, 21 landmarks, finger count, and position.
Uses [ONNX] tag in status.
"""
if self._hand_detector is None:
return
h, w = frame.shape[:2]
try:
results = self._hand_detector.detect(frame)
except Exception as exc:
print(f"[hand] ONNX inference failed: {exc}")
return
if not results:
return
accent = ACCENTS["hand"]
for r in results:
n_fingers = r.count_fingers()
landmarks = r.landmarks # 21x2 array
bbox = r.bounding_box()
bx, by, bw, bh = bbox
cx = bx + bw // 2
cy = by + bh // 2
# ββ Bounding box βββββββββββββββββββββββββββββββββββββββββ
cv2.rectangle(frame, (bx, by), (bx + bw, by + bh),
accent, 2, cv2.LINE_AA)
# ββ Draw landmarks βββββββββββββββββββββββββββββββββββββββ
# Draw connections first (from MediaPipe topology)
connections = [
(0, 1), (1, 2), (2, 3), (3, 4), # thumb
(0, 5), (5, 6), (6, 7), (7, 8), # index
(0, 9), (9, 10), (10, 11), (11, 12), # middle
(0, 13), (13, 14), (14, 15), (15, 16), # ring
(0, 17), (17, 18), (18, 19), (19, 20), # pinky
(5, 9), (9, 13), (13, 17), # palm
]
for a, b in connections:
pt1 = (int(landmarks[a, 0]), int(landmarks[a, 1]))
pt2 = (int(landmarks[b, 0]), int(landmarks[b, 1]))
# Check both points are within frame
if 0 <= pt1[0] < w and 0 <= pt1[1] < h and \
0 <= pt2[0] < w and 0 <= pt2[1] < h:
cv2.line(frame, pt1, pt2, _bgr("#89b4fa"), 1, cv2.LINE_AA)
# Landmark dots
for lm in landmarks:
lx, ly = int(lm[0]), int(lm[1])
if 0 <= lx < w and 0 <= ly < h:
cv2.circle(frame, (lx, ly), 3, accent, -1, cv2.LINE_AA)
cv2.circle(frame, (lx, ly), 4, _bgr("#1e1e2e"), 1, cv2.LINE_AA)
# ββ Finger count bar βββββββββββββββββββββββββββββββββββββ
bar_x = max(0, min(cx - 50, w - 100))
bar_y = max(24, by - 12)
seg_h, seg_w, gap = 8, 14, 3
for fi in range(5):
sx2 = bar_x + fi * (seg_w + gap)
clr = accent if fi < n_fingers else _bgr("#45475a")
cv2.rectangle(frame, (sx2, bar_y), (sx2 + seg_w, bar_y + seg_h),
clr, -1, cv2.LINE_AA)
cv2.rectangle(frame, (sx2, bar_y), (sx2 + seg_w, bar_y + seg_h),
_bgr("#585b70"), 1, cv2.LINE_AA)
# ββ Info banner βββββββββββββββββββββββββββββββββββββββββ
banner = f"β {n_fingers}/5 [ONNX]"
(banner_w, banner_h), _ = cv2.getTextSize(
banner, cv2.FONT_HERSHEY_SIMPLEX, 0.65, 2
)
banner_x = max(0, min(cx - banner_w // 2, w - banner_w))
banner_y = max(24, by - 10)
cv2.rectangle(frame, (banner_x - 6, banner_y - 22),
(banner_x + banner_w + 6, banner_y + 6),
(17, 17, 27, 180), -1, cv2.LINE_AA)
cv2.rectangle(frame, (banner_x - 6, banner_y - 22),
(banner_x + banner_w + 6, banner_y + 6),
_bgr("#2a2a3e"), 1, cv2.LINE_AA)
cv2.putText(frame, banner, (banner_x, banner_y),
cv2.FONT_HERSHEY_SIMPLEX, 0.65,
accent, 2, cv2.LINE_AA)
# Position label
pos_label = f"pos ({cx},{cy})"
cv2.putText(frame, pos_label,
(banner_x, banner_y + 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.45,
_bgr("#a6adc8"), 1, cv2.LINE_AA)
# Center dot
cv2.circle(frame, (cx, cy), 4, accent, -1, cv2.LINE_AA)
cv2.circle(frame, (cx, cy), 6, _bgr("#1e1e2e"), 1, cv2.LINE_AA)
parts.append(f"π€ {len(results)} hand(s) [ONNX]")
# ^ old _filter_hand_contours removed β ONNX model handles all filtering
def _process(self, frame: cv2.Mat) -> tuple[cv2.Mat, str]:
"""Apply enabled CV detections. Returns (annotated_frame, info_line)."""
# ββ 1. Enhance low-light frames ββββββββββββββββββββββββββββββββ
mean_brightness = frame.mean()
# Gamma correction for very dark frames (helps ONNX palm detection)
if mean_brightness < 80:
gamma = 0.6
look_up = np.array([((i / 255.0) ** gamma) * 255 for i in range(256)], dtype=np.uint8)
frame = cv2.LUT(frame, look_up)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
h, w = frame.shape[:2]
parts: list[str] = []
# ββ 2. Hand detection FIRST (priority) βββββββββββββββββββββββββ
hand_detected = False
if self.features["hand"].get():
self._process_hands(frame, parts)
hand_detected = any("π€" in p for p in parts)
# ββ 3. Other detection β ONLY when no hand is in frame βββββββ
if not hand_detected:
# ββ Face βββββββββββββββββββββββββββββββββββββββββββββββββ
if self.features["face"].get():
faces = self.classifiers["face"].detectMultiScale(
gray, scaleFactor=1.1, minNeighbors=5, minSize=(60, 60)
)
faces_arr = faces if isinstance(faces, list) else (faces if hasattr(faces, 'shape') else [])
for (x, y, fw, fh) in faces_arr:
cv2.rectangle(frame, (x, y), (x + fw, y + fh),
ACCENTS["face"], 2, cv2.LINE_AA)
cv2.putText(frame, "Face", (x, y - 6),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, ACCENTS["face"], 1)
if self.features["eye"].get():
roi_gray = gray[y:y + fh, x:x + fw]
eyes = self.classifiers["eye"].detectMultiScale(
roi_gray, scaleFactor=1.15, minNeighbors=4, minSize=(20, 20)
)
eyes_arr = eyes if hasattr(eyes, 'shape') else []
for (ex, ey, ew, eh) in eyes_arr:
cv2.rectangle(frame, (x + ex, y + ey),
(x + ex + ew, y + ey + eh),
ACCENTS["eye"], 1, cv2.LINE_AA)
if self.features["smile"].get():
roi_gray2 = gray[y:y + fh, x:x + fw]
smiles = self.classifiers["smile"].detectMultiScale(
roi_gray2, scaleFactor=1.7, minNeighbors=20, minSize=(25, 25)
)
smiles_arr = smiles if hasattr(smiles, 'shape') else []
for (sx, sy, sw, sh) in smiles_arr:
cv2.rectangle(frame, (x + sx, y + sy),
(x + sx + sw, y + sy + sh),
ACCENTS["smile"], 1, cv2.LINE_AA)
if hasattr(faces, 'any') and faces.any():
parts.append(f"π€ {len(faces)} face(s)")
# ββ Full / Upper body ββββββββββββββββββββββββββββββββββββ
if self.features["body"].get():
bodies = self.classifiers["fullbody"].detectMultiScale(
gray, scaleFactor=1.1, minNeighbors=3, minSize=(100, 200)
)
for (bx, by, bw, bh) in bodies:
cv2.rectangle(frame, (bx, by), (bx + bw, by + bh),
ACCENTS["body"], 2, cv2.LINE_AA)
if hasattr(bodies, 'any') and bodies.any():
parts.append(f"π€ {len(bodies)} body/face(s)")
else:
# Hand is detected β only draw a clean status
cv2.putText(frame, "β HAND ACTIVE β ONNX", (w - 260, 26),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, ACCENTS["hand"], 1, cv2.LINE_AA)
# ββ Edge (Canny) βββββββββββββββββββββββββββββββββββββββββββββββββ
if self.features["edge"].get():
edges = cv2.Canny(gray, 50, 150)
edge_bgr = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
frame = cv2.addWeighted(frame, 0.8, edge_bgr, 0.2, 0)
return (frame, " β’ ".join(parts) if parts else "")
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Video loop
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _loop(self) -> None:
fps_counter = 0
fps_timer = time.monotonic()
fps_val = 0
while self.running and self.capture and self.capture.isOpened():
ok, frame = self.capture.read()
if not ok:
continue
frame, info = self._process(frame)
# FPS measurement
fps_counter += 1
elapsed = time.monotonic() - fps_timer
if elapsed >= 1.0:
fps_val = round(fps_counter / elapsed)
fps_counter = 0
fps_timer = time.monotonic()
# FPS overlay
cv2.putText(frame, f"{fps_val} FPS", (8, 26),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, _BGR_AMBER, 1, cv2.LINE_AA)
# resize + display
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img = Image.fromarray(frame_rgb)
img = self._fit(img)
tk_img = ImageTk.PhotoImage(img)
self.root.after(0, self._update, tk_img, info)
time.sleep(1 / 30)
def _update(self, tk_img: ImageTk.PhotoImage, info: str) -> None:
self._tk_img = tk_img
self.video_lbl.configure(image=self._tk_img)
self.info_lbl.configure(text=info)
def _fit(self, img: Image.Image) -> Image.Image:
w = self.video_lbl.winfo_width() or 640
h = self.video_lbl.winfo_height() or 480
img.thumbnail((w, h), Image.LANCZOS)
return img
def _on_select(self, _=None) -> None:
if self.available_cameras:
idx = self.cam_combo.current()
dev_path = self.available_cameras[idx]
self.status_lbl.configure(text=f"Ready β {dev_path}")
def _on_close(self) -> None:
self.running = False
if self.thread:
self.thread.join(timeout=1)
if self.capture:
self.capture.release()
self.root.destroy()
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
if __name__ == "__main__":
window = tk.Tk()
app = CameraAssistant(window)
window.mainloop()