MogensR commited on
Commit
57eb798
Β·
1 Parent(s): 5d49a4b

Update ui/ui_components.py

Browse files
Files changed (1) hide show
  1. ui/ui_components.py +245 -578
ui/ui_components.py CHANGED
@@ -1,598 +1,265 @@
1
  #!/usr/bin/env python3
2
  """
3
- BackgroundFX Pro – Main Application Entry Point
4
- Refactored modular architecture – orchestrates specialised components
5
  """
6
- from __future__ import annotations
7
- # ── Early env/threading hygiene (safe default to silence libgomp) ────────────
8
  import os
9
- os.environ["OMP_NUM_THREADS"] = "2" # Force valid value early
10
- # If you use early_env in your project, keep this import (harmless if absent)
11
- try:
12
- import early_env # sets OMP/MKL/OPENBLAS + torch threads safely
13
- except Exception:
14
- pass
15
- import logging
16
- import threading
17
- import traceback
18
- import sys
19
- import time
20
- from pathlib import Path
21
- from typing import Optional, Tuple, Dict, Any, Callable
22
- # Mitigate CUDA fragmentation (must be set before importing torch)
23
- if "PYTORCH_CUDA_ALLOC_CONF" not in os.environ:
24
- os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True,max_split_size_mb:128"
25
- # ── Logging ──────────────────────────────────────────────────────────────────
26
- logging.basicConfig(
27
- level=logging.INFO,
28
- format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
29
- )
30
- logger = logging.getLogger("core.app")
31
- # ── Ensure project root importable ───────────────────────────────────────────
32
- PROJECT_FILE = Path(__file__).resolve()
33
- CORE_DIR = PROJECT_FILE.parent
34
- ROOT = CORE_DIR.parent
35
- if str(ROOT) not in sys.path:
36
- sys.path.insert(0, str(ROOT))
37
- # Create loader directories if they don't exist
38
- loaders_dir = ROOT / "models" / "loaders"
39
- loaders_dir.mkdir(parents=True, exist_ok=True)
40
- # ── Gradio schema patch (HF quirk) ───────────────────────────────────────────
41
- try:
42
- import gradio_client.utils as gc_utils
43
- _orig_get_type = gc_utils.get_type
44
- def _patched_get_type(schema):
45
- if not isinstance(schema, dict):
46
- if isinstance(schema, bool): return "boolean"
47
- if isinstance(schema, str): return "string"
48
- if isinstance(schema, (int, float)): return "number"
49
- return "string"
50
- return _orig_get_type(schema)
51
- gc_utils.get_type = _patched_get_type
52
- logger.info("Gradio schema patch applied")
53
- except Exception as e:
54
- logger.warning(f"Gradio patch failed: {e}")
55
- # ── Core config + components ─────────────────────────────────────────────────
56
- try:
57
- from config.app_config import get_config
58
- except ImportError:
59
- # Dummy if missing
60
- class DummyConfig:
61
- def to_dict(self):
62
- return {}
63
- get_config = lambda: DummyConfig()
64
- from utils.hardware.device_manager import DeviceManager
65
- from utils.system.memory_manager import MemoryManager
66
- # Try to import the new split loaders first, fall back to old if needed
67
- try:
68
- from models.loaders.model_loader import ModelLoader
69
- logger.info("Using split loader architecture")
70
- except ImportError:
71
- logger.warning("Split loaders not found, using legacy loader")
72
- # Fall back to old loader if split architecture isn't available yet
73
- from models.model_loader import ModelLoader # type: ignore
74
- from processing.video.video_processor import CoreVideoProcessor
75
- from processing.audio.audio_processor import AudioProcessor
76
- from utils.monitoring.progress_tracker import ProgressTracker
77
- from utils.cv_processing import validate_video_file
78
- # ── Optional Two-Stage import ────────────────────────────────────────────────
79
- TWO_STAGE_AVAILABLE = False
80
- TWO_STAGE_IMPORT_ORIGIN = ""
81
- TWO_STAGE_IMPORT_ERROR = ""
82
- CHROMA_PRESETS: Dict[str, Dict[str, Any]] = {"standard": {}}
83
- TwoStageProcessor = None # type: ignore
84
- # Try multiple import paths for two-stage processor
85
- two_stage_paths = [
86
- "processors.two_stage", # Your fixed version
87
- "processing.two_stage.two_stage_processor",
88
- "processing.two_stage",
89
- ]
90
- for import_path in two_stage_paths:
91
- try:
92
- exec(f"from {import_path} import TwoStageProcessor, CHROMA_PRESETS")
93
- TWO_STAGE_AVAILABLE = True
94
- TWO_STAGE_IMPORT_ORIGIN = import_path
95
- logger.info(f"Two-stage import OK ({import_path})")
96
- break
97
- except Exception as e:
98
- TWO_STAGE_IMPORT_ERROR = str(e)
99
- continue
100
- if not TWO_STAGE_AVAILABLE:
101
- logger.warning(f"Two-stage import FAILED from all paths: {TWO_STAGE_IMPORT_ERROR}")
102
- # ── Quiet startup self-check (async by default) ──────────────────────────────
103
- # Place the helper in tools/startup_selfcheck.py (with tools/__init__.py present)
104
- try:
105
- from tools.startup_selfcheck import schedule_startup_selfcheck
106
- except Exception:
107
- schedule_startup_selfcheck = None # graceful if the helper isn't shipped
108
- # Dummy exceptions if core.exceptions not available
109
- class ModelLoadingError(Exception):
110
- pass
111
- class VideoProcessingError(Exception):
112
- pass
113
- # ╔══════════════════════════════════════════════════════════════════════════╗
114
- # β•‘ VideoProcessor class β•‘
115
- # β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•
116
- class VideoProcessor:
117
- """
118
- Main orchestrator – coordinates all specialised components.
119
- """
120
- def __init__(self):
121
- self.config = get_config()
122
- self._patch_config_defaults(self.config) # avoid AttributeError on older configs
123
- self.device_manager = DeviceManager()
124
- self.memory_manager = MemoryManager(self.device_manager.get_optimal_device())
125
- self.model_loader = ModelLoader(self.device_manager, self.memory_manager)
126
- self.audio_processor = AudioProcessor()
127
- self.core_processor: Optional[CoreVideoProcessor] = None
128
- self.two_stage_processor: Optional[Any] = None
129
- self.models_loaded = False
130
- self.loading_lock = threading.Lock()
131
- self.cancel_event = threading.Event()
132
- self.progress_tracker: Optional[ProgressTracker] = None
133
- logger.info(f"VideoProcessor on device: {self.device_manager.get_optimal_device()}")
134
- # ── Config hardening: add missing fields safely ───────────────────────────
135
- @staticmethod
136
- def _patch_config_defaults(cfg: Any) -> None:
137
- defaults = {
138
- # video / i/o
139
- "use_nvenc": False,
140
- "prefer_mp4": True,
141
- "video_codec": "mp4v",
142
- "audio_copy": True,
143
- "ffmpeg_path": "ffmpeg",
144
- # model/resource guards
145
- "max_model_size": 0,
146
- "max_model_size_bytes": 0,
147
- # housekeeping
148
- "output_dir": str((Path(__file__).resolve().parent.parent) / "outputs"),
149
- # MatAnyone settings to ensure it's enabled
150
- "matanyone_enabled": True,
151
- "use_matanyone": True,
152
- }
153
- for k, v in defaults.items():
154
- if not hasattr(cfg, k):
155
- setattr(cfg, k, v)
156
- Path(cfg.output_dir).mkdir(parents=True, exist_ok=True)
157
- # ── Progress helper ───────────────────────────────────────────────────────
158
- def _init_progress(self, video_path: str, cb: Optional[Callable] = None):
159
- try:
160
- import cv2
161
- cap = cv2.VideoCapture(video_path)
162
- total = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
163
- cap.release()
164
- if total <= 0:
165
- total = 100
166
- self.progress_tracker = ProgressTracker(total, cb)
167
- except Exception as e:
168
- logger.warning(f"Progress init failed: {e}")
169
- self.progress_tracker = ProgressTracker(100, cb)
170
- # ── Model loading ─────────────────────────────────────────────────────────
171
- def load_models(self, progress_callback: Optional[Callable] = None) -> str:
172
- with self.loading_lock:
173
- if self.models_loaded:
174
- return "Models already loaded and validated"
175
- try:
176
- self.cancel_event.clear()
177
- if progress_callback:
178
- progress_callback(0.0, f"Loading on {self.device_manager.get_optimal_device()}")
179
- sam2_loaded, mat_loaded = self.model_loader.load_all_models(
180
- progress_callback=progress_callback, cancel_event=self.cancel_event
181
- )
182
- if self.cancel_event.is_set():
183
- return "Model loading cancelled"
184
- # Get the actual models
185
- sam2_predictor = sam2_loaded.model if sam2_loaded else None
186
- mat_model = mat_loaded.model if mat_loaded else None # NOTE: in our MatAnyone loader this is a stateful adapter (callable)
187
- # Initialize core processor
188
- self.core_processor = CoreVideoProcessor(config=self.config, models=self.model_loader)
189
- # Initialize two-stage processor if available
190
- self.two_stage_processor = None
191
- if TWO_STAGE_AVAILABLE and TwoStageProcessor and (sam2_predictor or mat_model):
192
- try:
193
- self.two_stage_processor = TwoStageProcessor(
194
- sam2_predictor=sam2_predictor, matanyone_model=mat_model
195
  )
