Update ui/callbacks.py
Browse files- ui/callbacks.py +12 -18
ui/callbacks.py
CHANGED
|
@@ -3,11 +3,7 @@
|
|
| 3 |
Callbacks for BackgroundFX Pro UI
|
| 4 |
---------------------------------
|
| 5 |
All functions here are *thin* wrappers wired to the Gradio interface.
|
| 6 |
-
|
| 7 |
-
• key_color_mode parameter already added (matches ui_components.py)
|
| 8 |
-
• PREVIEW FUNCTIONS NOW IMPLEMENTED:
|
| 9 |
-
- cb_video_changed → returns first frame
|
| 10 |
-
• SIMPLIFIED: Background handled by gr.Image component directly
|
| 11 |
"""
|
| 12 |
|
| 13 |
from __future__ import annotations
|
|
@@ -16,14 +12,8 @@
|
|
| 16 |
import numpy as np
|
| 17 |
from PIL import Image
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
load_models_with_validation,
|
| 22 |
-
process_video_fixed,
|
| 23 |
-
get_model_status,
|
| 24 |
-
get_cache_status,
|
| 25 |
-
PROCESS_CANCELLED,
|
| 26 |
-
)
|
| 27 |
|
| 28 |
# ---- Optional utilities (background generator & previews) ----
|
| 29 |
_try_bg_gen = None
|
|
@@ -134,6 +124,8 @@ def _blend(n, pal):
|
|
| 134 |
# ------------------------------------------------------------------
|
| 135 |
def cb_load_models() -> str:
|
| 136 |
"""Load SAM2 + MatAnyOne and return human-readable status."""
|
|
|
|
|
|
|
| 137 |
return load_models_with_validation()
|
| 138 |
|
| 139 |
|
|
@@ -143,7 +135,7 @@ def cb_load_models() -> str:
|
|
| 143 |
def cb_process_video(
|
| 144 |
vid: str,
|
| 145 |
style: str,
|
| 146 |
-
custom_bg_path: str | None,
|
| 147 |
use_two: bool,
|
| 148 |
chroma: str,
|
| 149 |
key_color_mode: str,
|
|
@@ -154,18 +146,18 @@ def cb_process_video(
|
|
| 154 |
Runs the two-stage (or single-stage) pipeline and returns:
|
| 155 |
(processed_video_path | None, status_message:str)
|
| 156 |
"""
|
|
|
|
|
|
|
|
|
|
| 157 |
# Reset any prior cancel flag when user clicks Run
|
| 158 |
if PROCESS_CANCELLED.is_set():
|
| 159 |
PROCESS_CANCELLED.clear()
|
| 160 |
|
| 161 |
-
# custom_bg_path is now directly a filepath string from gr.Image
|
| 162 |
-
# No need to extract from dict
|
| 163 |
-
|
| 164 |
# Fire the core function
|
| 165 |
return process_video_fixed(
|
| 166 |
video_path=vid,
|
| 167 |
background_choice=style,
|
| 168 |
-
custom_background_path=custom_bg_path,
|
| 169 |
progress_callback=None,
|
| 170 |
use_two_stage=use_two,
|
| 171 |
chroma_preset=chroma,
|
|
@@ -180,6 +172,7 @@ def cb_process_video(
|
|
| 180 |
# ------------------------------------------------------------------
|
| 181 |
def cb_cancel() -> str:
|
| 182 |
try:
|
|
|
|
| 183 |
PROCESS_CANCELLED.set()
|
| 184 |
return "Cancellation requested."
|
| 185 |
except Exception as e:
|
|
@@ -187,6 +180,7 @@ def cb_cancel() -> str:
|
|
| 187 |
|
| 188 |
def cb_status() -> Tuple[Dict[str, Any], Dict[str, Any]]:
|
| 189 |
try:
|
|
|
|
| 190 |
return get_model_status(), get_cache_status()
|
| 191 |
except Exception as e:
|
| 192 |
return {"error": str(e)}, {"error": str(e)}
|
|
|
|
| 3 |
Callbacks for BackgroundFX Pro UI
|
| 4 |
---------------------------------
|
| 5 |
All functions here are *thin* wrappers wired to the Gradio interface.
|
| 6 |
+
NO IMPORTS FROM core.app AT MODULE LEVEL to avoid circular imports
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
"""
|
| 8 |
|
| 9 |
from __future__ import annotations
|
|
|
|
| 12 |
import numpy as np
|
| 13 |
from PIL import Image
|
| 14 |
|
| 15 |
+
# DO NOT import from core.app here!
|
| 16 |
+
# We'll get these via parameter injection or lazy imports
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# ---- Optional utilities (background generator & previews) ----
|
| 19 |
_try_bg_gen = None
|
|
|
|
| 124 |
# ------------------------------------------------------------------
|
| 125 |
def cb_load_models() -> str:
|
| 126 |
"""Load SAM2 + MatAnyOne and return human-readable status."""
|
| 127 |
+
# Lazy import to avoid circular dependency
|
| 128 |
+
from core.app import load_models_with_validation
|
| 129 |
return load_models_with_validation()
|
| 130 |
|
| 131 |
|
|
|
|
| 135 |
def cb_process_video(
|
| 136 |
vid: str,
|
| 137 |
style: str,
|
| 138 |
+
custom_bg_path: str | None,
|
| 139 |
use_two: bool,
|
| 140 |
chroma: str,
|
| 141 |
key_color_mode: str,
|
|
|
|
| 146 |
Runs the two-stage (or single-stage) pipeline and returns:
|
| 147 |
(processed_video_path | None, status_message:str)
|
| 148 |
"""
|
| 149 |
+
# Lazy imports to avoid circular dependency
|
| 150 |
+
from core.app import process_video_fixed, PROCESS_CANCELLED
|
| 151 |
+
|
| 152 |
# Reset any prior cancel flag when user clicks Run
|
| 153 |
if PROCESS_CANCELLED.is_set():
|
| 154 |
PROCESS_CANCELLED.clear()
|
| 155 |
|
|
|
|
|
|
|
|
|
|
| 156 |
# Fire the core function
|
| 157 |
return process_video_fixed(
|
| 158 |
video_path=vid,
|
| 159 |
background_choice=style,
|
| 160 |
+
custom_background_path=custom_bg_path,
|
| 161 |
progress_callback=None,
|
| 162 |
use_two_stage=use_two,
|
| 163 |
chroma_preset=chroma,
|
|
|
|
| 172 |
# ------------------------------------------------------------------
|
| 173 |
def cb_cancel() -> str:
|
| 174 |
try:
|
| 175 |
+
from core.app import PROCESS_CANCELLED
|
| 176 |
PROCESS_CANCELLED.set()
|
| 177 |
return "Cancellation requested."
|
| 178 |
except Exception as e:
|
|
|
|
| 180 |
|
| 181 |
def cb_status() -> Tuple[Dict[str, Any], Dict[str, Any]]:
|
| 182 |
try:
|
| 183 |
+
from core.app import get_model_status, get_cache_status
|
| 184 |
return get_model_status(), get_cache_status()
|
| 185 |
except Exception as e:
|
| 186 |
return {"error": str(e)}, {"error": str(e)}
|