-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtab_video.py
More file actions
1194 lines (974 loc) · 45.8 KB
/
tab_video.py
File metadata and controls
1194 lines (974 loc) · 45.8 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 os
import cv2
import time
import datetime
import numpy as np
import threading
import queue
from concurrent.futures import ThreadPoolExecutor
from PIL import Image
from PySide6.QtWidgets import (QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton,
QLineEdit, QProgressBar, QGroupBox, QMessageBox,
QSplitter, QCheckBox, QSpinBox, QDoubleSpinBox,
QSizePolicy, QFileDialog, QSlider, QStyle)
from PySide6.QtCore import Qt, QThread, Signal, QTimer, QMutex, QWaitCondition
from PySide6.QtGui import QImage, QPixmap, QDragEnterEvent, QDropEvent, QIcon, QKeySequence, QShortcut
from gui_widgets import ResizableImageLabel
from gui_model_manager import ModelManagerDialog
# --- HELPER: THREADED FRAME LOADING ---
class FrameLoader(threading.Thread):
def __init__(self, video_path, start_frame, end_frame, step, queue_obj):
super().__init__()
self.video_path = video_path
self.start_frame = start_frame
self.end_frame = end_frame
self.step = step
self.queue = queue_obj
self.is_running = True
self.daemon = True
def run(self):
cap = cv2.VideoCapture(self.video_path)
cap.set(cv2.CAP_PROP_POS_FRAMES, self.start_frame)
current_idx = self.start_frame
while current_idx <= self.end_frame and self.is_running:
ret, frame = cap.read()
if not ret: break
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
if self.is_running:
while self.is_running:
try:
self.queue.put((current_idx, frame_rgb), timeout=0.1)
break
except queue.Full:
continue
next_target = current_idx + self.step
frames_to_skip = self.step - 1
if frames_to_skip > 0:
if frames_to_skip < 100:
for _ in range(frames_to_skip):
if not self.is_running: break
cap.grab()
else:
cap.set(cv2.CAP_PROP_POS_FRAMES, next_target)
current_idx = next_target
cap.release()
if self.is_running:
self.queue.put(None)
def stop(self):
self.is_running = False
# Preview Worker With Playback
class PreviewWorker(QThread):
# Emits (Image, Frame Index, IsPlaying)
frame_ready = Signal(QImage, int)
playback_stopped = Signal() # Signal when video ends naturally
def __init__(self, video_path):
super().__init__()
self.video_path = video_path
self.fps = 30.0
self.is_running = True
# State
self.pending_seek = -1
self.is_playing = False
self.current_frame_idx = 0
# Synchronization
self.mutex = QMutex()
self.condition = QWaitCondition()
def request_seek(self, frame_idx):
"""Request a jump to a specific frame."""
self.mutex.lock()
self.pending_seek = frame_idx
# Usually, if user scrubs manually, we pause playback
self.is_playing = False
self.condition.wakeOne()
self.mutex.unlock()
def play(self):
"""Start continuous playback."""
self.mutex.lock()
self.is_playing = True
self.condition.wakeOne()
self.mutex.unlock()
def pause(self):
"""Pause playback."""
self.mutex.lock()
self.is_playing = False
self.mutex.unlock()
def run(self):
cap = cv2.VideoCapture(self.video_path)
self.fps = cap.get(cv2.CAP_PROP_FPS) or 30.0
frame_delay = 1.0 / self.fps
while self.is_running:
self.mutex.lock()
# If not playing and no seek request, sleep and wait
if not self.is_playing and self.pending_seek == -1:
self.condition.wait(self.mutex)
if not self.is_running:
self.mutex.unlock()
break
# Check flags
do_seek = self.pending_seek
do_play = self.is_playing
# Clear pending request
self.pending_seek = -1
self.mutex.unlock()
# --- EXECUTION (Outside Mutex) ---
# 1. Handle Seek (Priority)
if do_seek != -1:
cap.set(cv2.CAP_PROP_POS_FRAMES, do_seek)
self.current_frame_idx = do_seek
# 2. Read Frame
start_time = time.time()
ret, frame = cap.read()
if ret:
# Process Image
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
h, w, ch = frame_rgb.shape
# Copy is essential here
q_img = QImage(frame_rgb.data, w, h, ch * w, QImage.Format_RGB888).copy()
# Update index tracking
if not do_seek != -1: # If we didn't just seek, we advanced by 1
self.current_frame_idx = int(cap.get(cv2.CAP_PROP_POS_FRAMES)) - 1
self.frame_ready.emit(q_img, self.current_frame_idx)
else:
# End of video or error
if do_play:
self.playback_stopped.emit()
self.mutex.lock()
self.is_playing = False
self.mutex.unlock()
# 3. Handle Playback Timing
if do_play and not do_seek != -1:
# Sleep to match FPS
elapsed = time.time() - start_time
wait_time = max(0.001, frame_delay - elapsed)
time.sleep(wait_time)
cap.release()
def stop(self):
self.mutex.lock()
self.is_running = False
self.condition.wakeOne()
self.mutex.unlock()
self.wait()
# Helper: Async Saver
def save_image_task(img, path, quality=95):
try:
img.save(path, quality=quality)
except Exception as e:
print(f"Save error: {e}")
# Single Frame Grab Worker
class SingleFrameWorker(QThread):
log_msg = Signal(str)
status_update = Signal(str)
finished = Signal(bool, str) # success, message
def __init__(self, engine, video_path, frame_idx, output_folder, prompt, sam_settings, output_flags):
super().__init__()
self.engine = engine
self.video_path = video_path
self.frame_idx = frame_idx
self.output_folder = output_folder
self.prompt = prompt
self.sam_settings = sam_settings
self.output_flags = output_flags
def run(self):
try:
model_path = os.path.join(os.getcwd(), "models", "sam3")
if not self.engine.model:
self.log_msg.emit("⏳ Auto-loading SAM3 model...")
self.status_update.emit("⏳ Loading SAM3 model...")
success, msg = self.engine.load_model(model_path)
if not success:
self.log_msg.emit(f"❌ Model Load Failed: {msg}")
self.finished.emit(False, f"Model load failed: {msg}")
return
self.status_update.emit("Reading frame...")
cap = cv2.VideoCapture(self.video_path)
cap.set(cv2.CAP_PROP_POS_FRAMES, self.frame_idx)
ret, frame = cap.read()
cap.release()
if not ret:
self.finished.emit(False, "Failed to read frame from video.")
return
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
pil_img = Image.fromarray(frame_rgb)
self.status_update.emit("Generating mask...")
mask_img, msg = self.engine.generate_mask(
pil_img,
self.prompt,
max_dimension=self.sam_settings['max_res'],
conf_threshold=self.sam_settings['conf'],
expand_ratio=self.sam_settings['expand']
)
if not os.path.exists(self.output_folder):
os.makedirs(self.output_folder)
base_filename = os.path.splitext(os.path.basename(self.video_path))[0]
saved_files = []
if mask_img:
save_img = pil_img
save_mask = mask_img
suffix = ""
if self.sam_settings['crop']:
bbox = mask_img.getbbox()
if bbox:
save_img = pil_img.crop(bbox)
save_mask = mask_img.crop(bbox)
else:
suffix = "_empty"
frame_name = f"{base_filename}_frame_{self.frame_idx:06d}{suffix}.jpg"
mask_name = f"{base_filename}_frame_{self.frame_idx:06d}{suffix}-masklabel.png"
if self.output_flags['save_color']:
out_path = os.path.join(self.output_folder, frame_name)
save_img.save(out_path, quality=95)
saved_files.append(frame_name)
if self.output_flags['save_mask']:
out_path = os.path.join(self.output_folder, mask_name)
save_mask.save(out_path)
saved_files.append(mask_name)
self.log_msg.emit(f"📸 Grabbed frame {self.frame_idx}: {', '.join(saved_files)}")
self.finished.emit(True, f"Saved {len(saved_files)} file(s) for frame {self.frame_idx}")
else:
self.log_msg.emit(f"⚠️ No mask generated for frame {self.frame_idx} (nothing detected)")
self.finished.emit(True, f"No object detected in frame {self.frame_idx}")
except Exception as e:
self.log_msg.emit(f"❌ Grab frame error: {e}")
self.finished.emit(False, str(e))
# Main Worker
class VideoExtractWorker(QThread):
progress = Signal(int, int, float, int, int, int, int, str)
log_msg = Signal(str)
status_update = Signal(str)
preview_ready = Signal(object, object)
finished = Signal(int, int, float)
def __init__(self, engine, video_tasks, output_folder, prompt, step, sam_settings, output_flags):
super().__init__()
self.engine = engine
self.video_tasks = video_tasks
self.output_folder = output_folder
self.prompt = prompt
self.step = step
self.sam_settings = sam_settings
self.output_flags = output_flags
self.is_running = True
self.frame_queue = queue.Queue(maxsize=5)
self.loader = None
self.save_executor = None
def run(self):
saved_count = 0
scanned_count = 0
start_time_global = time.time()
try:
model_path = os.path.join(os.getcwd(), "models", "sam3")
if not self.engine.model:
self.log_msg.emit("⏳ Auto-loading SAM3 model...")
self.status_update.emit("⏳ Auto-loading SAM3 model...")
success, msg = self.engine.load_model(model_path)
if not success:
self.log_msg.emit(f"❌ Model Load Failed: {msg}")
return
if not os.path.exists(self.output_folder):
try:
os.makedirs(self.output_folder)
except Exception as e:
self.log_msg.emit(f"❌ Error creating output folder: {e}")
return
self.save_executor = ThreadPoolExecutor(max_workers=4)
num_videos = len(self.video_tasks)
for v_idx, task in enumerate(self.video_tasks):
if not self.is_running: break
video_path = task['path']
base_filename = os.path.splitext(os.path.basename(video_path))[0]
self.log_msg.emit(f"🎬 Starting video {v_idx+1}/{num_videos}: {base_filename}")
self.status_update.emit(f"Starting {base_filename}...")
cap_temp = cv2.VideoCapture(video_path)
total_frames_in_video = int(cap_temp.get(cv2.CAP_PROP_FRAME_COUNT))
cap_temp.release()
start_f = task['start']
end_f = task['end']
if end_f == -1 or end_f >= total_frames_in_video:
end_f = total_frames_in_video - 1
with self.frame_queue.mutex:
self.frame_queue.queue.clear()
self.loader = FrameLoader(video_path, start_f, end_f, self.step, self.frame_queue)
self.loader.start()
avg_speed = 0.0
alpha = 0.3
while self.is_running:
try:
item = self.frame_queue.get(timeout=0.1)
except queue.Empty:
if not self.loader.is_alive() and self.frame_queue.empty():
break
continue
if item is None: break
frame_idx, frame_rgb_np = item
t0 = time.time()
pil_img = Image.fromarray(frame_rgb_np)
scanned_count += 1
mask_img, msg = self.engine.generate_mask(
pil_img,
self.prompt,
max_dimension=self.sam_settings['max_res'],
conf_threshold=self.sam_settings['conf'],
expand_ratio=self.sam_settings['expand']
)
if mask_img:
save_img = pil_img
save_mask = mask_img
suffix = ""
if self.sam_settings['crop']:
bbox = mask_img.getbbox()
if bbox:
save_img = pil_img.crop(bbox)
save_mask = mask_img.crop(bbox)
else:
suffix = "_empty"
frame_name = f"{base_filename}_frame_{frame_idx:06d}{suffix}.jpg"
mask_name = f"{base_filename}_frame_{frame_idx:06d}{suffix}-masklabel.png"
out_path_img = os.path.join(self.output_folder, frame_name)
out_path_mask = os.path.join(self.output_folder, mask_name)
did_save = False
if self.output_flags['save_color']:
self.save_executor.submit(save_image_task, save_img.copy(), out_path_img)
did_save = True
if self.output_flags['save_mask']:
self.save_executor.submit(save_image_task, save_mask.copy(), out_path_mask)
did_save = True
if did_save:
saved_count += 1
self.preview_ready.emit(pil_img, mask_img)
t1 = time.time()
dt = t1 - t0
if avg_speed == 0: avg_speed = dt
else: avg_speed = (alpha * dt) + ((1 - alpha) * avg_speed)
self.progress.emit(frame_idx, total_frames_in_video, avg_speed, saved_count, scanned_count, v_idx + 1, num_videos, base_filename)
# Drain queue to ensure loader can exit if blocked
while not self.frame_queue.empty():
try:
self.frame_queue.get_nowait()
except queue.Empty:
break
if self.loader:
self.loader.stop()
self.loader.join()
except Exception as e:
self.log_msg.emit(f"❌ Critical Error in Video Worker: {e}")
import traceback
traceback.print_exc()
finally:
if self.save_executor:
self.save_executor.shutdown(wait=True)
total_time = time.time() - start_time_global
self.finished.emit(saved_count, scanned_count, total_time)
def stop(self):
self.is_running = False
if self.loader:
self.loader.stop()
class VideoTab(QWidget):
log_msg = Signal(str)
work_finished = Signal()
def __init__(self, sam_engine):
super().__init__()
self.sam_engine = sam_engine
self.video_path = ""
self.current_folder = ""
self.recursive = False
self.total_frames = 0
self.fps = 30.0
self.worker = None
self.preview_worker = None
self.video_loaded = False
self.is_batch_mode = False
self.is_playing_preview = False
self.setAcceptDrops(True)
self.setup_ui()
# Check status immediately
self.check_model_status()
def setup_ui(self):
main_layout = QVBoxLayout(self)
main_layout.setContentsMargins(5, 5, 5, 5)
self.content_layout = QHBoxLayout()
main_layout.addLayout(self.content_layout)
# Left Panel (Settings)
left_widget = QWidget()
left_widget.setMaximumWidth(350)
left_layout = QVBoxLayout(left_widget)
left_layout.setContentsMargins(0, 0, 5, 0)
settings_group = QGroupBox("Extraction Settings")
settings_layout = QVBoxLayout(settings_group)
settings_layout.addWidget(QLabel("Video File:"))
file_layout = QHBoxLayout()
self.btn_open = QPushButton("Open Video")
self.btn_open.clicked.connect(self.open_video)
self.btn_batch = QPushButton("Batch Process")
self.btn_batch.setStyleSheet("background-color: #5a3fa3; color: white;")
self.btn_batch.clicked.connect(self.batch_process_videos)
file_layout.addWidget(self.btn_open)
file_layout.addWidget(self.btn_batch)
settings_layout.addLayout(file_layout)
self.lbl_file = QLabel("No file loaded (Drag & Drop supported)")
self.lbl_file.setWordWrap(True)
self.lbl_file.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Minimum)
self.lbl_file.setStyleSheet("color: #888; font-size: 10px;")
settings_layout.addWidget(self.lbl_file)
settings_layout.addSpacing(10)
settings_layout.addWidget(QLabel("Prompt:"))
self.txt_prompt = QLineEdit()
self.txt_prompt.setText("person")
self.txt_prompt.setPlaceholderText("e.g. person, car")
settings_layout.addWidget(self.txt_prompt)
sam_grid = QHBoxLayout()
v1 = QVBoxLayout(); v1.addWidget(QLabel("Resolution:"))
self.spin_res = QSpinBox(); self.spin_res.setRange(256, 4096); self.spin_res.setValue(1024); self.spin_res.setSingleStep(128)
v1.addWidget(self.spin_res)
v2 = QVBoxLayout(); v2.addWidget(QLabel("Confidence:"))
self.spin_conf = QDoubleSpinBox(); self.spin_conf.setRange(0.01, 1.0); self.spin_conf.setValue(0.25); self.spin_conf.setSingleStep(0.05)
v2.addWidget(self.spin_conf)
v3 = QVBoxLayout(); v3.addWidget(QLabel("Expand Mask:"))
self.spin_expand = QDoubleSpinBox()
self.spin_expand.setRange(0.0, 50.0)
self.spin_expand.setSingleStep(0.5)
self.spin_expand.setValue(0.0)
self.spin_expand.setSuffix("%")
v3.addWidget(self.spin_expand)
sam_grid.addLayout(v1)
sam_grid.addLayout(v2)
sam_grid.addLayout(v3)
settings_layout.addLayout(sam_grid)
self.chk_crop = QCheckBox("Crop output to mask")
settings_layout.addWidget(self.chk_crop)
settings_layout.addSpacing(10)
range_group = QGroupBox("Frame Range (Single Video Only)")
range_layout = QVBoxLayout(range_group)
r1 = QHBoxLayout()
r1.addWidget(QLabel("Start:"))
self.spin_start = QSpinBox(); self.spin_start.setRange(0, 999999)
self.spin_start.valueChanged.connect(self.update_scan_estimate)
self.btn_set_start = QPushButton("Set Start")
self.btn_set_start.clicked.connect(lambda: self.spin_start.setValue(self.slider.value()))
r1.addWidget(self.spin_start); r1.addWidget(self.btn_set_start)
range_layout.addLayout(r1)
r2 = QHBoxLayout()
r2.addWidget(QLabel("End:"))
self.spin_end = QSpinBox(); self.spin_end.setRange(0, 999999)
self.spin_end.valueChanged.connect(self.update_scan_estimate)
self.btn_set_end = QPushButton("Set End")
self.btn_set_end.clicked.connect(lambda: self.spin_end.setValue(self.slider.value()))
r2.addWidget(self.spin_end); r2.addWidget(self.btn_set_end)
range_layout.addLayout(r2)
r3 = QHBoxLayout()
r3.addWidget(QLabel("Step:"))
self.spin_step = QSpinBox(); self.spin_step.setRange(1, 10000); self.spin_step.setValue(100)
self.spin_step.valueChanged.connect(self.update_scan_estimate)
r3.addWidget(self.spin_step)
range_layout.addLayout(r3)
self.lbl_scan_est = QLabel("Scan Count: 0 frames")
self.lbl_scan_est.setStyleSheet("color: #aaa; font-style: italic; margin-top: 5px;")
self.lbl_scan_est.setAlignment(Qt.AlignCenter)
range_layout.addWidget(self.lbl_scan_est)
settings_layout.addWidget(range_group)
out_group = QGroupBox("Output Files")
out_layout = QHBoxLayout(out_group)
self.chk_save_img = QCheckBox("Color")
self.chk_save_img.setChecked(True)
self.chk_save_mask = QCheckBox("Mask")
self.chk_save_mask.setChecked(True)
out_layout.addWidget(self.chk_save_img)
out_layout.addWidget(self.chk_save_mask)
settings_layout.addWidget(out_group)
settings_layout.addStretch()
left_layout.addWidget(settings_group)
# Right Panel (Preview)
right_widget = QWidget()
right_layout = QVBoxLayout(right_widget)
right_layout.setContentsMargins(5, 0, 0, 0)
self.lbl_preview = ResizableImageLabel()
self.lbl_preview.setText("No Video Loaded")
self.lbl_preview.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
right_layout.addWidget(self.lbl_preview, stretch=1)
# Playback Controls
slider_layout = QHBoxLayout()
# Play Button
self.btn_play_preview = QPushButton()
self.btn_play_preview.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
self.btn_play_preview.setFixedSize(30, 30)
self.btn_play_preview.setEnabled(False)
self.btn_play_preview.clicked.connect(self.toggle_playback)
slider_layout.addWidget(self.btn_play_preview)
# Grab Frame Button
self.btn_grab_frame = QPushButton("Grab Frame (G)")
self.btn_grab_frame.setFixedHeight(30)
self.btn_grab_frame.setEnabled(False)
self.btn_grab_frame.setStyleSheet("QPushButton { background-color: #d6a316; color: white; font-weight: bold; border-radius: 4px; padding: 0 10px; } QPushButton:hover:!disabled { background-color: #c49515; } QPushButton:disabled { background-color: #555; color: #888; }")
self.btn_grab_frame.clicked.connect(self.grab_current_frame)
self.shortcut_grab = QShortcut(QKeySequence("G"), self)
self.shortcut_grab.activated.connect(self.grab_current_frame)
slider_layout.addWidget(self.btn_grab_frame)
self.lbl_time = QLabel("00:00:00")
self.slider = QSlider(Qt.Horizontal)
self.slider.setEnabled(False)
self.slider.valueChanged.connect(self.on_slider_changed)
self.lbl_frame = QLabel("0 / 0")
slider_layout.addWidget(self.lbl_time)
slider_layout.addWidget(self.slider)
slider_layout.addWidget(self.lbl_frame)
right_layout.addLayout(slider_layout)
ctrl_layout = QHBoxLayout()
self.btn_process = QPushButton("START EXTRACTION")
self.btn_process.setEnabled(False)
self.btn_process.clicked.connect(self.on_click_process)
self.btn_stop = QPushButton("STOP")
self.btn_stop.clicked.connect(self.stop_processing)
self.btn_stop.setStyleSheet("""
QPushButton { background-color: #d73a49; color: white; font-weight: bold; padding: 10px; border-radius: 4px; }
QPushButton:disabled { background-color: #555; color: #888; }
QPushButton:hover:!disabled { background-color: #cb2431; }
""")
self.btn_stop.setEnabled(False)
ctrl_layout.addWidget(self.btn_process)
ctrl_layout.addWidget(self.btn_stop)
right_layout.addLayout(ctrl_layout)
self.progress = QProgressBar()
self.progress.setTextVisible(True)
self.progress.setAlignment(Qt.AlignCenter)
self.progress.setRange(0, 100)
self.progress.setValue(0)
self.progress.setFormat("Ready")
self.progress.setStyleSheet("""
QProgressBar {
text-align: center;
color: white;
font-weight: bold;
border: 1px solid #555;
border-radius: 4px;
background-color: #333;
}
QProgressBar::chunk {
background-color: #2da44e;
border-radius: 4px;
}
""")
right_layout.addWidget(self.progress)
self.content_layout.addWidget(left_widget, 0)
self.content_layout.addWidget(right_widget, 1)
def on_click_process(self):
"""Handler that decides action based on model status."""
model_path = os.path.join(os.getcwd(), "models", "sam3")
if os.path.exists(model_path) and os.path.isdir(model_path):
self.start_processing()
else:
self.open_model_manager()
def dragEnterEvent(self, event: QDragEnterEvent):
if event.mimeData().hasUrls():
url = event.mimeData().urls()[0]
file_path = url.toLocalFile()
ext = os.path.splitext(file_path)[1].lower()
if ext in ['.mp4', '.mkv', '.avi', '.mov', '.webm', '.flv']:
event.acceptProposedAction()
def dropEvent(self, event: QDropEvent):
urls = event.mimeData().urls()
if urls:
file_path = urls[0].toLocalFile()
self.load_video(file_path)
def get_btn_style(self, color):
base = "QPushButton { color: white; font-weight: bold; padding: 10px; border-radius: 4px; }"
disabled = "QPushButton:disabled { background-color: #555; color: #888; }"
if color == "green":
return base + "QPushButton { background-color: #2da44e; } QPushButton:hover:!disabled { background-color: #2c974b; } " + disabled
elif color == "blue":
return base + "QPushButton { background-color: #0366d6; } QPushButton:hover:!disabled { background-color: #005cc5; } " + disabled
return ""
def check_model_status(self):
"""Updates UI appearance only. Logic is handled in on_click_process."""
model_path = os.path.join(os.getcwd(), "models", "sam3")
is_installed = os.path.exists(model_path) and os.path.isdir(model_path)
if is_installed:
self.btn_process.setText("START EXTRACTION")
self.btn_process.setStyleSheet(self.get_btn_style("green"))
# Only enable if video is loaded
self.btn_process.setEnabled(self.video_loaded)
else:
self.btn_process.setText("Install SAM3 Model")
self.btn_process.setStyleSheet(self.get_btn_style("blue"))
self.btn_process.setEnabled(True) # Always enable the install button
def open_model_manager(self):
dlg = ModelManagerDialog(self)
dlg.exec()
self.check_model_status()
def get_settings(self):
return {
"video_path": self.video_path,
"prompt": self.txt_prompt.text(),
"res": self.spin_res.value(),
"conf": self.spin_conf.value(),
"expand": self.spin_expand.value(),
"crop": self.chk_crop.isChecked(),
"step": self.spin_step.value(),
"save_color": self.chk_save_img.isChecked(),
"save_mask": self.chk_save_mask.isChecked(),
"start_frame": self.spin_start.value(),
"end_frame": self.spin_end.value(),
}
def set_settings(self, s):
if not s: return
if "prompt" in s: self.txt_prompt.setText(s["prompt"])
if "res" in s: self.spin_res.setValue(s["res"])
if "conf" in s: self.spin_conf.setValue(s["conf"])
if "expand" in s: self.spin_expand.setValue(s["expand"])
if "crop" in s: self.chk_crop.setChecked(s["crop"])
if "step" in s: self.spin_step.setValue(s["step"])
if "save_color" in s: self.chk_save_img.setChecked(s["save_color"])
if "save_mask" in s: self.chk_save_mask.setChecked(s["save_mask"])
if "video_path" in s and s["video_path"] and os.path.exists(s["video_path"]):
self.load_video(s["video_path"])
if "start_frame" in s: self.spin_start.setValue(s["start_frame"])
if "end_frame" in s: self.spin_end.setValue(s["end_frame"])
self.update_scan_estimate()
def set_recursive(self, recursive):
self.recursive = recursive
def update_folder(self, folder, recursive=None):
self.current_folder = folder
if recursive is not None:
self.recursive = recursive
def open_video(self):
start_dir = os.path.dirname(self.video_path) if self.video_path else ""
path, _ = QFileDialog.getOpenFileName(self, "Open Video", start_dir, "Video Files (*.mp4 *.mkv *.avi *.mov *.webm)")
if path:
self.load_video(path)
def load_video(self, path):
if self.preview_worker:
self.preview_worker.stop()
self.preview_worker = None
cap = cv2.VideoCapture(path)
if not cap.isOpened():
return
self.video_path = path
self.total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
self.fps = cap.get(cv2.CAP_PROP_FPS) or 30.0
cap.release()
filename = os.path.basename(path)
# Insert zero-width spaces to allow wrapping on underscores, dots, etc.
display_name = filename.replace("_", "_\u200b").replace(".", ".\u200b").replace("-", "-\u200b")
self.lbl_file.setText(display_name)
self.slider.setRange(0, self.total_frames - 1)
self.slider.setValue(0)
self.slider.setEnabled(True)
self.btn_play_preview.setEnabled(True)
self.btn_grab_frame.setEnabled(True)
self.set_playback_ui(False)
self.spin_start.setRange(0, self.total_frames - 1)
self.spin_start.setValue(0)
self.spin_end.setRange(0, self.total_frames - 1)
self.spin_end.setValue(self.total_frames - 1)
self.update_scan_estimate()
self.video_loaded = True
# Re-check status to potentially enable the start button
self.check_model_status()
self.lbl_preview.setText("")
self.preview_worker = PreviewWorker(path)
self.preview_worker.frame_ready.connect(self.on_preview_frame_ready)
self.preview_worker.playback_stopped.connect(lambda: self.set_playback_ui(False))
self.preview_worker.start()
# Load first frame
self.preview_worker.request_seek(0)
def update_scan_estimate(self):
start = self.spin_start.value()
end = self.spin_end.value()
step = self.spin_step.value()
if step <= 0 or end < start: count = 0
else: count = (end - start) // step + 1
self.lbl_scan_est.setText(f"Scan Count: ~{count} frames")
# --- PLAYBACK LOGIC ---
def toggle_playback(self):
if not self.preview_worker: return
if self.is_playing_preview:
# Stop
self.preview_worker.pause()
self.set_playback_ui(False)
else:
# Start
self.preview_worker.play()
self.set_playback_ui(True)
def set_playback_ui(self, playing):
self.is_playing_preview = playing
icon = QStyle.SP_MediaPause if playing else QStyle.SP_MediaPlay
self.btn_play_preview.setIcon(self.style().standardIcon(icon))
def grab_current_frame(self):
# Ignore shortcut if a text input has focus (e.g. prompt field)
focused = self.focusWidget()
if isinstance(focused, (QLineEdit, QSpinBox, QDoubleSpinBox)):
return
if not self.video_path or not self.video_loaded:
return
prompt = self.txt_prompt.text().strip()
if not prompt:
QMessageBox.warning(self, "Missing Prompt", "Please enter a prompt (e.g., 'person').")
return
if not self.chk_save_img.isChecked() and not self.chk_save_mask.isChecked():
QMessageBox.warning(self, "No Output", "Select at least one output type (Color or Mask).")
return
out_folder = self.current_folder
if not out_folder or not os.path.exists(out_folder):
ret = QMessageBox.warning(self, "No Output Folder",
"The main output folder is not set.\nSave to video location instead?", QMessageBox.Yes | QMessageBox.No)
if ret == QMessageBox.Yes:
out_folder = os.path.dirname(self.video_path)
else:
return
# Pause preview
if self.preview_worker and self.is_playing_preview:
self.preview_worker.pause()
self.set_playback_ui(False)
frame_idx = self.slider.value()
sam_settings = {
'max_res': self.spin_res.value(),
'conf': self.spin_conf.value(),
'expand': self.spin_expand.value() / 100.0,
'crop': self.chk_crop.isChecked()
}
output_flags = {
'save_color': self.chk_save_img.isChecked(),
'save_mask': self.chk_save_mask.isChecked()
}
self.toggle_ui(False)
self.progress.setRange(0, 0) # indeterminate
self.progress.setFormat(f"Grabbing frame {frame_idx}...")
self.grab_worker = SingleFrameWorker(
self.sam_engine, self.video_path, frame_idx,
out_folder, prompt, sam_settings, output_flags
)
self.grab_worker.log_msg.connect(self.log_msg.emit)
self.grab_worker.status_update.connect(lambda s: self.progress.setFormat(s))
self.grab_worker.finished.connect(self.on_grab_finished)
self.grab_worker.start()
def on_grab_finished(self, success, message):
self.toggle_ui(True)
self.progress.setRange(0, 100)
if success:
self.progress.setValue(100)
self.progress.setFormat(f"Done: {message}")
else:
self.progress.setValue(0)
self.progress.setFormat(f"Error: {message}")
self.work_finished.emit()
def on_slider_changed(self):
frame = self.slider.value()
self.lbl_frame.setText(f"{frame} / {self.total_frames}")
seconds = frame / self.fps
self.lbl_time.setText(str(datetime.timedelta(seconds=int(seconds))))
# If the user drags the slider, we request a seek.
# However, if this change was triggered programmatically (by playback),
# we skip the request (handled in on_preview_frame_ready).
if self.preview_worker and not self.slider.signalsBlocked():
# If user scrubs, we pause playback to avoid conflict
if self.is_playing_preview:
self.set_playback_ui(False)
self.preview_worker.request_seek(frame)
def on_preview_frame_ready(self, q_img, frame_idx):
# Update Image
self.lbl_preview.current_pixmap = QPixmap.fromImage(q_img)
self.lbl_preview.update_view()
# Update Slider (Block signals to prevent seek-loop)
self.slider.blockSignals(True)
self.slider.setValue(frame_idx)
self.slider.blockSignals(False)
# Update Time/Text manually since signal didn't fire
self.lbl_frame.setText(f"{frame_idx} / {self.total_frames}")
seconds = frame_idx / self.fps
self.lbl_time.setText(str(datetime.timedelta(seconds=int(seconds))))
def closeEvent(self, event):
if self.preview_worker:
self.preview_worker.stop()
super().closeEvent(event)
def batch_process_videos(self):
start_dir = os.path.dirname(self.video_path) if self.video_path else ""
paths, _ = QFileDialog.getOpenFileNames(self, "Select Multiple Videos", start_dir, "Video Files (*.mp4 *.mkv *.avi *.mov *.webm)")
if not paths:
return
tasks = []
for p in paths:
tasks.append({
'path': p,
'start': 0,
'end': -1
})
out_folder = self.current_folder
if not out_folder or not os.path.exists(out_folder):
ret = QMessageBox.warning(self, "No Output Folder",
"The main output folder is not set.\nSave to the folder of the first video?", QMessageBox.Yes | QMessageBox.No)
if ret == QMessageBox.Yes:
out_folder = os.path.dirname(paths[0])
else:
return
self.is_batch_mode = True
self.launch_worker(tasks, out_folder)
def start_processing(self):
if not self.video_path: return
prompt = self.txt_prompt.text().strip()
if not prompt:
QMessageBox.warning(self, "Missing Prompt", "Please enter a prompt (e.g., 'person').")
return
out_folder = self.current_folder
if not out_folder or not os.path.exists(out_folder):
ret = QMessageBox.warning(self, "No Output Folder",
"The main output folder is not set.\nSave to video location instead?", QMessageBox.Yes | QMessageBox.No)
if ret == QMessageBox.Yes: out_folder = os.path.dirname(self.video_path)
else: return
tasks = [{
'path': self.video_path,