196
- logger.info("Two-stage processor initialised")
197
- except Exception as e:
198
- logger.warning(f"Two-stage init failed: {e}")
199
- self.two_stage_processor = None
200
- self.models_loaded = True
201
- msg = self.model_loader.get_load_summary()
202
-
203
- # Add status about processors
204
- if self.two_stage_processor:
205
- msg += "\nβœ… Two-stage processor ready"
206
- else:
207
- msg += "\n⚠️ Two-stage processor not available"
208
-
209
- if mat_model:
210
- msg += "\nβœ… MatAnyone refinement active"
211
- else:
212
- msg += "\n⚠️ MatAnyone not loaded (edges may be rough)"
213
-
214
- logger.info(msg)
215
- return msg
216
- except (AttributeError, ModelLoadingError) as e:
217
- self.models_loaded = False
218
- err = f"Model loading failed: {e}"
219
- logger.error(err)
220
- return err
221
- except Exception as e:
222
- self.models_loaded = False
223
- err = f"Unexpected error during model loading: {e}"
224
- logger.error(f"{err}\n{traceback.format_exc()}")
225
- return err
226
- # ── Public entry – process video ─────────────────────────────────────────
227
- def process_video(
228
- self,
229
- video_path: str,
230
- background_choice: str,
231
- custom_background_path: Optional[str] = None,
232
- progress_callback: Optional[Callable] = None,
233
- use_two_stage: bool = False,
234
- chroma_preset: str = "standard",
235
- key_color_mode: str = "auto",
236
- preview_mask: bool = False,
237
- preview_greenscreen: bool = False,
238
- ) -> Tuple[Optional[str], Optional[str], str]:
239
 
