-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
1121 lines (976 loc) · 47.2 KB
/
Copy pathmain.py
File metadata and controls
1121 lines (976 loc) · 47.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
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
"""
CCTV Motion-Aware Video Player · Enhanced Edition v3
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Install dependencies:
pip install opencv-python pillow numpy
GPU acceleration (analysis):
─ CUDA : if cv2.cuda is available (NVIDIA)
─ OpenCL : iGPU / AMD / Intel (default for most systems)
─ CPU : fallback
Keyboard shortcuts
──────────────────
Space → Play / Pause
← / → → Seek ±10 s
Shift+← / → → Seek ±3 s
Ctrl+← / → → Seek ±1 min
Click seekbar → Jump (respects zoom window)
Click graph → Jump to that timestamp
Scroll graph → Zoom in / out around cursor
"""
import json
import math
import queue as _queue
import threading
import time
import tkinter as tk
from tkinter import colorchooser, filedialog, messagebox
import cv2
import numpy as np
from PIL import Image, ImageTk
# ─────────────────────── Configuration ───────────────────────────────
GRAPH_HEIGHT = 110
ANALYSIS_STEP = 6
GRAPH_BUCKETS = 600
QUEUE_DEPTH = 32 # decode-ahead buffer between producer & GPU consumer
C_BG_DARK = "#0c0e13"
C_BG_MID = "#13171f"
C_BG_PANEL = "#181d27"
C_ACCENT = "#00e5ff"
C_WARN = "#ff7730"
C_ALERT = "#ff2244"
C_LOW = "#1a5566"
C_YELLOW = "#e8c030"
C_TEXT = "#dde0e8"
C_DIM = "#48505f"
C_SEEK_BG = "#20262f"
C_SEEK_FILL = "#00c8e8"
C_GRAPH_GRID = "#1e2530"
C_BTN_ACTIVE = "#00a0b8"
class CCTVPlayer:
def __init__(self, root: tk.Tk):
self.root = root
self.root.title("CCTV Motion Player")
self.root.configure(bg=C_BG_DARK)
# Video
self.cap : cv2.VideoCapture | None = None
self.video_path : str = ""
self.total_frames : int = 0
self.fps : float = 30.0
self.current_frame : int = 0
# Playback
self.playing : bool = False
self.speed : float = 1.0
self._stop_evt = threading.Event()
self._seek_lock = threading.Lock()
# Motion analysis
self.motion_scores : np.ndarray = np.array([])
self.graph_buckets : np.ndarray = np.array([])
self.analysis_done : bool = False
self.scores_seen_frac : float = 0.0
self.photo_image = None
self._scores_imported : bool = False
# Heatmap
self.heatmap_on : bool = False
self.heatmap_color : tuple = (0, 0, 255) # BGR
self._prev_hm_frame = None
# Graph zoom (fractions of total duration 0–1)
self.zoom_start : float = 0.0
self.zoom_end : float = 1.0
self._min_zoom_frac : float = 0.01
# Pending seek: set by _seek_to, consumed by _loop once
self._pending_seek : int | None = None
# Realtime playback toggle (shown only while playing)
self._realtime_var = tk.BooleanVar(value=True)
self._realtime_tip = None
self._build_ui()
self._bind_keys()
# ──────────────────── UI ─────────────────────────────────────────
def _build_ui(self):
r = self.root
# ── Toolbar ───────────────────────────────────────────────────
bar = tk.Frame(r, bg=C_BG_PANEL, pady=5)
bar.pack(fill="x", side="top")
btn_cfg = dict(bg=C_BG_MID, fg=C_TEXT, activebackground=C_ACCENT,
activeforeground=C_BG_DARK, relief="flat", bd=0,
font=("Consolas", 10, "bold"), padx=10, pady=5,
cursor="hand2")
self.btn_open = tk.Button(bar, text="⏏ Open", command=self.open_file, **btn_cfg)
self.btn_play = tk.Button(bar, text="▶ Play", command=self.toggle_play, **btn_cfg)
self.btn_stop = tk.Button(bar, text="■ Stop", command=self.stop, **btn_cfg)
self.btn_open.pack(side="left", padx=(8, 3))
self.btn_play.pack(side="left", padx=3)
self.btn_stop.pack(side="left", padx=3)
self._realtime_frame = tk.Frame(bar, bg=C_BG_PANEL)
self._realtime_cb = tk.Checkbutton(
self._realtime_frame, text="Realtime",
variable=self._realtime_var,
bg=C_BG_PANEL, fg=C_TEXT, selectcolor=C_BG_MID,
activebackground=C_BG_PANEL, activeforeground=C_ACCENT,
font=("Consolas", 9), cursor="hand2", bd=0, relief="flat")
self._realtime_cb.pack()
self._realtime_cb.bind("<Enter>", self._rt_tip_show)
self._realtime_cb.bind("<Leave>", self._rt_tip_hide)
_sep(bar)
tk.Label(bar, text="Speed:", bg=C_BG_PANEL, fg=C_DIM,
font=("Consolas", 9)).pack(side="left", padx=(4, 2))
self.speed_var = tk.StringVar(value="1.0")
self.speed_entry = tk.Entry(bar, textvariable=self.speed_var,
width=4, bg=C_BG_MID, fg=C_ACCENT,
insertbackground=C_ACCENT, relief="flat",
font=("Consolas", 10), justify="center")
self.speed_entry.pack(side="left", ipady=3)
self.speed_entry.bind("<Return>", self._apply_speed)
self.speed_entry.bind("<FocusOut>", self._apply_speed)
tk.Label(bar, text="×", bg=C_BG_PANEL, fg=C_DIM,
font=("Consolas", 9)).pack(side="left", padx=(1, 6))
_sep(bar)
self.btn_heatmap = tk.Button(bar, text="🌡 Heatmap",
command=self._toggle_heatmap, **btn_cfg)
self.btn_heatmap.pack(side="left", padx=(6, 3))
tk.Label(bar, text="Opac%:", bg=C_BG_PANEL, fg=C_DIM,
font=("Consolas", 9)).pack(side="left", padx=(4, 1))
self.opacity_var = tk.StringVar(value="50")
tk.Entry(bar, textvariable=self.opacity_var,
width=4, bg=C_BG_MID, fg=C_ACCENT,
insertbackground=C_ACCENT, relief="flat",
font=("Consolas", 10), justify="center"
).pack(side="left", ipady=3)
tk.Label(bar, text="Thr:", bg=C_BG_PANEL, fg=C_DIM,
font=("Consolas", 9)).pack(side="left", padx=(6, 1))
self.threshold_var = tk.StringVar(value="20")
tk.Entry(bar, textvariable=self.threshold_var,
width=4, bg=C_BG_MID, fg=C_ACCENT,
insertbackground=C_ACCENT, relief="flat",
font=("Consolas", 10), justify="center"
).pack(side="left", ipady=3)
self.btn_hm_color = tk.Button(bar, text=" 🎨 ", command=self._pick_heatmap_color,
bg="#ff0000", fg="#ffffff", relief="flat", bd=0,
font=("Consolas", 10), padx=6, pady=5,
activeforeground="#ffffff", cursor="hand2")
self.btn_hm_color.pack(side="left", padx=(6, 0))
_sep(bar)
self.btn_export = tk.Button(bar, text="⬇ Export", command=self._export_intensity,
**btn_cfg)
self.btn_import = tk.Button(bar, text="⬆ Import", command=self._import_intensity,
**btn_cfg)
self._io_anchor = tk.Frame(bar, width=0, bg=C_BG_PANEL)
self._io_anchor.pack(side="left")
self.btn_import.pack(side="left", padx=3, after=self._io_anchor)
self.lbl_time = tk.Label(bar, text="--:-- / --:--",
bg=C_BG_PANEL, fg=C_ACCENT,
font=("Consolas", 10))
self.lbl_time.pack(side="right", padx=12)
self.lbl_status = tk.Label(bar, text="Open a video file",
bg=C_BG_PANEL, fg=C_DIM,
font=("Consolas", 9))
self.lbl_status.pack(side="right", padx=12)
# ── Video canvas ──────────────────────────────────────────────
self.vid_canvas = tk.Canvas(r, bg="#000000", highlightthickness=0)
self.vid_canvas.pack(fill="both", expand=True)
# ── Bottom panel ──────────────────────────────────────────────
bot = tk.Frame(r, bg=C_BG_DARK)
bot.pack(fill="x", side="bottom")
sf = tk.Frame(bot, bg=C_BG_DARK, padx=8, pady=4)
sf.pack(fill="x")
zr = tk.Frame(sf, bg=C_BG_DARK)
zr.pack(fill="x")
self.lbl_zoom_left = tk.Label(zr, text="00:00", bg=C_BG_DARK,
fg=C_DIM, font=("Consolas", 7), anchor="w")
self.lbl_zoom_right = tk.Label(zr, text="--:--", bg=C_BG_DARK,
fg=C_DIM, font=("Consolas", 7), anchor="e")
self.lbl_zoom_left.pack(side="left")
self.lbl_zoom_right.pack(side="right")
self.seek_cv = tk.Canvas(sf, height=22, bg=C_SEEK_BG,
highlightthickness=0, cursor="hand2")
self.seek_cv.pack(fill="x")
self.seek_cv.bind("<Button-1>", self._seek_click)
self.seek_cv.bind("<B1-Motion>", self._seek_click)
self.seek_cv.bind("<Configure>", lambda e: self._draw_seekbar())
tk.Label(bot, text="◈ MOTION INTENSITY — scroll to zoom · click to seek",
bg=C_BG_DARK, fg=C_DIM, font=("Consolas", 8), anchor="w"
).pack(fill="x", padx=8)
self.graph_cv = tk.Canvas(bot, height=GRAPH_HEIGHT,
bg=C_BG_MID, highlightthickness=1,
highlightbackground="#252d3a", cursor="hand2")
self.graph_cv.pack(fill="x", padx=8, pady=(0, 8))
self.graph_cv.bind("<Button-1>", self._graph_click)
self.graph_cv.bind("<Configure>", lambda e: self._draw_graph())
self.graph_cv.bind("<Motion>", self._graph_hover)
self.graph_cv.bind("<MouseWheel>", self._graph_scroll)
self.graph_cv.bind("<Button-4>", self._graph_scroll)
self.graph_cv.bind("<Button-5>", self._graph_scroll)
for widget in (self.vid_canvas, self.seek_cv, self.graph_cv):
widget.bind("<Button-1>", self._defocus, add="+")
self.root.bind("<Button-1>", self._defocus_root, add="+")
self._draw_graph()
def _bind_keys(self):
r = self.root
def _ent():
return isinstance(self.root.focus_get(), tk.Entry)
r.bind("<space>", lambda e: None if _ent() else self.toggle_play())
r.bind("<Left>", lambda e: None if _ent() else self._step_sec(-10))
r.bind("<Right>", lambda e: None if _ent() else self._step_sec(10))
r.bind("<Shift-Left>", lambda e: None if _ent() else self._step_sec(-3))
r.bind("<Shift-Right>", lambda e: None if _ent() else self._step_sec(3))
r.bind("<Control-Left>", lambda e: None if _ent() else self._step_sec(-60))
r.bind("<Control-Right>", lambda e: None if _ent() else self._step_sec(60))
# ──────────────────── Focus management ──────────────────────────
def _defocus(self, event=None):
self.root.focus_set()
def _defocus_root(self, event=None):
if isinstance(event.widget, tk.Entry):
return
self.root.focus_set()
# ──────────────────── Realtime tooltip ───────────────────────────
_RT_TIP = ("✔ Realtime ON — drops frames to keep in sync with wall clock.\n"
"✘ Realtime OFF — shows every frame; video clock may lag real time.")
def _rt_tip_show(self, event=None):
if self._realtime_tip:
return
tip = tk.Toplevel(self.root)
tip.wm_overrideredirect(True)
tip.wm_attributes("-topmost", True)
x = self._realtime_cb.winfo_rootx()
y = self._realtime_cb.winfo_rooty() + self._realtime_cb.winfo_height() + 4
tip.wm_geometry(f"+{x}+{y}")
tk.Label(tip, text=self._RT_TIP, justify="left",
bg="#1e2530", fg=C_TEXT, font=("Consolas", 8),
padx=8, pady=5, relief="flat", bd=0).pack()
self._realtime_tip = tip
def _rt_tip_hide(self, event=None):
if self._realtime_tip:
self._realtime_tip.destroy()
self._realtime_tip = None
# ──────────────────── Speed ──────────────────────────────────────
def _apply_speed(self, event=None):
try:
v = float(self.speed_var.get())
v = max(0.1, min(8.0, v))
self.speed = v
self.speed_var.set(f"{v:.2g}")
except ValueError:
self.speed_var.set(f"{self.speed:.2g}")
# ──────────────────── Heatmap ────────────────────────────────────
def _toggle_heatmap(self):
self.heatmap_on = not self.heatmap_on
self.btn_heatmap.config(
bg=C_BTN_ACTIVE if self.heatmap_on else C_BG_MID,
fg=C_BG_DARK if self.heatmap_on else C_TEXT)
if not self.heatmap_on:
self._prev_hm_frame = None
def _pick_heatmap_color(self):
b, g, r = self.heatmap_color
init_hex = f"#{r:02x}{g:02x}{b:02x}"
result = colorchooser.askcolor(color=init_hex, title="Choose heatmap colour")
if result and result[0]:
ri, gi, bi = [int(x) for x in result[0]]
self.heatmap_color = (bi, gi, ri)
hex_col = f"#{ri:02x}{gi:02x}{bi:02x}"
self.btn_hm_color.config(bg=hex_col)
def _get_opacity(self) -> float:
try:
v = float(self.opacity_var.get())
return max(0.0, min(100.0, v)) / 100.0
except ValueError:
return 0.5
def _get_threshold(self) -> int:
try:
v = int(self.threshold_var.get())
return max(1, min(254, v))
except ValueError:
return 20
def _apply_heatmap(self, frame: np.ndarray) -> np.ndarray:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.GaussianBlur(gray, (5, 5), 0, dst=gray)
if self._prev_hm_frame is None or self._prev_hm_frame.shape != gray.shape:
self._prev_hm_frame = gray
return frame
thr = self._get_threshold()
diff = cv2.absdiff(gray, self._prev_hm_frame)
_, mask = cv2.threshold(diff, thr, 255, cv2.THRESH_BINARY)
kernel = np.ones((5, 5), np.uint8)
mask = cv2.dilate(mask, kernel, iterations=2)
overlay = np.zeros_like(frame)
overlay[:] = self.heatmap_color
mask3 = cv2.merge([mask, mask, mask])
cv2.bitwise_and(overlay, mask3, dst=overlay)
alpha = self._get_opacity()
result = cv2.addWeighted(frame, 1.0 - alpha, overlay, alpha, 0)
inv_mask3 = cv2.bitwise_not(mask3)
cv2.bitwise_and(frame, inv_mask3, dst=frame)
cv2.bitwise_or(frame, cv2.bitwise_and(result, mask3), dst=frame)
self._prev_hm_frame = gray
return frame
# ──────────────────── Export / Import visibility ─────────────────
def _refresh_io_buttons(self):
if self.cap is None:
try:
self.btn_import.pack_info()
except tk.TclError:
self.btn_import.pack(side="left", padx=3, after=self._io_anchor)
else:
self.btn_import.pack_forget()
if self.analysis_done:
try:
self.btn_export.pack_info()
except tk.TclError:
self.btn_export.pack(side="left", padx=3, after=self._io_anchor)
else:
self.btn_export.pack_forget()
# ──────────────────── Export / Import ────────────────────────────
def _export_intensity(self):
if not self.analysis_done or len(self.motion_scores) == 0:
messagebox.showwarning("Export", "Analysis not complete yet.")
return
path = filedialog.asksaveasfilename(
title="Export Motion Intensity",
defaultextension=".json",
filetypes=[("JSON", "*.json"), ("All files", "*.*")])
if not path:
return
data = {
"video_path" : self.video_path,
"total_frames" : int(self.total_frames),
"fps" : float(self.fps),
"analysis_done": True,
"motion_scores": [round(float(x), 6) for x in self.motion_scores],
}
with open(path, "w") as f:
json.dump(data, f, separators=(",", ":"))
name = path.replace("\\", "/").split("/")[-1]
self.lbl_status.config(text=f"Exported → {name}", fg=C_ACCENT)
def _import_intensity(self):
path = filedialog.askopenfilename(
title="Import Motion Intensity",
filetypes=[("JSON", "*.json"), ("All files", "*.*")])
if not path:
return
try:
with open(path) as f:
data = json.load(f)
scores = np.array(data["motion_scores"], dtype=np.float32)
self.motion_scores = scores
self.total_frames = len(scores)
self.fps = float(data.get("fps", 30.0))
self.analysis_done = True
self._scores_imported = True
self._bucket_scores()
self._draw_graph()
self._update_zoom_labels()
self.lbl_status.config(
text="Intensity imported — open the matching video file",
fg=C_ACCENT)
self._refresh_io_buttons()
except Exception as exc:
messagebox.showerror("Import error", str(exc))
# ──────────────────── Graph zoom ─────────────────────────────────
def _graph_scroll(self, event):
if self.total_frames == 0:
return
w = self.graph_cv.winfo_width()
if w < 2:
return
if event.num == 4: delta = 1
elif event.num == 5: delta = -1
else: delta = event.delta
zoom_range = self.zoom_end - self.zoom_start
cursor_frac = max(0.0, min(1.0, event.x / w))
abs_cursor = self.zoom_start + cursor_frac * zoom_range
factor = 0.85 if delta > 0 else (1.0 / 0.85)
new_range = zoom_range * factor
self._min_zoom_frac = max(0.001, 2.0 / max(1, self.total_frames / self.fps))
new_range = max(self._min_zoom_frac, min(1.0, new_range))
new_start = abs_cursor - cursor_frac * new_range
new_end = new_start + new_range
if new_start < 0:
new_start, new_end = 0.0, new_range
if new_end > 1.0:
new_end, new_start = 1.0, 1.0 - new_range
self.zoom_start = max(0.0, new_start)
self.zoom_end = min(1.0, new_end)
self._draw_graph()
self._draw_seekbar()
self._update_zoom_labels()
def _update_zoom_labels(self):
if self.total_frames == 0:
return
t_start = self.zoom_start * self.total_frames / self.fps
t_end = self.zoom_end * self.total_frames / self.fps
self.lbl_zoom_left.config(text=self._fmt(t_start))
self.lbl_zoom_right.config(text=self._fmt(t_end))
# ──────────────────── File / Analysis ────────────────────────────
def open_file(self):
path = filedialog.askopenfilename(
title="Open CCTV / Video File",
filetypes=[("Video", "*.mp4 *.avi *.mkv *.mov *.wmv *.m4v *.ts *.flv"),
("All files", "*.*")])
if not path:
return
self.stop()
if self.cap:
self.cap.release()
self.video_path = path
self.cap = cv2.VideoCapture(path)
if not self.cap.isOpened():
messagebox.showerror("Error", "Cannot open this file.")
self.cap = None
return
try:
self.cap.set(cv2.CAP_PROP_HW_ACCELERATION, cv2.VIDEO_ACCELERATION_ANY)
except Exception:
pass
if cv2.ocl.haveOpenCL():
cv2.ocl.setUseOpenCL(True)
self.total_frames = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT))
self.fps = self.cap.get(cv2.CAP_PROP_FPS) or 30.0
self.current_frame = 0
self._prev_hm_frame = None
self._min_zoom_frac = max(0.001, 2.0 / max(1, self.total_frames / self.fps))
self.zoom_start = 0.0
self.zoom_end = 1.0
if self._scores_imported and len(self.motion_scores) == self.total_frames:
self.analysis_done = True
self.scores_seen_frac = 1.0
self._bucket_scores()
self._seek_to(0)
self._update_zoom_labels()
self._refresh_io_buttons()
name = path.replace("\\", "/").split("/")[-1]
self.root.title(f"CCTV Motion Player — {name}")
self.lbl_status.config(text="Ready (imported intensity)", fg=C_ACCENT)
self._draw_graph()
return
self.analysis_done = False
self.scores_seen_frac = 0.0
self._scores_imported = False
self.motion_scores = np.zeros(max(self.total_frames, 1))
self.graph_buckets = np.array([])
name = path.replace("\\", "/").split("/")[-1]
self.root.title(f"CCTV Motion Player — {name}")
self.lbl_status.config(text="Analysing motion…", fg=C_WARN)
self._refresh_io_buttons()
self._seek_to(0)
self._update_zoom_labels()
threading.Thread(target=self._analyse_motion, daemon=True).start()
# ──────────────────── Motion Analysis (GPU Pipeline) ─────────────
def _analyse_motion(self):
"""
Two-thread pipeline for maximum iGPU utilisation:
Producer (background thread)
VideoCapture.read() / grab() → downscale on CPU
→ frame_queue (QUEUE_DEPTH=32 deep)
Consumer (this thread)
frame_queue → CUDA / OpenCL / CPU diff scoring
→ motion_scores[]
Why this boosts iGPU from ~40 % → ~75-90 %:
Before: GPU idle while CPU decodes next frame.
After: Decode and GPU-compute run concurrently;
the queue keeps the GPU always fed.
Extra wins:
• OpenCL JIT pre-warm (eliminates 1st-kernel stall)
• CUDA GaussianFilter created once, not per frame
• grab() for skipped frames (cheaper than read())
• UMat stays on GPU across absdiff/threshold/countNonZero
"""
# ── Select best GPU backend ────────────────────────────────
use_cuda = False
use_ocl = False
dev_name = "CPU"
cuda_gauss = None
if hasattr(cv2, "cuda"):
try:
n_cuda = cv2.cuda.getCudaEnabledDeviceCount()
except Exception:
n_cuda = 0
if n_cuda > 0:
use_cuda = True
try:
dev_name = cv2.cuda.DeviceInfo(0).name()
cuda_gauss = cv2.cuda.createGaussianFilter(
cv2.CV_8UC1, cv2.CV_8UC1, (5, 5), 0)
except Exception:
use_cuda = False
if not use_cuda and cv2.ocl.haveOpenCL():
cv2.ocl.setUseOpenCL(True)
dev = cv2.ocl.Device.getDefault()
if dev.available() and dev.type() in (2, 4): # GPU or ACCELERATOR
use_ocl = True
dev_name = dev.name()
# ── Pre-warm OpenCL JIT ──────────────────────────
# First kernel compilation stalls ~200-400 ms; run it now
# on a dummy mat so the hot loop never hits that stall.
try:
_w = cv2.UMat(np.zeros((8, 8), np.uint8))
_b = cv2.GaussianBlur(_w, (5, 5), 0)
_d = cv2.absdiff(_b, _w)
_, _t = cv2.threshold(_d, 20, 255, cv2.THRESH_BINARY)
cv2.countNonZero(_t) # force full pipeline compile
except Exception:
use_ocl = False
cv2.ocl.setUseOpenCL(False)
else:
use_ocl = False
cv2.ocl.setUseOpenCL(False)
backend = ("CUDA·" + dev_name if use_cuda
else "OpenCL·" + dev_name if use_ocl
else "CPU")
self.root.after(0, lambda: self.lbl_status.config(
text=f"Analysing… [{backend}]", fg=C_WARN))
skip = max(1, ANALYSIS_STEP)
scores = np.zeros(self.total_frames, dtype=np.float32)
frame_q = _queue.Queue(maxsize=QUEUE_DEPTH)
update_every = max(1, (self.total_frames // skip) // 40)
# ────────────────────────────────────────────────────────────
# Producer thread: decode + downscale, feed the queue
# Runs on a separate thread so GPU stays busy in consumer.
# ────────────────────────────────────────────────────────────
def _producer():
cap2 = cv2.VideoCapture(self.video_path)
# Request hardware-accelerated decode (Quick Sync / D3D11 / VAAPI)
for prop, val in (
(cv2.CAP_PROP_HW_ACCELERATION, cv2.VIDEO_ACCELERATION_ANY),
(cv2.CAP_PROP_HW_DEVICE, 0),
):
try:
cap2.set(prop, val)
except Exception:
pass
frame_num = 0
while frame_num < self.total_frames:
if self._scores_imported:
break
if frame_num % skip == 0:
ret, frame = cap2.read()
if not ret:
break
# Downscale on CPU before upload — smaller buffer = faster GPU xfer
h, w = frame.shape[:2]
if h > 360:
nw = int(w * 360 / h)
frame = cv2.resize(frame, (nw, 360),
interpolation=cv2.INTER_LINEAR)
frame_q.put((frame_num, frame)) # blocks if queue is full
frame_num += 1
else:
# grab() skips the JPEG/codec decode — far cheaper than read()
if not cap2.grab():
break
frame_num += 1
frame_q.put(None) # sentinel: tell consumer we're done
cap2.release()
prod_thread = threading.Thread(target=_producer, daemon=True)
prod_thread.start()
# ────────────────────────────────────────────────────────────
# Consumer (this thread): GPU diff scoring
# Pulls pre-decoded frames and runs the full pipeline on GPU.
# ────────────────────────────────────────────────────────────
prev_gpu = None # UMat | GpuMat | ndarray
sampled = 0
last_pct = -1
scores_seen = 0
while True:
item = frame_q.get()
if item is None:
break
frame_num, frame = item
# ── CUDA path (NVIDIA) ────────────────────────────────
if use_cuda:
try:
gf = cv2.cuda_GpuMat()
gf.upload(frame)
gray_g = cv2.cuda.cvtColor(gf, cv2.COLOR_BGR2GRAY)
blur_g = cuda_gauss.apply(gray_g)
if prev_gpu is not None:
diff_g = cv2.cuda.absdiff(blur_g, prev_gpu)
_, thr_g = cv2.cuda.threshold(
diff_g, 20, 255, cv2.THRESH_BINARY)
# countNonZero needs a CPU mat; download is cheap at 360p
thr_cpu = thr_g.download()
count = float(np.count_nonzero(thr_cpu))
total = float(thr_cpu.size)
score = count / total if total > 0 else 0.0
end_i = min(frame_num + skip, self.total_frames)
scores[frame_num:end_i] = score
scores_seen = end_i
prev_gpu = blur_g
except Exception:
use_cuda = False # fall through to OpenCL / CPU silently
prev_gpu = None
cuda_gauss = None
# ── OpenCL path (iGPU / AMD / Intel) ─────────────────
if use_ocl and not use_cuda:
# Upload once; all ops stay on GPU until countNonZero
uf = cv2.UMat(frame)
gray = cv2.cvtColor(uf, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
if prev_gpu is not None:
diff = cv2.absdiff(blurred, prev_gpu)
_, thr = cv2.threshold(diff, 20, 255, cv2.THRESH_BINARY)
# countNonZero on UMat triggers implicit sync — unavoidable,
# but the result is a scalar so the readback is near-zero cost
count = float(cv2.countNonZero(thr))
total = float(frame.shape[0] * frame.shape[1])
score = count / total if total > 0 else 0.0
end_i = min(frame_num + skip, self.total_frames)
scores[frame_num:end_i] = score
scores_seen = end_i
prev_gpu = blurred # keep blurred UMat on GPU for next diff
# ── CPU fallback ──────────────────────────────────────
if not use_cuda and not use_ocl:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.GaussianBlur(gray, (5, 5), 0, dst=gray)
if prev_gpu is not None:
diff = cv2.absdiff(gray, prev_gpu)
count = float(np.count_nonzero(diff > 20))
total = float(diff.size)
score = count / total if total > 0 else 0.0
end_i = min(frame_num + skip, self.total_frames)
scores[frame_num:end_i] = score
scores_seen = end_i
prev_gpu = gray
# ── Progress update ───────────────────────────────────
sampled += 1
if sampled % update_every == 0 and scores_seen > 0:
pct = int(100 * frame_num / self.total_frames)
if pct != last_pct:
last_pct = pct
self._bucket_scores_arr(scores)
self.scores_seen_frac = scores_seen / self.total_frames
self.root.after(0, lambda p=pct, b=backend:
self.lbl_status.config(
text=f"Analysing… {p}% [{b}]", fg=C_WARN))
self.root.after(0, self._draw_graph)
prod_thread.join(timeout=10)
self.motion_scores = scores
self._bucket_scores()
self.analysis_done = True
self.root.after(0, self._analysis_complete)
def _bucket_scores_arr(self, arr: np.ndarray):
n = len(arr)
if n == 0:
self.graph_buckets = np.array([])
return
step = max(1, n // GRAPH_BUCKETS)
self.graph_buckets = np.array([arr[i:i+step].mean() for i in range(0, n, step)])
def _bucket_scores(self):
self._bucket_scores_arr(self.motion_scores)
def _analysis_complete(self):
self.lbl_status.config(text="Ready — scroll graph to zoom · click to seek",
fg=C_ACCENT)
self._refresh_io_buttons()
self._draw_graph()
# ──────────────────── Playback ───────────────────────────────────
def toggle_play(self):
if not self.cap:
return
if self.playing:
self._pause()
else:
self._play()
def _play(self):
self.playing = True
self._stop_evt.clear()
self.btn_play.config(text="⏸ Pause")
if self.heatmap_on:
self._prev_hm_frame = None
self._realtime_frame.pack(side="left", padx=(4, 3), after=self.btn_stop)
threading.Thread(target=self._loop, daemon=True).start()
def _pause(self):
self.playing = False
self._stop_evt.set()
self.btn_play.config(text="▶ Play")
self._realtime_frame.pack_forget()
def stop(self):
self._pause()
if self.cap:
self.current_frame = 0
self._refresh_ui()
def _loop(self):
ocl = cv2.ocl.useOpenCL() and cv2.ocl.haveOpenCL()
interval = 1.0 / max(1.0, self.fps)
next_frame_time = time.perf_counter()
cw = self.vid_canvas.winfo_width()
ch = self.vid_canvas.winfo_height()
while not self._stop_evt.is_set():
with self._seek_lock:
if self._pending_seek is not None:
target = max(0, min(self._pending_seek, self.total_frames - 1))
self.cap.set(cv2.CAP_PROP_POS_FRAMES, target)
self._pending_seek = None
self.current_frame = target
if self.heatmap_on:
self._prev_hm_frame = None
next_frame_time = time.perf_counter()
ret, frame = self.cap.read()
if not ret:
self.root.after(0, self._pause)
break
self.current_frame = int(self.cap.get(cv2.CAP_PROP_POS_FRAMES))
if self.heatmap_on:
frame = self._apply_heatmap(frame)
cw = self.vid_canvas.winfo_width()
ch = self.vid_canvas.winfo_height()
if cw > 1 and ch > 1:
fh, fw = frame.shape[:2]
scale = min(cw / fw, ch / fh)
nw = int(fw * scale)
nh = int(fh * scale)
if ocl:
try:
uf = cv2.UMat(frame)
sm_u = cv2.resize(uf, (nw, nh),
interpolation=cv2.INTER_LINEAR)
rgb_u = cv2.cvtColor(sm_u, cv2.COLOR_BGR2RGB)
rgb = rgb_u.get()
except Exception:
ocl = False
small = cv2.resize(frame, (nw, nh),
interpolation=cv2.INTER_LINEAR)
rgb = cv2.cvtColor(small, cv2.COLOR_BGR2RGB)
else:
small = cv2.resize(frame, (nw, nh),
interpolation=cv2.INTER_LINEAR)
rgb = cv2.cvtColor(small, cv2.COLOR_BGR2RGB)
img = Image.fromarray(rgb)
photo = ImageTk.PhotoImage(img)
ox = (cw - nw) // 2
oy = (ch - nh) // 2
self.root.after(0, self._blit_photo, photo, ox, oy)
self.root.after(0, self._refresh_ui)
interval = 1.0 / max(0.1, self.fps * self.speed)
next_frame_time += interval
now = time.perf_counter()
slack = next_frame_time - now
if self._realtime_var.get():
if slack > 0.0005:
time.sleep(slack)
elif slack < -interval:
next_frame_time = now
else:
if slack > 0.0005:
time.sleep(slack)
else:
time.sleep(max(0.0, interval * 0.05))
def _blit_photo(self, photo, ox: int, oy: int):
self.photo_image = photo
self.vid_canvas.delete("all")
self.vid_canvas.create_image(ox, oy, anchor="nw", image=photo)
def _show_frame(self, frame):
cw = self.vid_canvas.winfo_width()
ch = self.vid_canvas.winfo_height()
if cw < 2 or ch < 2:
return
fh, fw = frame.shape[:2]
scale = min(cw / fw, ch / fh)
nw, nh = int(fw * scale), int(fh * scale)
small = cv2.resize(frame, (nw, nh), interpolation=cv2.INTER_LINEAR)
rgb = cv2.cvtColor(small, cv2.COLOR_BGR2RGB)
photo = ImageTk.PhotoImage(Image.fromarray(rgb))
self.photo_image = photo
self.vid_canvas.delete("all")
self.vid_canvas.create_image((cw - nw) // 2, (ch - nh) // 2,
anchor="nw", image=photo)
def _seek_to(self, frame_idx: int):
frame_idx = max(0, min(int(frame_idx), max(0, self.total_frames - 1)))
if self.playing:
with self._seek_lock:
self._pending_seek = frame_idx
self.current_frame = frame_idx
self._refresh_ui()
else:
with self._seek_lock:
self.cap.set(cv2.CAP_PROP_POS_FRAMES, frame_idx)
self.current_frame = frame_idx
ret, frame = self.cap.read()
if ret:
if self.heatmap_on:
frame = self._apply_heatmap(frame)
self._show_frame(frame)
self._refresh_ui()
def _step_sec(self, seconds: float):
if not self.cap:
return
delta = int(seconds * self.fps)
was = self.playing
if was:
self._pause()
self._seek_to(self.current_frame + delta)
if was:
self._play()
# ──────────────────── Seekbar ─────────────────────────────────────
def _draw_seekbar(self):
c = self.seek_cv
w, h = c.winfo_width(), c.winfo_height()
if w < 2:
return
c.delete("all")
c.create_rectangle(0, 0, w, h, fill=C_SEEK_BG, outline="")
if self.total_frames > 0:
cur_frac = self.current_frame / self.total_frames
zoom_range = self.zoom_end - self.zoom_start
rel = ((cur_frac - self.zoom_start) / zoom_range
if zoom_range > 0 else 0.0)
rel = max(0.0, min(1.0, rel))
fill_x = int(w * rel)
c.create_rectangle(0, 0, fill_x, h, fill=C_SEEK_FILL, outline="")
r, cy = 7, h // 2
c.create_oval(fill_x-r, cy-r, fill_x+r, cy+r,
fill=C_ACCENT, outline="#ffffff", width=1)
def _seek_click(self, event):
if not self.cap:
return
w = self.seek_cv.winfo_width()
if w < 2:
return
rel = max(0.0, min(1.0, event.x / w))
abs_f = self.zoom_start + rel * (self.zoom_end - self.zoom_start)
self._seek_to(int(abs_f * self.total_frames))
# ──────────────────── Motion Graph ───────────────────────────────
def _draw_graph(self):
c = self.graph_cv
w = c.winfo_width()
h = c.winfo_height()
c.delete("all")
c.create_rectangle(0, 0, w, h, fill=C_BG_MID, outline="")
if len(self.graph_buckets) == 0:
msg = ("Analysing motion, please wait…"
if self.cap and not self.analysis_done
else "Open a video file to see motion graph")
c.create_text(w // 2, h // 2, text=msg,
fill=C_DIM, font=("Consolas", 9))
return
buckets = self.graph_buckets
n_buckets = len(buckets)
pad_t = 10
usable = h - pad_t - 6
i_start = int(self.zoom_start * n_buckets)
i_end = max(i_start + 1, int(math.ceil(self.zoom_end * n_buckets)))
i_start = max(0, min(i_start, n_buckets - 1))
i_end = max(0, min(i_end, n_buckets))
visible = buckets[i_start:i_end]
n_vis = len(visible)
if n_vis == 0:
return
if not self.analysis_done and self.scores_seen_frac > 0:
seen_n = int(n_buckets * self.scores_seen_frac)
sm = buckets[:max(1, seen_n)].max()
peak = sm if sm > 0 else 1.0
else:
peak = buckets.max() if buckets.max() > 0 else 1.0
bar_w = w / n_vis
for frac in (0.25, 0.5, 0.75):
y = pad_t + usable * (1 - frac)
c.create_line(0, y, w, y, fill=C_GRAPH_GRID, dash=(4, 8))
for j, score in enumerate(visible):
norm = score / peak
bar_h = max(1, int(usable * norm))
x0, x1 = j * bar_w, j * bar_w + bar_w - 0.5
y0, y1 = pad_t + usable - bar_h, pad_t + usable