Update ui/callbacks.py
Browse files- ui/callbacks.py +15 -28
ui/callbacks.py
CHANGED
|
@@ -7,8 +7,7 @@
|
|
| 7 |
• key_color_mode parameter already added (matches ui_components.py)
|
| 8 |
• PREVIEW FUNCTIONS NOW IMPLEMENTED:
|
| 9 |
- cb_video_changed → returns first frame
|
| 10 |
-
|
| 11 |
-
• FIXED: Returns only 2 values for process_video (don't touch preview)
|
| 12 |
"""
|
| 13 |
|
| 14 |
from __future__ import annotations
|
|
@@ -144,7 +143,7 @@ def cb_load_models() -> str:
|
|
| 144 |
def cb_process_video(
|
| 145 |
vid: str,
|
| 146 |
style: str,
|
| 147 |
-
|
| 148 |
use_two: bool,
|
| 149 |
chroma: str,
|
| 150 |
key_color_mode: str,
|
|
@@ -154,22 +153,19 @@ def cb_process_video(
|
|
| 154 |
"""
|
| 155 |
Runs the two-stage (or single-stage) pipeline and returns:
|
| 156 |
(processed_video_path | None, status_message:str)
|
| 157 |
-
Returns ONLY 2 values - doesn't touch the background preview.
|
| 158 |
"""
|
| 159 |
# Reset any prior cancel flag when user clicks Run
|
| 160 |
if PROCESS_CANCELLED.is_set():
|
| 161 |
PROCESS_CANCELLED.clear()
|
| 162 |
|
| 163 |
-
#
|
| 164 |
-
|
| 165 |
-
if isinstance(custom_file, dict) and custom_file.get("name"):
|
| 166 |
-
custom_path = custom_file["name"]
|
| 167 |
|
| 168 |
# Fire the core function
|
| 169 |
return process_video_fixed(
|
| 170 |
video_path=vid,
|
| 171 |
background_choice=style,
|
| 172 |
-
custom_background_path=
|
| 173 |
progress_callback=None,
|
| 174 |
use_two_stage=use_two,
|
| 175 |
chroma_preset=chroma,
|
|
@@ -196,8 +192,8 @@ def cb_status() -> Tuple[Dict[str, Any], Dict[str, Any]]:
|
|
| 196 |
return {"error": str(e)}, {"error": str(e)}
|
| 197 |
|
| 198 |
def cb_clear():
|
| 199 |
-
"""Clear all outputs
|
| 200 |
-
# Return blanks for (out_video, status, gen_preview, gen_path,
|
| 201 |
return None, "", None, "", None
|
| 202 |
|
| 203 |
|
|
@@ -209,10 +205,13 @@ def cb_generate_bg(prompt_text: str, w: int, h: int, b: float, v: float, c: floa
|
|
| 209 |
img, path = _generate_ai_background(prompt_text, int(w), int(h), b, v, c)
|
| 210 |
return img, path
|
| 211 |
|
| 212 |
-
def cb_use_gen_bg(
|
| 213 |
-
"""
|
| 214 |
-
|
| 215 |
-
|
|
|
|
|
|
|
|
|
|
| 216 |
return None
|
| 217 |
|
| 218 |
|
|
@@ -238,22 +237,10 @@ def cb_video_changed(vid_path: str):
|
|
| 238 |
except Exception:
|
| 239 |
return None
|
| 240 |
|
| 241 |
-
def cb_custom_bg_preview(file_obj: dict | None):
|
| 242 |
-
"""
|
| 243 |
-
Display the uploaded background image (if any) as a preview.
|
| 244 |
-
"""
|
| 245 |
-
try:
|
| 246 |
-
if file_obj and file_obj.get("name"):
|
| 247 |
-
img = Image.open(file_obj["name"]).convert("RGB")
|
| 248 |
-
return np.array(img)
|
| 249 |
-
except Exception:
|
| 250 |
-
pass
|
| 251 |
-
return None
|
| 252 |
-
|
| 253 |
def cb_preset_bg_preview(style: str):
|
| 254 |
"""
|
| 255 |
Generate and display preview for preset backgrounds.
|
| 256 |
-
|
| 257 |
"""
|
| 258 |
try:
|
| 259 |
from utils.cv_processing import create_professional_background
|
|
|
|
| 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
|
|
|
|
| 143 |
def cb_process_video(
|
| 144 |
vid: str,
|
| 145 |
style: str,
|
| 146 |
+
custom_bg_path: str | None, # Now directly a filepath from gr.Image
|
| 147 |
use_two: bool,
|
| 148 |
chroma: str,
|
| 149 |
key_color_mode: str,
|
|
|
|
| 153 |
"""
|
| 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, # Direct path
|
| 169 |
progress_callback=None,
|
| 170 |
use_two_stage=use_two,
|
| 171 |
chroma_preset=chroma,
|
|
|
|
| 192 |
return {"error": str(e)}, {"error": str(e)}
|
| 193 |
|
| 194 |
def cb_clear():
|
| 195 |
+
"""Clear all outputs"""
|
| 196 |
+
# Return blanks for (out_video, status, gen_preview, gen_path, custom_bg)
|
| 197 |
return None, "", None, "", None
|
| 198 |
|
| 199 |
|
|
|
|
| 205 |
img, path = _generate_ai_background(prompt_text, int(w), int(h), b, v, c)
|
| 206 |
return img, path
|
| 207 |
|
| 208 |
+
def cb_use_gen_bg(gen_path: str):
|
| 209 |
+
"""
|
| 210 |
+
Use generated background as custom.
|
| 211 |
+
Returns the path for gr.Image to display.
|
| 212 |
+
"""
|
| 213 |
+
if gen_path and os.path.exists(gen_path):
|
| 214 |
+
return gen_path # gr.Image can display from path
|
| 215 |
return None
|
| 216 |
|
| 217 |
|
|
|
|
| 237 |
except Exception:
|
| 238 |
return None
|
| 239 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
def cb_preset_bg_preview(style: str):
|
| 241 |
"""
|
| 242 |
Generate and display preview for preset backgrounds.
|
| 243 |
+
Returns image for gr.Image component to display.
|
| 244 |
"""
|
| 245 |
try:
|
| 246 |
from utils.cv_processing import create_professional_background
|