240
- # ===== BACKGROUND PATH DEBUG & FIX =====
241
- logger.info("=" * 60)
242
- logger.info("BACKGROUND PATH DEBUGGING")
243
- logger.info(f"background_choice: {background_choice}")
244
- logger.info(f"custom_background_path type: {type(custom_background_path)}")
245
- logger.info(f"custom_background_path value: {custom_background_path}")
246
 
247
- # Fix 1: Handle if Gradio sends a dict
248
- if isinstance(custom_background_path, dict):
249
- original = custom_background_path
250
- custom_background_path = custom_background_path.get('name') or custom_background_path.get('path')
251
- logger.info(f"Extracted path from dict: {original} -> {custom_background_path}")
 
 
252
 
253
- # Fix 2: Handle PIL Image objects
254
- try:
255
- from PIL import Image
256
- if isinstance(custom_background_path, Image.Image):
257
- import tempfile
258
- with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp:
259
- custom_background_path.save(tmp.name)
260
- custom_background_path = tmp.name
261
- logger.info(f"Saved PIL Image to: {custom_background_path}")
262
- except ImportError:
263
- pass
264
 
265
- # Fix 3: Verify file exists when using custom background
266
- if background_choice == "custom" or custom_background_path:
267
- if custom_background_path:
268
- if Path(custom_background_path).exists():
269
- logger.info(f"βœ… Background file exists: {custom_background_path}")
270
- else:
271
- logger.warning(f"⚠️ Background file does not exist: {custom_background_path}")
272
- # Try to find it in Gradio temp directories
273
- import glob
274
- patterns = [
275
- "/tmp/gradio*/**/*.jpg",
276
- "/tmp/gradio*/**/*.jpeg",
277
- "/tmp/gradio*/**/*.png",
278
- "/tmp/**/*.jpg",
279
- "/tmp/**/*.jpeg",
280
- "/tmp/**/*.png",
281
- ]
282
- for pattern in patterns:
283
- files = glob.glob(pattern, recursive=True)
284
- if files:
285
- # Get the most recent file
286
- newest = max(files, key=os.path.getmtime)
287
- logger.info(f"Found potential background: {newest}")
288
- # Only use it if it was created in the last 5 minutes
289
- if (time.time() - os.path.getmtime(newest)) < 300:
290
- custom_background_path = newest
291
- logger.info(f"βœ… Using recent temp file: {custom_background_path}")
292
- break
293
- else:
294
- logger.error("❌ Custom background mode but path is None!")
295
 
