Update ui/ui_components.py
Browse files- ui/ui_components.py +245 -578
ui/ui_components.py
CHANGED
|
@@ -1,598 +1,265 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
-
|
| 4 |
-
|
| 5 |
"""
|
| 6 |
-
|
| 7 |
-
|
| 8 |
import os
|
| 9 |
-
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
import
|
| 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 |
-
"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 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 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 |
-
#
|
| 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 |
-
#
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
|
|
|
|
|
|
| 252 |
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 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 |
-
#
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 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 |
-
|
| 297 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 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 |
-
#
|
| 371 |
-
|
| 372 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 373 |
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 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 |
-
|
| 390 |
-
|
| 391 |
-
|
| 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 |
-
|
| 431 |
-
|
| 432 |
-
|
|
|
|
| 433 |
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 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 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 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 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 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 |
-
|
| 596 |
-
|
| 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
|
|
|
|
|
|