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