296
- logger.info(f"Final custom_background_path: {custom_background_path}")
297
- logger.info("=" * 60)
 
 
 
 
298
 
299
- if not self.models_loaded or not self.core_processor:
300
- return None, None, "Models not loaded. Please click 'Load Models' first."
301
- if self.cancel_event.is_set():
302
- return None, None, "Processing cancelled"
303
- self._init_progress(video_path, progress_callback)
304
- ok, why = validate_video_file(video_path)
305
- if not ok:
306
- return None, None, f"Invalid video: {why}"
307
- try:
308
- # Log which mode we're using
309
- mode = "two-stage" if use_two_stage else "single-stage"
310
- matanyone_status = "enabled" if self.model_loader.get_matanyone() else "disabled"
311
- logger.info(f"Processing video in {mode} mode, MatAnyone: {matanyone_status}")
312
- # IMPORTANT: start each video with a clean MatAnyone memory
313
- self._reset_matanyone_session()
314
- if use_two_stage:
315
- if not TWO_STAGE_AVAILABLE or self.two_stage_processor is None:
316
- return None, None, "Two-stage processing not available"
317
- final, green, msg = self._process_two_stage(
318
- video_path,
319
- background_choice,
320
- custom_background_path,
321
- progress_callback,
322
- chroma_preset,
323
- key_color_mode,
324
- )
325
- return final, green, msg
326
- else:
327
- final, green, msg = self._process_single_stage(
328
- video_path,
329
- background_choice,
330
- custom_background_path,
331
- progress_callback,
332
- preview_mask,
333
- preview_greenscreen,
334
- )
335
- return final, green, msg
336
- except VideoProcessingError as e:
337
- logger.error(f"Processing failed: {e}")
338
- return None, None, f"Processing failed: {e}"
339
- except Exception as e:
340
- logger.error(f"Unexpected processing error: {e}\n{traceback.format_exc()}")
341
- return None, None, f"Unexpected error: {e}"
342
- # ── Private – per-video MatAnyone reset ──────────────────────────────────
343
- def _reset_matanyone_session(self):
344
- """
345
- Ensure a fresh MatAnyone memory per video. The MatAnyone loader we use returns a
346
- callable *stateful adapter*. If present, reset() clears its InferenceCore memory.
347
- """
348
- try:
349
- mat = self.model_loader.get_matanyone()
350
- except Exception:
351
- mat = None
352
- if mat is not None and hasattr(mat, "reset") and callable(mat.reset):
353
- try:
354
- mat.reset()
355
- logger.info("MatAnyone session reset for new video")
356
- except Exception as e:
357
- logger.warning(f"MatAnyone session reset failed (continuing): {e}")
358
- # ── Private – single-stage ───────────────────────────────────────────────
359
- def _process_single_stage(
360
- self,
361
- video_path: str,
362
- background_choice: str,
363
- custom_background_path: Optional[str],
364
- progress_callback: Optional[Callable],
365
- preview_mask: bool,
366
- preview_greenscreen: bool,
367
- ) -> Tuple[Optional[str], Optional[str], str]:
368
- import time
369
 
