Update ui/callbacks.py
Browse files- ui/callbacks.py +65 -9
ui/callbacks.py
CHANGED
|
@@ -8,10 +8,11 @@
|
|
| 8 |
• PREVIEW FUNCTIONS NOW IMPLEMENTED:
|
| 9 |
- cb_video_changed → returns first frame
|
| 10 |
- cb_custom_bg_preview → returns uploaded image
|
|
|
|
| 11 |
"""
|
| 12 |
|
| 13 |
from __future__ import annotations
|
| 14 |
-
import os, cv2
|
| 15 |
from typing import Any, Dict, Tuple
|
| 16 |
import numpy as np
|
| 17 |
from PIL import Image
|
|
@@ -32,6 +33,9 @@
|
|
| 32 |
except Exception:
|
| 33 |
pass
|
| 34 |
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
# ------------------------------------------------------------------
|
| 37 |
# LIGHTWEIGHT BG GENERATOR (inline fallback)
|
|
@@ -146,14 +150,17 @@ def cb_process_video(
|
|
| 146 |
custom_file: dict | None,
|
| 147 |
use_two: bool,
|
| 148 |
chroma: str,
|
| 149 |
-
key_color_mode: str,
|
| 150 |
prev_mask: bool,
|
| 151 |
prev_green: bool,
|
| 152 |
):
|
| 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()
|
|
@@ -162,19 +169,36 @@ def cb_process_video(
|
|
| 162 |
custom_path = None
|
| 163 |
if isinstance(custom_file, dict) and custom_file.get("name"):
|
| 164 |
custom_path = custom_file["name"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
# Fire the core function
|
| 167 |
-
|
| 168 |
video_path=vid,
|
| 169 |
background_choice=style,
|
| 170 |
custom_background_path=custom_path,
|
| 171 |
-
progress_callback=None,
|
| 172 |
use_two_stage=use_two,
|
| 173 |
chroma_preset=chroma,
|
| 174 |
-
key_color_mode=key_color_mode,
|
| 175 |
preview_mask=prev_mask,
|
| 176 |
preview_greenscreen=prev_green,
|
| 177 |
)
|
|
|
|
|
|
|
|
|
|
| 178 |
|
| 179 |
|
| 180 |
# ------------------------------------------------------------------
|
|
@@ -194,19 +218,34 @@ def cb_status() -> Tuple[Dict[str, Any], Dict[str, Any]]:
|
|
| 194 |
return {"error": str(e)}, {"error": str(e)}
|
| 195 |
|
| 196 |
def cb_clear():
|
| 197 |
-
|
| 198 |
-
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
|
| 201 |
# ------------------------------------------------------------------
|
| 202 |
# AI BACKGROUND
|
| 203 |
# ------------------------------------------------------------------
|
| 204 |
def cb_generate_bg(prompt_text: str, w: int, h: int, b: float, v: float, c: float):
|
|
|
|
|
|
|
| 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(path_text: str):
|
|
|
|
|
|
|
| 209 |
if path_text and os.path.exists(path_text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
return {"name": path_text, "size": os.path.getsize(path_text)}
|
| 211 |
return None
|
| 212 |
|
|
@@ -236,11 +275,28 @@ def cb_video_changed(vid_path: str):
|
|
| 236 |
def cb_custom_bg_preview(file_obj: dict | None):
|
| 237 |
"""
|
| 238 |
Display the uploaded background image (if any) as a preview.
|
|
|
|
| 239 |
"""
|
|
|
|
| 240 |
try:
|
| 241 |
if file_obj and file_obj.get("name"):
|
| 242 |
img = Image.open(file_obj["name"]).convert("RGB")
|
| 243 |
-
|
|
|
|
| 244 |
except Exception:
|
| 245 |
pass
|
| 246 |
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
• PREVIEW FUNCTIONS NOW IMPLEMENTED:
|
| 9 |
- cb_video_changed → returns first frame
|
| 10 |
- cb_custom_bg_preview → returns uploaded image
|
| 11 |
+
• UPDATED: Background preview persists during processing
|
| 12 |
"""
|
| 13 |
|
| 14 |
from __future__ import annotations
|
| 15 |
+
import os, cv2
|
| 16 |
from typing import Any, Dict, Tuple
|
| 17 |
import numpy as np
|
| 18 |
from PIL import Image
|
|
|
|
| 33 |
except Exception:
|
| 34 |
pass
|
| 35 |
|
| 36 |
+
# Global to store current background for persistence
|
| 37 |
+
current_background_image = None
|
| 38 |
+
|
| 39 |
|
| 40 |
# ------------------------------------------------------------------
|
| 41 |
# LIGHTWEIGHT BG GENERATOR (inline fallback)
|
|
|
|
| 150 |
custom_file: dict | None,
|
| 151 |
use_two: bool,
|
| 152 |
chroma: str,
|
| 153 |
+
key_color_mode: str,
|
| 154 |
prev_mask: bool,
|
| 155 |
prev_green: bool,
|
| 156 |
):
|
| 157 |
"""
|
| 158 |
Runs the two-stage (or single-stage) pipeline and returns:
|
| 159 |
+
(processed_video_path | None, status_message:str, background_preview)
|
| 160 |
+
Now returns THREE values to maintain background preview during processing.
|
| 161 |
"""
|
| 162 |
+
global current_background_image
|
| 163 |
+
|
| 164 |
# Reset any prior cancel flag when user clicks Run
|
| 165 |
if PROCESS_CANCELLED.is_set():
|
| 166 |
PROCESS_CANCELLED.clear()
|
|
|
|
| 169 |
custom_path = None
|
| 170 |
if isinstance(custom_file, dict) and custom_file.get("name"):
|
| 171 |
custom_path = custom_file["name"]
|
| 172 |
+
# Update the background preview for custom uploads
|
| 173 |
+
try:
|
| 174 |
+
img = Image.open(custom_path).convert("RGB")
|
| 175 |
+
current_background_image = np.array(img)
|
| 176 |
+
except Exception:
|
| 177 |
+
pass
|
| 178 |
+
elif not custom_file and style:
|
| 179 |
+
# Generate preview for preset background
|
| 180 |
+
try:
|
| 181 |
+
from utils.cv_processing import create_professional_background
|
| 182 |
+
# Create a preview-sized version of the preset
|
| 183 |
+
current_background_image = create_professional_background(style, 640, 360)
|
| 184 |
+
except Exception:
|
| 185 |
+
pass
|
| 186 |
|
| 187 |
# Fire the core function
|
| 188 |
+
video_path, status = process_video_fixed(
|
| 189 |
video_path=vid,
|
| 190 |
background_choice=style,
|
| 191 |
custom_background_path=custom_path,
|
| 192 |
+
progress_callback=None,
|
| 193 |
use_two_stage=use_two,
|
| 194 |
chroma_preset=chroma,
|
| 195 |
+
key_color_mode=key_color_mode,
|
| 196 |
preview_mask=prev_mask,
|
| 197 |
preview_greenscreen=prev_green,
|
| 198 |
)
|
| 199 |
+
|
| 200 |
+
# Return video, status, AND the background preview to keep it visible
|
| 201 |
+
return video_path, status, current_background_image
|
| 202 |
|
| 203 |
|
| 204 |
# ------------------------------------------------------------------
|
|
|
|
| 218 |
return {"error": str(e)}, {"error": str(e)}
|
| 219 |
|
| 220 |
def cb_clear():
|
| 221 |
+
"""Clear all outputs including the background preview"""
|
| 222 |
+
global current_background_image
|
| 223 |
+
current_background_image = None
|
| 224 |
+
# Return blanks for (out_video, status, gen_preview, gen_path, bg_preview)
|
| 225 |
+
return None, "", None, "", None
|
| 226 |
|
| 227 |
|
| 228 |
# ------------------------------------------------------------------
|
| 229 |
# AI BACKGROUND
|
| 230 |
# ------------------------------------------------------------------
|
| 231 |
def cb_generate_bg(prompt_text: str, w: int, h: int, b: float, v: float, c: float):
|
| 232 |
+
"""Generate AI background and update global preview"""
|
| 233 |
+
global current_background_image
|
| 234 |
img, path = _generate_ai_background(prompt_text, int(w), int(h), b, v, c)
|
| 235 |
+
# Store as numpy array for consistency
|
| 236 |
+
if img:
|
| 237 |
+
current_background_image = np.array(img)
|
| 238 |
return img, path
|
| 239 |
|
| 240 |
def cb_use_gen_bg(path_text: str):
|
| 241 |
+
"""Use generated background as custom and update preview"""
|
| 242 |
+
global current_background_image
|
| 243 |
if path_text and os.path.exists(path_text):
|
| 244 |
+
try:
|
| 245 |
+
img = Image.open(path_text).convert("RGB")
|
| 246 |
+
current_background_image = np.array(img)
|
| 247 |
+
except Exception:
|
| 248 |
+
pass
|
| 249 |
return {"name": path_text, "size": os.path.getsize(path_text)}
|
| 250 |
return None
|
| 251 |
|
|
|
|
| 275 |
def cb_custom_bg_preview(file_obj: dict | None):
|
| 276 |
"""
|
| 277 |
Display the uploaded background image (if any) as a preview.
|
| 278 |
+
Stores it globally so it persists during processing.
|
| 279 |
"""
|
| 280 |
+
global current_background_image
|
| 281 |
try:
|
| 282 |
if file_obj and file_obj.get("name"):
|
| 283 |
img = Image.open(file_obj["name"]).convert("RGB")
|
| 284 |
+
current_background_image = np.array(img)
|
| 285 |
+
return current_background_image
|
| 286 |
except Exception:
|
| 287 |
pass
|
| 288 |
return None
|
| 289 |
+
|
| 290 |
+
def cb_preset_bg_preview(style: str):
|
| 291 |
+
"""
|
| 292 |
+
Generate and display preview for preset backgrounds.
|
| 293 |
+
Called when background style dropdown changes.
|
| 294 |
+
"""
|
| 295 |
+
global current_background_image
|
| 296 |
+
try:
|
| 297 |
+
from utils.cv_processing import create_professional_background
|
| 298 |
+
# Create a preview-sized version
|
| 299 |
+
current_background_image = create_professional_background(style, 640, 360)
|
| 300 |
+
return current_background_image
|
| 301 |
+
except Exception:
|
| 302 |
+
return None
|