Update ui/callbacks.py
Browse files- ui/callbacks.py +9 -47
ui/callbacks.py
CHANGED
|
@@ -8,7 +8,7 @@
|
|
| 8 |
• PREVIEW FUNCTIONS NOW IMPLEMENTED:
|
| 9 |
- cb_video_changed → returns first frame
|
| 10 |
- cb_custom_bg_preview → returns uploaded image
|
| 11 |
-
•
|
| 12 |
"""
|
| 13 |
|
| 14 |
from __future__ import annotations
|
|
@@ -33,9 +33,6 @@
|
|
| 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)
|
|
@@ -156,11 +153,9 @@ def cb_process_video(
|
|
| 156 |
):
|
| 157 |
"""
|
| 158 |
Runs the two-stage (or single-stage) pipeline and returns:
|
| 159 |
-
(processed_video_path | None, status_message:str
|
| 160 |
-
|
| 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,23 +164,9 @@ def cb_process_video(
|
|
| 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 |
-
|
| 189 |
video_path=vid,
|
| 190 |
background_choice=style,
|
| 191 |
custom_background_path=custom_path,
|
|
@@ -196,9 +177,6 @@ def cb_process_video(
|
|
| 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 |
# ------------------------------------------------------------------
|
|
@@ -219,8 +197,6 @@ def cb_status() -> Tuple[Dict[str, Any], Dict[str, Any]]:
|
|
| 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 |
|
|
@@ -229,23 +205,13 @@ def cb_clear():
|
|
| 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
|
| 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
|
| 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,14 +241,11 @@ def cb_video_changed(vid_path: str):
|
|
| 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 |
-
|
| 285 |
-
return current_background_image
|
| 286 |
except Exception:
|
| 287 |
pass
|
| 288 |
return None
|
|
@@ -292,11 +255,10 @@ def cb_preset_bg_preview(style: str):
|
|
| 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 |
-
|
| 300 |
-
return
|
| 301 |
except Exception:
|
| 302 |
return None
|
|
|
|
| 8 |
• PREVIEW FUNCTIONS NOW IMPLEMENTED:
|
| 9 |
- cb_video_changed → returns first frame
|
| 10 |
- cb_custom_bg_preview → returns uploaded image
|
| 11 |
+
• FIXED: Returns only 2 values for process_video (don't touch preview)
|
| 12 |
"""
|
| 13 |
|
| 14 |
from __future__ import annotations
|
|
|
|
| 33 |
except Exception:
|
| 34 |
pass
|
| 35 |
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# ------------------------------------------------------------------
|
| 38 |
# LIGHTWEIGHT BG GENERATOR (inline fallback)
|
|
|
|
| 153 |
):
|
| 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()
|
|
|
|
| 164 |
custom_path = None
|
| 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=custom_path,
|
|
|
|
| 177 |
preview_mask=prev_mask,
|
| 178 |
preview_greenscreen=prev_green,
|
| 179 |
)
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
|
| 182 |
# ------------------------------------------------------------------
|
|
|
|
| 197 |
|
| 198 |
def cb_clear():
|
| 199 |
"""Clear all outputs including the background preview"""
|
|
|
|
|
|
|
| 200 |
# Return blanks for (out_video, status, gen_preview, gen_path, bg_preview)
|
| 201 |
return None, "", None, "", None
|
| 202 |
|
|
|
|
| 205 |
# AI BACKGROUND
|
| 206 |
# ------------------------------------------------------------------
|
| 207 |
def cb_generate_bg(prompt_text: str, w: int, h: int, b: float, v: float, c: float):
|
| 208 |
+
"""Generate AI background"""
|
|
|
|
| 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(path_text: str):
|
| 213 |
+
"""Use generated background as custom"""
|
|
|
|
| 214 |
if path_text and os.path.exists(path_text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
return {"name": path_text, "size": os.path.getsize(path_text)}
|
| 216 |
return None
|
| 217 |
|
|
|
|
| 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
|
|
|
|
| 255 |
Generate and display preview for preset backgrounds.
|
| 256 |
Called when background style dropdown changes.
|
| 257 |
"""
|
|
|
|
| 258 |
try:
|
| 259 |
from utils.cv_processing import create_professional_background
|
| 260 |
# Create a preview-sized version
|
| 261 |
+
preview_bg = create_professional_background(style, 640, 360)
|
| 262 |
+
return preview_bg
|
| 263 |
except Exception:
|
| 264 |
return None
|