370
- # Additional debug logging for single-stage
371
- logger.info(f"[Single-stage] background_choice: {background_choice}")
372
- logger.info(f"[Single-stage] custom_background_path: {custom_background_path}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
373
 
374
- ts = int(time.time())
375
- out_dir = Path(self.config.output_dir) / "single_stage"
376
- out_dir.mkdir(parents=True, exist_ok=True)
377
- out_path = str(out_dir / f"processed_{ts}.mp4")
378
- # Process video via your CoreVideoProcessor
379
- result = self.core_processor.process_video(
380
- input_path=video_path,
381
- output_path=out_path,
382
- bg_config={
383
- "background_choice": background_choice,
384
- "custom_path": custom_background_path,
385
- },
386
- progress_callback=progress_callback,
387
  )
388
-
389
- if not result:
390
- return None, None, "Video processing failed"
391
- # Mux audio unless preview-only
392
- if not (preview_mask or preview_greenscreen):
393
- try:
394
- final_path = self.audio_processor.add_audio_to_video(
395
- original_video=video_path, processed_video=out_path
396
- )
397
- except Exception as e:
398
- logger.warning(f"Audio mux failed, returning video without audio: {e}")
399
- final_path = out_path
400
- else:
401
- final_path = out_path
402
- # Build status message
403
- try:
404
- mat_loaded = bool(self.model_loader.get_matanyone())
405
- except Exception:
406
- mat_loaded = False
407
- matanyone_status = "βœ“" if mat_loaded else "βœ—"
408
- msg = (
409
- "Processing completed.\n"
410
- f"Frames: {result.get('frames', 'unknown')}\n"
411
- f"Background: {background_choice}\n"
412
- f"Mode: Single-stage\n"
413
- f"MatAnyone: {matanyone_status}\n"
414
- f"Device: {self.device_manager.get_optimal_device()}"
415
  )
416
- return final_path, None, msg # No green in single-stage
417
- # ── Private – two-stage ──────────────────────────────��──────────────────
418
- def _process_two_stage(
419
- self,
420
- video_path: str,
421
- background_choice: str,
422
- custom_background_path: Optional[str],
423
- progress_callback: Optional[Callable],
424
- chroma_preset: str,
425
- key_color_mode: str,
426
- ) -> Tuple[Optional[str], Optional[str], str]:
427
- if self.two_stage_processor is None:
428
- return None, None, "Two-stage processor not available"
429
 
430
- # Additional debug logging for two-stage
431
- logger.info(f"[Two-stage] background_choice: {background_choice}")
432
- logger.info(f"[Two-stage] custom_background_path: {custom_background_path}")
 
433
 
434
- import cv2, time
435
- cap = cv2.VideoCapture(video_path)
436
- if not cap.isOpened():
437
- return None, None, "Could not open input video"
438
- w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) or 1280
439
- h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) or 720
440
- cap.release()
441
- # Prepare background
442
- try:
443
- background = self.core_processor.prepare_background(
444
- background_choice, custom_background_path, w, h
445
- )
446
- except Exception as e:
447
- logger.error(f"Background preparation failed: {e}")
448
- return None, None, f"Failed to prepare background: {e}"
449
- if background is None:
450
- return None, None, "Failed to prepare background"
451
- ts = int(time.time())
452
- out_dir = Path(self.config.output_dir) / "two_stage"
453
- out_dir.mkdir(parents=True, exist_ok=True)
454
- final_out = str(out_dir / f"final_{ts}.mp4")
455
- chroma_cfg = CHROMA_PRESETS.get(chroma_preset, CHROMA_PRESETS.get("standard", {}))
456
- logger.info(f"Two-stage with preset: {chroma_preset} | key_color: {key_color_mode}")
457
- # (Per-video reset already called in process_video)
458
- final_path, green_path, stage2_msg = self.two_stage_processor.process_full_pipeline(
459
- video_path,
460
- background,
461
- final_out,
462
- key_color_mode=key_color_mode,
463
- chroma_settings=chroma_cfg,
464
- progress_callback=progress_callback,
465
  )
466
- if final_path is None:
467
- return None, None, stage2_msg
468
- # Mux audio
469
- try:
470
- final_with_audio = self.audio_processor.add_audio_to_video(
471
- original_video=video_path, processed_video=final_path
472
- )
473
- except Exception as e:
474
- logger.warning(f"Audio mux failed: {e}")
475
- final_with_audio = final_path
476
- try:
477
- mat_loaded = bool(self.model_loader.get_matanyone())
478
- except Exception:
479
- mat_loaded = False
480
- matanyone_status = "βœ“" if mat_loaded else "βœ—"
481
- msg = (
482
- "Two-stage processing completed.\n"
483
- f"Background: {background_choice}\n"
484
- f"Chroma Preset: {chroma_preset}\n"
485
- f"MatAnyone: {matanyone_status}\n"
486
- f"Device: {self.device_manager.get_optimal_device()}"
487
  )
