-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideo_input_processing.py
More file actions
816 lines (697 loc) · 39.2 KB
/
video_input_processing.py
File metadata and controls
816 lines (697 loc) · 39.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
# Part of Filuta AI KeyFramer
# Created by G. Michael Youngblood, Ph.D.
# This application is a tool for processing video input and generating keyframes.
#
# ©2026 Copyright Filuta AI, Inc. All rights reserved.
# Distributed under the MIT License. See LICENSE file in the project root for full license information.
import asyncio
import os
import threading
import time
from pathlib import Path
import numpy as np
from nicegui import context, events, ui
from keyframing import (
DEFAULT_MAX_PAIR_WORKERS,
DEFAULT_PAIR_BATCH_SIZE,
JobCancelledError as KeyframingCancelledError,
Keyframer,
auto_tune_parallel_settings,
)
from youtube import JobCancelledError as SegmentationCancelledError
from youtube import YouTubeVideoRetrieval
class VideoInputProcessing:
"""Build the NiceGUI workflow and coordinate long-running background jobs."""
def __init__(self):
self.video_filename = ''
self.audio_filename = ''
self.yt_processor = YouTubeVideoRetrieval()
self.mp4_files = []
self.path = os.path.expanduser('./library/')
self.directories = self.get_directories(self.path)
self.keyframer = None
self.selected_directory = None
self.threshold_ready = False
self.max_pair_workers = DEFAULT_MAX_PAIR_WORKERS
self.pair_batch_size = DEFAULT_PAIR_BATCH_SIZE
self.segment_cancel_event = None
self.paired_cancel_event = None
self.threshold_cancel_event = None
self.youtube_progress_state = self.make_progress_state('Downloaded', 0.15)
self.segment_progress_state = self.make_progress_state('Processed', 1 / 240.0)
self.paired_progress_state = self.make_progress_state('Compared', 1 / 90.0)
self.threshold_progress_state = self.make_progress_state('Evaluated', 1 / 45.0)
self.apply_auto_tuned_parallel_settings()
self.panel()
def make_progress_state(self, verb, ballpark_seconds_per_unit):
"""Create a small mutable state object shared by the worker thread and the UI."""
return {
'verb': verb,
'processed': 0,
'total': 0,
'started_at': None,
'finished': False,
'ballpark_seconds_per_unit': ballpark_seconds_per_unit,
}
def reset_progress_state(self, state):
state['processed'] = 0
state['total'] = 0
state['started_at'] = time.monotonic()
state['finished'] = False
def update_progress_state(self, state, processed, total):
state['processed'] = max(processed, 0)
state['total'] = max(total, 0)
def finish_progress_state(self, state):
state['finished'] = True
def format_eta(self, seconds):
if seconds is None:
return 'estimating...'
remaining = max(int(round(seconds)), 0)
minutes, seconds = divmod(remaining, 60)
hours, minutes = divmod(minutes, 60)
if hours:
return f'{hours:d}:{minutes:02d}:{seconds:02d}'
return f'{minutes:d}:{seconds:02d}'
def progress_metrics(self, state):
"""Turn raw progress counters into percent complete plus a rough ETA."""
total = state['total']
processed = min(state['processed'], total) if total else state['processed']
percent = 0.0 if total <= 0 else min(processed / total, 1.0) * 100.0
if total <= 0:
return percent, processed, total, 'estimating...', True
if processed >= total:
return percent, processed, total, '0:00', False
if processed <= 0 or state['started_at'] is None:
ballpark_seconds = total * state['ballpark_seconds_per_unit']
return percent, processed, total, f'~{self.format_eta(ballpark_seconds)}', True
elapsed = max(time.monotonic() - state['started_at'], 0.001)
rate = processed / elapsed
dynamic_eta = (total - processed) / rate if rate > 0 else None
# Early measurements are noisy, so show a ballpark ETA until enough work has completed.
warmup_cutoff = max(3, int(total * 0.03))
if processed < warmup_cutoff:
ballpark_seconds = total * state['ballpark_seconds_per_unit']
return percent, processed, total, f'~{self.format_eta(ballpark_seconds)}', True
return percent, processed, total, self.format_eta(dynamic_eta), False
def format_progress_amount(self, value, noun):
if noun == 'MB':
return f'{value:0.1f}'
return f'{int(round(value))}'
def progress_message(self, state, noun):
percent, processed, total, eta_text, _ = self.progress_metrics(state)
if total <= 0:
return f'{percent:0.1f}% | {state["verb"]} 0 of 0 {noun} | ETA {eta_text}'
processed_text = self.format_progress_amount(processed, noun)
total_text = self.format_progress_amount(total, noun)
return f'{percent:0.1f}% | {state["verb"]} {processed_text} of {total_text} {noun} | ETA {eta_text}'
def render_progress(self, state, progress_bar, progress_label, noun):
percent, processed, total, _, _ = self.progress_metrics(state)
progress_bar.style(f'width: {percent:.1f}%;')
progress_label.set_text(self.progress_message(state, noun))
async def track_progress(self, state, progress_bar, progress_label, noun):
while not state['finished']:
self.render_progress(state, progress_bar, progress_label, noun)
await asyncio.sleep(0.2)
self.render_progress(state, progress_bar, progress_label, noun)
def update_keyframing_buttons(self):
"""Keep the button states aligned with the current directory selection and running jobs."""
has_directory = bool(self.get_selected_directory())
paired_active = self.paired_cancel_event is not None
threshold_active = self.threshold_cancel_event is not None
segment_active = self.segment_cancel_event is not None
if hasattr(self, 'segment_button'):
self.segment_button.enabled = bool(self.video_filename) and not segment_active
self.segment_button.update()
if hasattr(self, 'paired_button'):
self.paired_button.enabled = has_directory and not paired_active and not threshold_active
self.paired_button.update()
if hasattr(self, 'threshold_button'):
self.threshold_button.enabled = has_directory and self.threshold_ready and not paired_active and not threshold_active
self.threshold_button.update()
def on_directory_change(self, event):
self.selected_directory = event.value or None
self.keyframer = None
self.threshold_ready = False
self.apply_auto_tuned_parallel_settings(self.selected_directory)
self.chart_placeholder.clear()
self.chart_info_1.style('display: none;')
self.chart_info_2.style('display: none;')
self.keyframe_info.style('display: none;')
self.focus_progress.style('display: none;')
self.focus_progress_text.style('display: none;')
self.threshold_progress_bar.style('display: none;')
self.threshold_progress_text.style('display: none;')
self.update_keyframing_buttons()
def on_parallel_setting_change(self, _event=None):
self.max_pair_workers = max(1, int(self.max_pair_workers_input.value or DEFAULT_MAX_PAIR_WORKERS))
self.pair_batch_size = max(1, int(self.pair_batch_size_input.value or DEFAULT_PAIR_BATCH_SIZE))
self.keyframer = None
def on_local_file_change(self, event):
has_selection = bool(event.value)
self.load_local_file_button.enabled = has_selection
self.load_local_file_button.update()
def notify_client(self, client, message, color=None):
with client:
ui.notify(message, color=color)
def get_selected_directory(self):
selected = self.focus_dropdown.value if hasattr(self, 'focus_dropdown') else None
if selected:
self.selected_directory = selected
return selected
return self.selected_directory
def estimate_frame_count_for_directory(self, directory=None):
if not directory:
return 0
directory_path = Path(self.path) / directory
if not directory_path.is_dir():
return 0
analysis_frames = list(directory_path.glob('*-analysis.jpg'))
if analysis_frames:
return len(analysis_frames)
full_frames = list(directory_path.glob('*-video.jpg'))
if full_frames:
return len(full_frames)
return len(list(directory_path.glob('*.jpg')))
def apply_auto_tuned_parallel_settings(self, directory=None):
"""Choose sensible default worker settings, but still let the user override them later."""
frame_count = self.estimate_frame_count_for_directory(directory or self.selected_directory)
self.max_pair_workers, self.pair_batch_size = auto_tune_parallel_settings(frame_count)
self.keyframer = None
if hasattr(self, 'max_pair_workers_input'):
self.max_pair_workers_input.value = self.max_pair_workers
self.max_pair_workers_input.update()
if hasattr(self, 'pair_batch_size_input'):
self.pair_batch_size_input.value = self.pair_batch_size
self.pair_batch_size_input.update()
def begin_job(self, event_attr, cancel_button):
"""Register a cancellable background job and expose its cancel button in the UI."""
cancel_event = threading.Event()
setattr(self, event_attr, cancel_event)
cancel_button.style('display: inline-flex;')
cancel_button.enabled = True
cancel_button.update()
self.update_keyframing_buttons()
return cancel_event
def end_job(self, event_attr, cancel_button):
setattr(self, event_attr, None)
cancel_button.style('display: none;')
cancel_button.enabled = False
cancel_button.update()
self.update_keyframing_buttons()
def request_cancel(self, event_attr, cancel_button, label):
cancel_event = getattr(self, event_attr)
if cancel_event is None or cancel_event.is_set():
return
cancel_event.set()
cancel_button.enabled = False
cancel_button.update()
ui.notify(f'Cancelling {label}...')
def cancel_segment(self):
self.request_cancel('segment_cancel_event', self.segment_cancel_button, 'segmentation')
def cancel_paired(self):
self.request_cancel('paired_cancel_event', self.paired_cancel_button, 'sequential difference evaluation')
def cancel_threshold(self):
self.request_cancel('threshold_cancel_event', self.threshold_cancel_button, 'keyframe creation')
def ensure_keyframer(self):
selected_directory = self.get_selected_directory()
if not selected_directory:
return None
requested_workers = int(self.max_pair_workers_input.value or self.max_pair_workers)
requested_batch_size = int(self.pair_batch_size_input.value or self.pair_batch_size)
if (
self.keyframer is None
or self.keyframer.selected_directory != selected_directory
or self.keyframer.max_pair_workers != requested_workers
or self.keyframer.pair_batch_size != requested_batch_size
):
self.max_pair_workers = requested_workers
self.pair_batch_size = requested_batch_size
self.keyframer = Keyframer(
selected_directory,
max_pair_workers=requested_workers,
pair_batch_size=requested_batch_size,
)
return self.keyframer
async def fetch_video(self):
"""Download a YouTube video without blocking the NiceGUI event loop."""
client = context.client
self.notify_client(client, 'Fetching video...')
self.fetch_youtube_button.enabled = False
self.fetch_youtube_button.update()
self.youtube_progress_text.style('display: block;')
self.youtube_progress.style('display: block;')
self.reset_progress_state(self.youtube_progress_state)
self.render_progress(self.youtube_progress_state, self.youtube_progress, self.youtube_progress_text, 'MB')
# Convert bytes into megabytes so the UI message is more readable for people.
def progress_callback(downloaded_bytes, total_bytes):
mb = 1024 * 1024
downloaded_mb = downloaded_bytes / mb if downloaded_bytes else 0.0
total_mb = total_bytes / mb if total_bytes else 0.0
self.update_progress_state(self.youtube_progress_state, downloaded_mb, total_mb)
progress_task = asyncio.create_task(
self.track_progress(self.youtube_progress_state, self.youtube_progress, self.youtube_progress_text, 'MB')
)
try:
await asyncio.sleep(0.5)
loop = asyncio.get_running_loop()
self.video_filename, self.audio_filename = await loop.run_in_executor(
None,
lambda: self.yt_processor.load_video(self.yt_url.value, self.yt_rename.value, progress_callback),
)
await asyncio.sleep(0.5)
print(f"Just got {self.video_filename} and {self.audio_filename}\n")
self.v_file_label.set_text(self.video_filename)
self.update_keyframing_buttons()
self.notify_client(client, 'Video retrieval task finished')
except Exception as exc:
self.notify_client(client, f'Video retrieval failed: {exc}', color='red')
finally:
self.finish_progress_state(self.youtube_progress_state)
await progress_task
self.fetch_youtube_button.enabled = True
self.fetch_youtube_button.update()
async def fetch_local_video(self):
client = context.client
selected_video = self.mp4_dropdown.value
if not selected_video:
self.notify_client(client, 'Select a local video first.', color='red')
return
self.notify_client(client, 'Fetching local video...')
self.lf_spinner.style('display: block;')
self.video_filename = selected_video
self.v_file_label.set_text(self.video_filename)
print(f"Locally assigned: {self.video_filename}")
await asyncio.sleep(0.5)
loop = asyncio.get_running_loop()
await loop.run_in_executor(None, self.yt_processor.load_local_video, self.video_filename)
await asyncio.sleep(0.5)
self.lf_spinner.style('display: none;')
self.update_keyframing_buttons()
self.notify_client(client, 'Video retrieval task finished')
async def segment_video(self):
"""Run segmentation in a worker thread and keep the progress bar updated live."""
client = context.client
if not self.video_filename:
self.notify_client(client, 'Load or upload a video before segmenting.', color='red')
return
# Always sync the processor with the last video source the user picked in the UI.
self.yt_processor.load_local_video(self.video_filename)
self.notify_client(client, 'Segmenting media...')
self.segment_progress.style('display: block;')
self.segment_progress_text.style('display: block;')
self.segment_info.style('display: none;')
self.reset_progress_state(self.segment_progress_state)
self.render_progress(self.segment_progress_state, self.segment_progress, self.segment_progress_text, 'segmentation steps')
cancel_event = self.begin_job('segment_cancel_event', self.segment_cancel_button)
def progress_callback(processed, total):
self.update_progress_state(self.segment_progress_state, processed, total)
progress_task = asyncio.create_task(
self.track_progress(self.segment_progress_state, self.segment_progress, self.segment_progress_text, 'segmentation steps')
)
try:
await asyncio.sleep(0.5)
loop = asyncio.get_running_loop()
# run_in_executor keeps CPU-heavy work off the main async thread so the UI stays responsive.
result = await loop.run_in_executor(
None,
lambda: self.yt_processor.segment_video(
self.video_freq.value,
progress_callback=progress_callback,
cancel_event=cancel_event,
reuse_existing=bool(self.reuse_segmentation_checkbox.value),
),
)
await asyncio.sleep(0.5)
frame_count = result['frame_count']
segmented_directory = result['directory']
self.refresh_directories()
if segmented_directory in self.directories:
self.selected_directory = segmented_directory
self.focus_dropdown.value = segmented_directory
self.focus_dropdown.update()
self.apply_auto_tuned_parallel_settings(segmented_directory)
self.threshold_ready = False
self.update_keyframing_buttons()
self.segment_info.style('display: block;')
if result['reused']:
analysis_note = '' if result['has_analysis_frames'] else ' Existing frames are missing some 640x360 analysis images, so re-segmentation may still be useful.'
self.segment_info.set_text(f'Reused existing segmentation with {frame_count} frames from {segmented_directory}.{analysis_note}')
self.notify_client(client, 'Existing segmentation reused')
else:
method_label = result.get('method', 'opencv')
self.segment_info.set_text(f'Video segmented into {frame_count} frames using {method_label}')
self.notify_client(client, 'Media segmentation task finished')
except SegmentationCancelledError:
self.segment_info.style('display: block;')
self.segment_info.set_text('Segmentation cancelled before completion.')
self.notify_client(client, 'Segmentation cancelled', color='warning')
except Exception as exc:
self.notify_client(client, f'Segmentation failed: {exc}', color='red')
finally:
self.finish_progress_state(self.segment_progress_state)
await progress_task
self.end_job('segment_cancel_event', self.segment_cancel_button)
def load_keyframe_pairs(self):
"""Load cached paired results only when the keyframer says they still match this directory."""
try:
keyframer = self.ensure_keyframer()
if keyframer is None:
return False
keypairs = keyframer.load_cached_paired_results()
if keypairs is None:
ui.notify('No current paired keyframes associated with the selected images', color='red')
return False
print(f'Loaded {len(keypairs)} paired keyframes')
except Exception:
ui.notify('No current paired keyframes associated with the selected images', color='red')
return False
deltas = [pair[2] for pair in keypairs]
data_median = np.median(deltas)
data_mean = np.mean(deltas)
data_stddev = np.std(deltas)
data_len = len(deltas)
data_info_1 = f'Stats: median = {data_median:.5f}, mean = {data_mean:.5f}, std dev = {data_stddev:.5f} from {data_len}'
data_info_2 = f' (mean - std dev) = {(data_mean - data_stddev):.5f}, (mean - 1/2 std dev) = {(data_mean - 0.5 * data_stddev):.5f} (default)'
self.threshold.set_value(data_mean - 0.5 * data_stddev)
print(data_info_1)
self.chart_info_1.style('display: block;')
self.chart_info_1.set_text(data_info_1)
self.chart_info_2.style('display: block;')
self.chart_info_2.set_text(data_info_2)
self.chart_placeholder.clear()
with self.chart_placeholder:
ui.highchart({
'title': {'text': 'Histogram of Sequential Image Differences'},
'xAxis': [
{'title': {'text': 'Data'}, 'alignTicks': False},
{'title': {'text': 'Histogram'}, 'alignTicks': False, 'opposite': True},
],
'yAxis': [
{'title': {'text': 'Data'}},
{'title': {'text': 'Histogram'}, 'opposite': True},
],
'plotOptions': {
'histogram': {
'accessibility': {
'point': {'valueDescriptionFormat': '{index}. {point.x:.3f} to {point.x2:.3f}, {point.y}.'},
},
},
},
'series': [
{
'name': 'Histogram',
'type': 'histogram',
'xAxis': 1,
'yAxis': 1,
'baseSeries': 's1',
'zIndex': -1,
},
{
'name': 'Structural Similarity Indexes',
'type': 'scatter',
'data': deltas,
'id': 's1',
'marker': {'radius': 1.5},
},
],
}, extras=['histogram-bellcurve'])
self.threshold_ready = True
self.update_keyframing_buttons()
self.focus_progress.style('display: none;')
self.focus_progress_text.style('display: none;')
return True
async def keyframe_pairs(self):
"""Compute or reuse adjacent-frame similarities, then rebuild the histogram UI."""
client = context.client
keyframer = self.ensure_keyframer()
if keyframer is None:
self.notify_client(client, 'Select a segmented directory first.', color='red')
return
if self.load_keyframe_pairs():
return
self.notify_client(client, 'Pairwise sequential frame comparisons...')
self.focus_progress.style('display: block;')
self.focus_progress_text.style('display: block;')
self.reset_progress_state(self.paired_progress_state)
self.render_progress(self.paired_progress_state, self.focus_progress, self.focus_progress_text, 'frame pairs')
cancel_event = self.begin_job('paired_cancel_event', self.paired_cancel_button)
def progress_callback(processed, total):
self.update_progress_state(self.paired_progress_state, processed, total)
progress_task = asyncio.create_task(
self.track_progress(self.paired_progress_state, self.focus_progress, self.focus_progress_text, 'frame pairs')
)
try:
await asyncio.sleep(0.5)
loop = asyncio.get_running_loop()
keypairs = await loop.run_in_executor(None, lambda: keyframer.keyframe_paired(progress_callback, cancel_event))
await asyncio.sleep(0.5)
except KeyframingCancelledError:
self.threshold_ready = False
self.notify_client(client, 'Sequential difference evaluation cancelled', color='warning')
return
except Exception as exc:
self.threshold_ready = False
self.notify_client(client, f'Sequential difference evaluation failed: {exc}', color='red')
return
finally:
self.finish_progress_state(self.paired_progress_state)
await progress_task
self.end_job('paired_cancel_event', self.paired_cancel_button)
deltas = [pair[2] for pair in keypairs]
data_median = np.median(deltas)
data_mean = np.mean(deltas)
data_stddev = np.std(deltas)
data_len = len(deltas)
data_info_1 = f'Stats: median = {data_median:.5f}, mean = {data_mean:.5f}, std dev = {data_stddev:.5f} from {data_len}'
data_info_2 = f' (mean - std dev) = {(data_mean - data_stddev):.5f}, (mean - 1/2 std dev) = {(data_mean - 0.5 * data_stddev):.5f} (default)'
self.threshold.set_value(data_mean - 0.5 * data_stddev)
print(data_info_1)
self.chart_info_1.style('display: block;')
self.chart_info_1.set_text(data_info_1)
self.chart_info_2.style('display: block;')
self.chart_info_2.set_text(data_info_2)
self.chart_placeholder.clear()
with self.chart_placeholder:
ui.highchart({
'title': {'text': 'Histogram of Sequential Image Differences'},
'xAxis': [
{'title': {'text': 'Data'}, 'alignTicks': False},
{'title': {'text': 'Histogram'}, 'alignTicks': False, 'opposite': True},
],
'yAxis': [
{'title': {'text': 'Data'}},
{'title': {'text': 'Histogram'}, 'opposite': True},
],
'plotOptions': {
'histogram': {
'accessibility': {
'point': {'valueDescriptionFormat': '{index}. {point.x:.3f} to {point.x2:.3f}, {point.y}.'},
},
},
},
'series': [
{
'name': 'Histogram',
'type': 'histogram',
'xAxis': 1,
'yAxis': 1,
'baseSeries': 's1',
'zIndex': -1,
},
{
'name': 'Structural Similarity Indexes',
'type': 'scatter',
'data': deltas,
'id': 's1',
'marker': {'radius': 1.5},
},
],
}, extras=['histogram-bellcurve'])
self.threshold_ready = True
self.update_keyframing_buttons()
self.notify_client(client, 'Paired frame comparison finished')
async def keyframe_threshold(self):
"""Create threshold keyframes and summarize how many frames were selected."""
client = context.client
keyframer = self.ensure_keyframer()
if keyframer is None:
self.notify_client(client, 'Select a segmented directory first.', color='red')
return
self.notify_client(client, 'Keyframe threshold frame comparisons...')
self.threshold_progress_bar.style('display: block;')
self.threshold_progress_text.style('display: block;')
self.keyframe_info.style('display: none;')
self.reset_progress_state(self.threshold_progress_state)
self.render_progress(self.threshold_progress_state, self.threshold_progress_bar, self.threshold_progress_text, 'frames')
cancel_event = self.begin_job('threshold_cancel_event', self.threshold_cancel_button)
def progress_callback(processed, total):
self.update_progress_state(self.threshold_progress_state, processed, total)
progress_task = asyncio.create_task(
self.track_progress(self.threshold_progress_state, self.threshold_progress_bar, self.threshold_progress_text, 'frames')
)
try:
await asyncio.sleep(0.5)
loop = asyncio.get_running_loop()
data, filename = await loop.run_in_executor(
None,
lambda: keyframer.keyframe_threshold(self.threshold.value, progress_callback, cancel_event),
)
await asyncio.sleep(0.5)
except KeyframingCancelledError:
self.notify_client(client, 'Keyframe creation cancelled', color='warning')
return
except Exception as exc:
self.notify_client(client, f'Keyframe creation failed: {exc}', color='red')
return
finally:
self.finish_progress_state(self.threshold_progress_state)
await progress_task
self.end_job('threshold_cancel_event', self.threshold_cancel_button)
total_frames = keyframer.get_total_frames()
# The saved transition list stores jumps, so we rebuild the distinct frame count for the summary text.
selected_indices = {0} if total_frames else set()
for start_index, next_index, *_rest in data:
selected_indices.add(start_index)
selected_indices.add(next_index)
selected_keyframes = len(selected_indices)
selected_percent = 0.0 if total_frames <= 0 else (selected_keyframes / total_frames) * 100.0
self.keyframe_info.style('display: block;')
self.keyframe_info.set_text(
f'{selected_keyframes} selected keyframes from {total_frames} frames ({selected_percent:.1f}%) | {len(data)} transitions saved to {filename}'
)
self.update_keyframing_buttons()
self.notify_client(client, 'Threshold keyframes finished')
def get_mp4_files(self):
files = os.listdir(os.path.expanduser('./'))
video_files = [name for name in files if os.path.isfile(name) and name.endswith(('.mp4', '.mkv', '.webm'))]
self.mp4_files = sorted(video_files)
self.mp4_dropdown.options = self.mp4_files
if self.mp4_dropdown.value not in self.mp4_files:
self.mp4_dropdown.value = None
self.mp4_dropdown.update()
if hasattr(self, 'load_local_file_button'):
self.load_local_file_button.enabled = bool(self.mp4_dropdown.value)
self.load_local_file_button.update()
def get_directories(self, path):
if not os.path.isdir(path):
return []
return sorted(name for name in os.listdir(path) if os.path.isdir(os.path.join(path, name)))
def refresh_directories(self):
"""Refresh the segmented-directory dropdown while preserving selection when possible."""
current_selection = self.get_selected_directory()
self.directories = self.get_directories(self.path)
self.focus_dropdown.options = self.directories
if current_selection in self.directories:
self.focus_dropdown.value = current_selection
self.selected_directory = current_selection
self.apply_auto_tuned_parallel_settings(current_selection)
else:
self.focus_dropdown.value = None
self.selected_directory = None
self.threshold_ready = False
self.apply_auto_tuned_parallel_settings()
self.focus_dropdown.update()
self.keyframer = None
self.update_keyframing_buttons()
def panel(self):
"""Assemble the three-step NiceGUI workflow shown in the app."""
with ui.stepper().props('vertical').classes('w-full') as stepper:
with ui.step('Specify video to load using one of these 3 methods'):
ui.label('1. Load from local file or from YouTube').classes('text-xl')
with ui.column():
with ui.row():
self.yt_url = ui.input(label='YouTube URL', placeholder='https://www.youtube.com/watch?v=juVKSx7qw24').style('width: 600px;')
self.yt_rename = ui.input(label='Rename to ', placeholder='Witcher-Demo').style('width: 600px;')
with ui.row():
self.fetch_youtube_button = ui.button('Retrieve YouTube Video', on_click=self.fetch_video, color='red')
self.youtube_progress_text = ui.label('').style('display: none; color: #b91c1c;')
self.youtube_progress = ui.html('<div style="width:0%;height:100%;background:#dc2626;border-radius:999px;transition:width 0.2s ease;box-shadow:inset 0 0 0 1px rgba(255,255,255,0.18);"></div>').style('width: 400px; height: 14px; background: #fee2e2; border-radius: 999px; overflow: hidden; display: none; border: 1px solid #fca5a5;')
ui.space()
ui.separator()
ui.space()
ui.label('2. Load video from local directory').classes('text-xl')
with ui.row():
ui.button('♻️', on_click=self.get_mp4_files).classes('text-xl')
self.mp4_dropdown = ui.select(options=self.mp4_files, label='Select a file', on_change=self.on_local_file_change).style('width: 400px;')
self.get_mp4_files()
with ui.row():
self.load_local_file_button = ui.button('Load Video from Local File', on_click=self.fetch_local_video)
self.load_local_file_button.enabled = False
self.lf_spinner = ui.spinner().props('size=lg').style('display: none;')
ui.space()
ui.separator()
ui.space()
ui.label('3. Bring your own file').classes('text-xl')
ui.label('Upload mp4, mkv, or webm file from your computer')
async def handle_upload(event: events.UploadEventArguments):
client = context.client
uploaded_file = event.file
await uploaded_file.save(uploaded_file.name)
print(f'Uploaded file: {uploaded_file.name}, Size: {uploaded_file.size()} bytes')
print(f"File '{uploaded_file.name}' saved successfully.")
self.video_filename = uploaded_file.name
self.v_file_label.set_text(self.video_filename)
self.get_mp4_files()
await asyncio.sleep(0.5)
loop = asyncio.get_running_loop()
await loop.run_in_executor(None, self.yt_processor.load_local_video, self.video_filename)
await asyncio.sleep(0.5)
self.update_keyframing_buttons()
self.notify_client(client, 'Uploaded file ready')
ui.upload(on_upload=handle_upload).props('accept=.mp4,.MP4,.mkv,.MKV,.webm,.WEBM')
with ui.stepper_navigation():
ui.button('Next', on_click=stepper.next)
with ui.step('Segment media into segments'):
ui.label('Specify frequency of the segmentation').classes('text-xl')
with ui.row():
ui.label('File in focus: ')
self.v_file_label = ui.label()
with ui.column().style('width: 1000px'):
with ui.row():
ui.label('Video Frequency')
self.video_freq = ui.select([10, 24, 30, 60, 90, 120], value=10)
self.reuse_segmentation_checkbox = ui.checkbox('Reuse existing segmentation if present', value=True)
ui.label('When enabled, KeyFramer will reuse an existing segmented folder for the current video instead of regenerating frames.').style('width: 900px;')
with ui.row():
self.segment_button = ui.button('Segment Media', on_click=self.segment_video)
self.segment_cancel_button = ui.button('Cancel', on_click=self.cancel_segment).props('outline color=negative').style('display: none;')
self.segment_progress_text = ui.label('').style('display: none;')
self.segment_progress = ui.html('<div style="width:0%;height:100%;background:#1976d2;border-radius:999px;transition:width 0.2s ease;box-shadow:inset 0 0 0 1px rgba(255,255,255,0.18);"></div>').style('width: 400px; height: 14px; background: #dbeafe; border-radius: 999px; overflow: hidden; display: none; border: 1px solid #93c5fd;')
self.segment_info = ui.label('').style('display: none;')
with ui.stepper_navigation():
ui.button('Next', on_click=stepper.next)
ui.button('Back', on_click=stepper.previous).props('flat')
with ui.step('Keyframing'):
ui.label('Evaluate image to image differential and determine threshold for keyframing (data will show up below this line)').classes('text-xl')
with ui.column().style('width: 1000px'):
ui.label('1. Start by examining the sequential differences in the images')
with ui.row():
ui.button('♻️', on_click=self.refresh_directories).classes('text-xl')
self.focus_dropdown = ui.select(options=self.directories, label='Select a directory', on_change=self.on_directory_change).style('width: 400px;')
ui.label('Pair Workers controls how many CPU processes evaluate adjacent frame pairs in parallel. Batch Size controls how many frame pairs each worker processes at a time before returning results. KeyFramer auto-fills these based on your CPU and the selected directory, and you can still override them at any time.').style('width: 900px;')
with ui.row():
self.max_pair_workers_input = ui.number(label='Pair Workers', value=self.max_pair_workers, min=1, step=1, format='%.0f', on_change=self.on_parallel_setting_change).style('width: 120px;')
self.pair_batch_size_input = ui.number(label='Batch Size', value=self.pair_batch_size, min=1, step=1, format='%.0f', on_change=self.on_parallel_setting_change).style('width: 120px;')
self.paired_button = ui.button('Evaluate sequential differences', on_click=self.keyframe_pairs)
self.paired_cancel_button = ui.button('Cancel', on_click=self.cancel_paired).props('outline color=negative').style('display: none;')
self.focus_progress_text = ui.label('').style('display: none;')
self.focus_progress = ui.html('<div style="width:0%;height:100%;background:#1976d2;border-radius:999px;transition:width 0.2s ease;box-shadow:inset 0 0 0 1px rgba(255,255,255,0.18);"></div>').style('width: 400px; height: 14px; background: #dbeafe; border-radius: 999px; overflow: hidden; display: none; border: 1px solid #93c5fd;')
ui.label('2. Evaluate the sequential data to determine the threshold for keyframing difference')
with ui.row():
with ui.row() as self.chart_placeholder:
pass
with ui.column():
self.chart_info_1 = ui.label('').style('display: none;')
self.chart_info_2 = ui.label('').style('display: none;')
ui.label('3. Enter the threshold value (0.0-1.0) and then calculate keyframes. Higher values indicate greater similarity and more keyframes.')
with ui.row():
self.threshold = ui.number(label='Threshold', value=0.5, format='%.5f').style('width: 75px;')
self.threshold_button = ui.button('Create Keyframes', on_click=self.keyframe_threshold)
self.threshold_cancel_button = ui.button('Cancel', on_click=self.cancel_threshold).props('outline color=negative').style('display: none;')
self.threshold_progress_text = ui.label('').style('display: none;')
self.threshold_progress_bar = ui.html('<div style="width:0%;height:100%;background:#1976d2;border-radius:999px;transition:width 0.2s ease;box-shadow:inset 0 0 0 1px rgba(255,255,255,0.18);"></div>').style('width: 400px; height: 14px; background: #dbeafe; border-radius: 999px; overflow: hidden; display: none; border: 1px solid #93c5fd;')
self.keyframe_info = ui.label('').style('display: none;')
self.update_keyframing_buttons()
with ui.stepper_navigation():
ui.button('Done', on_click=lambda: ui.notify('Yay!', type='positive'))
ui.button('Back', on_click=stepper.previous).props('flat')