Create ui/callbacks.py
Browse files- ui/callbacks.py +75 -0
ui/callbacks.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Callbacks for BackgroundFX Pro UI
|
| 3 |
+
Pure functions wired to UI events.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
from typing import Any, Dict, Tuple
|
| 8 |
+
|
| 9 |
+
from core.app import (
|
| 10 |
+
load_models_with_validation,
|
| 11 |
+
process_video_fixed,
|
| 12 |
+
get_model_status,
|
| 13 |
+
get_cache_status,
|
| 14 |
+
PROCESS_CANCELLED,
|
| 15 |
+
)
|
| 16 |
+
from utils.bg_generator import generate_ai_background
|
| 17 |
+
from utils.media_utils import first_frame, pil_from_path
|
| 18 |
+
|
| 19 |
+
# -------- MODEL MANAGEMENT --------
|
| 20 |
+
def cb_load_models() -> str:
|
| 21 |
+
return load_models_with_validation()
|
| 22 |
+
|
| 23 |
+
def cb_process_video(
|
| 24 |
+
vid: str, style: str, custom_file: dict | None,
|
| 25 |
+
use_two: bool, chroma: str, prev_mask: bool, prev_green: bool,
|
| 26 |
+
):
|
| 27 |
+
if PROCESS_CANCELLED.is_set():
|
| 28 |
+
PROCESS_CANCELLED.clear()
|
| 29 |
+
custom_path = None
|
| 30 |
+
if isinstance(custom_file, dict) and custom_file.get("name"):
|
| 31 |
+
custom_path = custom_file["name"]
|
| 32 |
+
return process_video_fixed(
|
| 33 |
+
video_path=vid,
|
| 34 |
+
background_choice=style,
|
| 35 |
+
custom_background_path=custom_path,
|
| 36 |
+
progress_callback=None,
|
| 37 |
+
use_two_stage=use_two,
|
| 38 |
+
chroma_preset=chroma,
|
| 39 |
+
preview_mask=prev_mask,
|
| 40 |
+
preview_greenscreen=prev_green,
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
def cb_cancel() -> str:
|
| 44 |
+
try:
|
| 45 |
+
PROCESS_CANCELLED.set()
|
| 46 |
+
return "Cancellation requested."
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return f"Cancel failed: {e}"
|
| 49 |
+
|
| 50 |
+
def cb_status() -> Tuple[Dict[str, Any], Dict[str, Any]]:
|
| 51 |
+
try:
|
| 52 |
+
return get_model_status(), get_cache_status()
|
| 53 |
+
except Exception as e:
|
| 54 |
+
return {"error": str(e)}, {"error": str(e)}
|
| 55 |
+
|
| 56 |
+
def cb_clear():
|
| 57 |
+
return None, "", None, ""
|
| 58 |
+
|
| 59 |
+
# -------- AI BACKGROUND --------
|
| 60 |
+
def cb_generate_bg(prompt_text: str, w: int, h: int, b: float, v: float, c: float):
|
| 61 |
+
img, path = generate_ai_background(prompt_text, width=int(w), height=int(h), bokeh=b, vignette=v, contrast=c)
|
| 62 |
+
return img, path
|
| 63 |
+
|
| 64 |
+
def cb_use_gen_bg(path_text: str):
|
| 65 |
+
return {"name": path_text, "size": os.path.getsize(path_text)} if path_text and os.path.exists(path_text) else None
|
| 66 |
+
|
| 67 |
+
# -------- PREVIEWS --------
|
| 68 |
+
def cb_video_changed(vid_path: str):
|
| 69 |
+
img, info = first_frame(vid_path)
|
| 70 |
+
return img # Could output preview img if UI has a slot
|
| 71 |
+
|
| 72 |
+
def cb_custom_bg_preview(file_obj: dict | None):
|
| 73 |
+
if isinstance(file_obj, dict) and file_obj.get("name") and os.path.exists(file_obj["name"]):
|
| 74 |
+
return pil_from_path(file_obj["name"])
|
| 75 |
+
return None
|