488
- return final_with_audio, green_path, msg
489
- # ── Status helpers ───────────────────────────────────────────────────────
490
- def get_status(self) -> Dict[str, Any]:
491
- status = {
492
- "models_loaded": self.models_loaded,
493
- "two_stage_available": bool(TWO_STAGE_AVAILABLE and self.two_stage_processor),
494
- "two_stage_origin": TWO_STAGE_IMPORT_ORIGIN or "",
495
- "device": str(self.device_manager.get_optimal_device()),
496
- "core_processor_loaded": self.core_processor is not None,
497
- "config": self._safe_config_dict(),
498
- "memory_usage": self._safe_memory_usage(),
499
- }
500
-
501
- try:
502
- status["sam2_loaded"] = self.model_loader.get_sam2() is not None
503
- status["matanyone_loaded"] = self.model_loader.get_matanyone() is not None
504
- status["model_info"] = self.model_loader.get_model_info()
505
- except Exception:
506
- status["sam2_loaded"] = False
507
- status["matanyone_loaded"] = False
508
- if self.progress_tracker:
509
- status["progress"] = self.progress_tracker.get_all_progress()
510
-
511
- return status
512
- def _safe_config_dict(self) -> Dict[str, Any]:
513
- try:
514
- return self.config.to_dict()
515
- except Exception:
516
- keys = ["use_nvenc", "prefer_mp4", "video_codec", "audio_copy",
517
- "ffmpeg_path", "max_model_size", "max_model_size_bytes",
518
- "output_dir", "matanyone_enabled"]
519
- return {k: getattr(self.config, k, None) for k in keys}
520
- def _safe_memory_usage(self) -> Dict[str, Any]:
521
- try:
522
- return self.memory_manager.get_memory_usage()
523
- except Exception:
524
- return {}
525
- def cancel_processing(self):
526
- self.cancel_event.set()
527
- logger.info("Cancellation requested")
528
- def cleanup_resources(self):
529
- try:
530
- self.memory_manager.cleanup_aggressive()
531
- except Exception:
532
- pass
533
- try:
534
- self.model_loader.cleanup()
535
- except Exception:
536
- pass
537
- logger.info("Resources cleaned up")
538
- # ── Singleton + thin wrappers (used by UI callbacks) ────────────────────────
539
- processor = VideoProcessor()
540
- def load_models_with_validation(progress_callback: Optional[Callable] = None) -> str:
541
- return processor.load_models(progress_callback)
542
- def process_video_fixed(
543
- video_path: str,
544
- background_choice: str,
545
- custom_background_path: Optional[str],
546
- progress_callback: Optional[Callable] = None,
547
- use_two_stage: bool = False,
548
- chroma_preset: str = "standard",
549
- key_color_mode: str = "auto",
550
- preview_mask: bool = False,
551
- preview_greenscreen: bool = False,
552
- ) -> Tuple[Optional[str], Optional[str], str]:
553
- return processor.process_video(
554
- video_path,
555
- background_choice,
556
- custom_background_path,
557
- progress_callback,
558
- use_two_stage,
559
- chroma_preset,
560
- key_color_mode,
561
- preview_mask,
562
- preview_greenscreen,
563
- )
564
- def get_model_status() -> Dict[str, Any]:
565
- return processor.get_status()
566
- def get_cache_status() -> Dict[str, Any]:
567
- return processor.get_status()
568
- PROCESS_CANCELLED = processor.cancel_event
569
- # ── CLI entrypoint (must exist; app.py imports main) ─────────────────────────
570
- def main():
571
- try:
572
- logger.info("Starting BackgroundFX Pro")
573
- logger.info(f"Device: {processor.device_manager.get_optimal_device()}")
574
- logger.info(f"Two-stage available: {TWO_STAGE_AVAILABLE}")
575
- # πŸ”Ή Quiet model self-check (defaults to async; set SELF_CHECK_MODE=sync to block)
576
- if schedule_startup_selfcheck is not None:
577
- try:
578
- schedule_startup_selfcheck(mode=os.getenv("SELF_CHECK_MODE", "async"))
579
- except Exception as e:
580
- logger.error(f"Startup self-check skipped: {e}", exc_info=True)
581
- # Log model loader type
582
- try:
583
- from models.loaders.model_loader import ModelLoader
584
- logger.info("Using split loader architecture")
585
- except Exception:
586
- logger.info("Using legacy loader")
587
- from ui.ui_components import create_interface
588
- demo = create_interface()
589
- demo.queue().launch(
590
- server_name="0.0.0.0",
591
- server_port=7860,
592
- show_error=True,
593
- debug=False,
594
  )
595
- finally:
596
- processor.cleanup_resources()
597
- if __name__ == "__main__":
598
- main()
 
1
  #!/usr/bin/env python3
2
  """
3
+ UI Components for BackgroundFX Pro
4
+ Complete interface with all features
5
  """
6
+
7
+ import gradio as gr
8
  import os
9
+
10
+ # Background style choices
11
+ BG_STYLES = ["minimalist", "office_modern", "studio_blue", "studio_green", "warm_gradient", "tech_dark"]
12
+
13
+ def create_interface():
14
+ """Create the full BackgroundFX Pro interface"""
15
+
16
+ # Import callbacks inside function to avoid circular imports
17
+ from ui.callbacks import (
18
+ cb_load_models,
19
+ cb_process_video,
20
+ cb_cancel,
21
+ cb_status,
22
+ cb_clear,
23
+ cb_generate_bg,
24
+ cb_use_gen_bg,
25
+ cb_preset_bg_preview,
26
+ )
27
+
28
+ with gr.Blocks(title="🎬 BackgroundFX Pro", theme=gr.themes.Soft()) as demo:
29
+ gr.Markdown(
30
+ """
31
+ # 🎬 BackgroundFX Pro
32
+ ### AI-Powered Video Background Replacement
33
+ Replace your video background with professional quality using SAM2 + MatAnyone
34
+ """
35
+ )
36
+
37
+ with gr.Tabs():
38
+ # Main Processing Tab
39
+ with gr.Tab("πŸŽ₯ Process Video"):
40
+ with gr.Row():
41
+ # Left Column - Inputs
42
+ with gr.Column(scale=1):
43
+ video_input = gr.Video(
44
+ label="Upload Video",
45
+ interactive=True
46
+ )
47
+
48
+ gr.Markdown("### Background Settings")
49
+
50
+ bg_method = gr.Radio(
51
+ label="Background Source",
52
+ choices=[
53
+ "Preset Styles",
54
+ "Upload Custom",
55
+ "Generate AI Background"
56
+ ],
57
+ value="Preset Styles"
58
+ )
59
+
60
+ # Preset styles
61
+ with gr.Group(visible=True) as preset_group:
62
+ bg_style = gr.Dropdown(
63
+ label="Background Style",
64
+ choices=BG_STYLES,
65
+ value="minimalist"
66
+ )
67
+ preset_preview = gr.Image(
68
+ label="Preview",
69
+ interactive=False,
70
+ height=200
71
+ )
72
+
73
+ # Custom upload
74
+ with gr.Group(visible=False) as upload_group:
75
+ custom_bg = gr.Image(
76
+ label="Upload Background Image",
77
+ type="filepath",
78
+ interactive=True
79
+ )
80
+
81
+ # AI Generation
82
+ with gr.Group(visible=False) as generate_group:
83
+ prompt = gr.Textbox(
84
+ label="Describe Background",
85
+ value="modern office space",
86
+ placeholder="e.g., 'warm sunset studio', 'minimalist white room'"
87
+ )
88
+ with gr.Row():
89
+ gen_width = gr.Slider(640, 1920, 1280, step=10, label="Width")
90
+ gen_height = gr.Slider(360, 1080, 720, step=10, label="Height")
91
+ with gr.Row():
92
+ bokeh = gr.Slider(0, 30, 8, step=1, label="Blur")
93
+ vignette = gr.Slider(0, 0.6, 0.15, step=0.01, label="Vignette")
94
+ btn_generate = gr.Button("✨ Generate Background", variant="secondary")
95
+ gen_preview = gr.Image(label="Generated Background", interactive=False)
96
+ gen_path = gr.Textbox(visible=False)
97
+ btn_use_generated = gr.Button("Use This Background", variant="secondary")
98
+
99
+ # Processing Options
100
+ with gr.Accordion("βš™οΈ Advanced Options", open=False):
101
+ use_two_stage = gr.Checkbox(
102
+ label="Use Two-Stage Processing",
103
+ value=True,
104
+ info="Better quality but slower"
105
+ )
106
+ chroma_preset = gr.Dropdown(
107
+ label="Chroma Preset",
108
+ choices=["standard"],
109
+ value="standard"
110
+ )
111
+ key_color = gr.Dropdown(
112
+ label="Key Color Mode",
113
+ choices=["auto", "green", "blue"],
114
+ value="auto"
115
+ )
116
+ quality = gr.Radio(
117
+ label="Quality",
118
+ choices=["speed", "balanced", "max"],
119
+ value="balanced"
120
+ )
121
+
122
+ # Action Buttons
123
+ gr.Markdown("### Actions")
124
+ with gr.Row():
125
+ btn_load = gr.Button("πŸ“¦ Load Models", variant="secondary", scale=1)
126
+ btn_process = gr.Button("🎬 Process Video", variant="primary", scale=2)
127
+ with gr.Row():
128
+ btn_cancel = gr.Button("⏹ Cancel", variant="stop")
129
+ btn_clear = gr.Button("πŸ—‘οΈ Clear All")
130
+
131
+ # Right Column - Outputs
132
+ with gr.Column(scale=1):
133
+ video_output = gr.Video(
134
+ label="Processed Video",
135
+ interactive=False
136
+ )
137
+ status = gr.Textbox(
138
+ label="Status",
139
+ lines=8,
140
+ max_lines=12,
141
+ interactive=False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
  )
143
+ with gr.Row():
144
+ processing_time = gr.Textbox(
145
+ label="Processing Time",
146
+ interactive=False
147
+ )
148
+ quality_info = gr.Textbox(
149
+ label="Quality Mode",
150
+ interactive=False
151
+ )
152
+
153
+ # Status Tab
154
+ with gr.Tab("πŸ“Š System Status"):
155
+ with gr.Row():
156
+ with gr.Column():
157
+ gr.Markdown("### Model Status")
158
+ model_status = gr.JSON(label="Models")
159
+ with gr.Column():
160
+ gr.Markdown("### System Info")
161
+ system_status = gr.JSON(label="System")
162
+ btn_refresh_status = gr.Button("πŸ”„ Refresh Status")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
 
164
+ # Wire up all the callbacks
 
 
 
 
 
165
 
166
+ # Background method switching
167
+ def switch_bg_method(method):
168
+ return (
169
+ gr.update(visible=(method == "Preset Styles")),
170
+ gr.update(visible=(method == "Upload Custom")),
171
+ gr.update(visible=(method == "Generate AI Background"))
172
+ )
173
 
174
+ bg_method.change(
175
+ switch_bg_method,
176
+ inputs=[bg_method],
177
+ outputs=[preset_group, upload_group, generate_group]
178
+ )
 
 
 
 
 
 
179
 
180
+ # Preset preview
181
+ bg_style.change(
182
+ cb_preset_bg_preview,
183
+ inputs=[bg_style],
184
+ outputs=[preset_preview]
185
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186
 
187
+ # AI Generation
188
+ btn_generate.click(
189
+ cb_generate_bg,
190
+ inputs=[prompt, gen_width, gen_height, bokeh, vignette, gr.State(1.0)],
191
+ outputs=[gen_preview, gen_path]
192
+ )
193
 
194
+ btn_use_generated.click(
195
+ cb_use_gen_bg,
196
+ inputs=[gen_path],
197
+ outputs=[custom_bg]
198
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
199
 
200
+ # Main actions
201
+ btn_load.click(
202
+ cb_load_models,
203
+ outputs=[status]
204
+ )
205
+
206
+ # Process with proper background selection
207
+ def process_with_bg(video, method, style, custom, gen_path, two_stage, chroma, key, quality):
208
+ # Set quality environment variable
209
+ os.environ["BFX_QUALITY"] = quality.lower()
210
+
211
+ # Determine which background to use
212
+ if method == "Upload Custom":
213
+ bg_choice = "custom"
214
+ bg_path = custom
215
+ elif method == "Generate AI Background":
216
+ bg_choice = "custom"
217
+ bg_path = gen_path
218
+ else: # Preset Styles
219
+ bg_choice = style
220
+ bg_path = None
221
+
222
+ return cb_process_video(
223
+ video, bg_choice, bg_path, two_stage,
224
+ chroma, key, False, False
225
+ )
226
 
227
+ btn_process.click(
228
+ process_with_bg,
229
+ inputs=[
230
+ video_input, bg_method, bg_style, custom_bg,
231
+ gen_path, use_two_stage, chroma_preset, key_color, quality
232
+ ],
233
+ outputs=[video_output, status]
 
 
 
 
 
 
234
  )
235
+
236
+ btn_cancel.click(
237
+ cb_cancel,
238
+ outputs=[status]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
239
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
240
 
241
+ btn_clear.click(
242
+ cb_clear,
243
+ outputs=[video_output, status, gen_preview, gen_path, custom_bg]
244
+ )
245
 
246
+ btn_refresh_status.click(
247
+ cb_status,
248
+ outputs=[model_status, system_status]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
249
  )
250
+
251
+ # Update quality info when changed
252
+ quality.change(
253
+ lambda q: f"Mode: {q} ({'fastest' if q=='speed' else 'best quality' if q=='max' else 'balanced'})",
254
+ inputs=[quality],
255
+ outputs=[quality_info]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
256
  )
257
+
258
+ # Load preset preview on startup
259
+ demo.load(
260
+ cb_preset_bg_preview,
261
+ inputs=[bg_style],
262
+ outputs=[preset_preview]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
263
  )
264
+
265
+ return demo