Update ui/ui_components.py
Browse files- ui/ui_components.py +19 -81
ui/ui_components.py
CHANGED
|
@@ -22,6 +22,7 @@
|
|
| 22 |
cb_use_gen_bg,
|
| 23 |
cb_video_changed,
|
| 24 |
cb_custom_bg_preview,
|
|
|
|
| 25 |
)
|
| 26 |
|
| 27 |
CSS = """
|
|
@@ -60,9 +61,6 @@ def create_interface() -> gr.Blocks:
|
|
| 60 |
analytics_enabled=False,
|
| 61 |
theme=gr.themes.Soft()
|
| 62 |
) as demo:
|
| 63 |
-
|
| 64 |
-
# State to hold the current background preview
|
| 65 |
-
background_state = gr.State()
|
| 66 |
|
| 67 |
# ------------------------------------------------------------------
|
| 68 |
# HERO
|
|
@@ -105,6 +103,12 @@ def create_interface() -> gr.Blocks:
|
|
| 105 |
interactive=True
|
| 106 |
)
|
| 107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
# ββ Advanced options accordion βββββββββββββββββββββββ
|
| 109 |
with gr.Accordion("Advanced", open=False):
|
| 110 |
use_two_stage = gr.Checkbox(
|
|
@@ -142,15 +146,6 @@ def create_interface() -> gr.Blocks:
|
|
| 142 |
|
| 143 |
# ββ Right column ββββββββββββββββββββββββββββββββββββββββββ
|
| 144 |
with gr.Column(scale=1):
|
| 145 |
-
# Persistent background preview at the top
|
| 146 |
-
gr.Markdown("### π Background Preview")
|
| 147 |
-
persistent_bg_preview = gr.Image(
|
| 148 |
-
label="Selected Background (stays visible during processing)",
|
| 149 |
-
interactive=False,
|
| 150 |
-
elem_classes=["preview-img", "bg-preview-persistent"],
|
| 151 |
-
show_label=True
|
| 152 |
-
)
|
| 153 |
-
|
| 154 |
out_video = gr.Video(label="Processed Output", interactive=False)
|
| 155 |
statusbox = gr.Textbox(label="Status", lines=8, elem_id="statusbox")
|
| 156 |
|
|
@@ -209,36 +204,9 @@ def create_interface() -> gr.Blocks:
|
|
| 209 |
# ------------------------------------------------------------------
|
| 210 |
# Callback wiring
|
| 211 |
# ------------------------------------------------------------------
|
| 212 |
-
|
| 213 |
-
# Modified callback to update persistent preview when custom background changes
|
| 214 |
-
def update_persistent_preview(custom_bg_file):
|
| 215 |
-
"""Update both the temporary preview and persistent preview"""
|
| 216 |
-
if custom_bg_file is None:
|
| 217 |
-
return None, None, None
|
| 218 |
-
try:
|
| 219 |
-
import cv2
|
| 220 |
-
img = cv2.imread(custom_bg_file.name)
|
| 221 |
-
if img is not None:
|
| 222 |
-
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 223 |
-
return img_rgb, img_rgb, custom_bg_file # Return for both previews and state
|
| 224 |
-
except:
|
| 225 |
-
pass
|
| 226 |
-
return None, None, None
|
| 227 |
-
|
| 228 |
-
# Update persistent preview when background style changes (for presets)
|
| 229 |
-
def update_preset_preview(bg_style_choice):
|
| 230 |
-
"""Generate preview for preset backgrounds"""
|
| 231 |
-
try:
|
| 232 |
-
from utils.cv_processing import create_professional_background
|
| 233 |
-
# Create a small preview version
|
| 234 |
-
preview_bg = create_professional_background(bg_style_choice, 640, 360)
|
| 235 |
-
return preview_bg
|
| 236 |
-
except:
|
| 237 |
-
return None
|
| 238 |
-
|
| 239 |
btn_load.click(cb_load_models, outputs=statusbox)
|
| 240 |
|
| 241 |
-
#
|
| 242 |
btn_run.click(
|
| 243 |
cb_process_video,
|
| 244 |
inputs=[
|
|
@@ -251,16 +219,16 @@ def update_preset_preview(bg_style_choice):
|
|
| 251 |
preview_mask,
|
| 252 |
preview_greenscreen,
|
| 253 |
],
|
| 254 |
-
outputs=[out_video, statusbox], #
|
| 255 |
)
|
| 256 |
|
| 257 |
btn_cancel.click(cb_cancel, outputs=statusbox)
|
| 258 |
btn_refresh.click(cb_status, outputs=[model_status, cache_status])
|
| 259 |
|
| 260 |
-
#
|
| 261 |
btn_clear.click(
|
| 262 |
cb_clear,
|
| 263 |
-
outputs=[out_video, statusbox, gen_preview, gen_path,
|
| 264 |
)
|
| 265 |
|
| 266 |
# AI background generation
|
|
@@ -270,54 +238,24 @@ def update_preset_preview(bg_style_choice):
|
|
| 270 |
outputs=[gen_preview, gen_path],
|
| 271 |
)
|
| 272 |
|
| 273 |
-
|
| 274 |
-
def use_generated_as_custom(gen_path_value):
|
| 275 |
-
"""Use generated background as custom and update persistent preview"""
|
| 276 |
-
if gen_path_value:
|
| 277 |
-
try:
|
| 278 |
-
import cv2
|
| 279 |
-
img = cv2.imread(gen_path_value)
|
| 280 |
-
if img is not None:
|
| 281 |
-
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 282 |
-
# Create a file-like object for the custom_bg component
|
| 283 |
-
import tempfile
|
| 284 |
-
import shutil
|
| 285 |
-
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png')
|
| 286 |
-
cv2.imwrite(temp_file.name, img)
|
| 287 |
-
temp_file.close()
|
| 288 |
-
return temp_file.name, img_rgb
|
| 289 |
-
except:
|
| 290 |
-
pass
|
| 291 |
-
return None, None
|
| 292 |
-
|
| 293 |
-
use_gen_as_custom.click(
|
| 294 |
-
use_generated_as_custom,
|
| 295 |
-
inputs=[gen_path],
|
| 296 |
-
outputs=[custom_bg, persistent_bg_preview]
|
| 297 |
-
)
|
| 298 |
|
| 299 |
# Live previews
|
| 300 |
video.change(cb_video_changed, inputs=[video], outputs=[video_preview])
|
|
|
|
| 301 |
|
| 302 |
-
# Update
|
| 303 |
-
custom_bg.change(
|
| 304 |
-
update_persistent_preview,
|
| 305 |
-
inputs=[custom_bg],
|
| 306 |
-
outputs=[video_preview, persistent_bg_preview, background_state]
|
| 307 |
-
)
|
| 308 |
-
|
| 309 |
-
# Update persistent preview when background style dropdown changes
|
| 310 |
bg_style.change(
|
| 311 |
-
|
| 312 |
inputs=[bg_style],
|
| 313 |
-
outputs=[
|
| 314 |
)
|
| 315 |
|
| 316 |
-
# Initialize with default background preview
|
| 317 |
demo.load(
|
| 318 |
-
|
| 319 |
inputs=[bg_style],
|
| 320 |
-
outputs=[
|
| 321 |
)
|
| 322 |
|
| 323 |
return demo
|
|
|
|
| 22 |
cb_use_gen_bg,
|
| 23 |
cb_video_changed,
|
| 24 |
cb_custom_bg_preview,
|
| 25 |
+
cb_preset_bg_preview, # Added import for preset preview
|
| 26 |
)
|
| 27 |
|
| 28 |
CSS = """
|
|
|
|
| 61 |
analytics_enabled=False,
|
| 62 |
theme=gr.themes.Soft()
|
| 63 |
) as demo:
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
# ------------------------------------------------------------------
|
| 66 |
# HERO
|
|
|
|
| 103 |
interactive=True
|
| 104 |
)
|
| 105 |
|
| 106 |
+
custom_bg_preview_img = gr.Image(
|
| 107 |
+
label="Custom Background Preview",
|
| 108 |
+
interactive=False,
|
| 109 |
+
elem_classes=["preview-img"]
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
# ββ Advanced options accordion βββββββββββββββββββββββ
|
| 113 |
with gr.Accordion("Advanced", open=False):
|
| 114 |
use_two_stage = gr.Checkbox(
|
|
|
|
| 146 |
|
| 147 |
# ββ Right column ββββββββββββββββββββββββββββββββββββββββββ
|
| 148 |
with gr.Column(scale=1):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
out_video = gr.Video(label="Processed Output", interactive=False)
|
| 150 |
statusbox = gr.Textbox(label="Status", lines=8, elem_id="statusbox")
|
| 151 |
|
|
|
|
| 204 |
# ------------------------------------------------------------------
|
| 205 |
# Callback wiring
|
| 206 |
# ------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
btn_load.click(cb_load_models, outputs=statusbox)
|
| 208 |
|
| 209 |
+
# CRITICAL FIX: Process button now expects THREE outputs from callback
|
| 210 |
btn_run.click(
|
| 211 |
cb_process_video,
|
| 212 |
inputs=[
|
|
|
|
| 219 |
preview_mask,
|
| 220 |
preview_greenscreen,
|
| 221 |
],
|
| 222 |
+
outputs=[out_video, statusbox, custom_bg_preview_img], # THREE outputs now!
|
| 223 |
)
|
| 224 |
|
| 225 |
btn_cancel.click(cb_cancel, outputs=statusbox)
|
| 226 |
btn_refresh.click(cb_status, outputs=[model_status, cache_status])
|
| 227 |
|
| 228 |
+
# Clear button - adjusted for 5 outputs from callback
|
| 229 |
btn_clear.click(
|
| 230 |
cb_clear,
|
| 231 |
+
outputs=[out_video, statusbox, gen_preview, gen_path, custom_bg_preview_img]
|
| 232 |
)
|
| 233 |
|
| 234 |
# AI background generation
|
|
|
|
| 238 |
outputs=[gen_preview, gen_path],
|
| 239 |
)
|
| 240 |
|
| 241 |
+
use_gen_as_custom.click(cb_use_gen_bg, inputs=[gen_path], outputs=[custom_bg])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
|
| 243 |
# Live previews
|
| 244 |
video.change(cb_video_changed, inputs=[video], outputs=[video_preview])
|
| 245 |
+
custom_bg.change(cb_custom_bg_preview, inputs=[custom_bg], outputs=[custom_bg_preview_img])
|
| 246 |
|
| 247 |
+
# Update preview when background style dropdown changes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
bg_style.change(
|
| 249 |
+
cb_preset_bg_preview,
|
| 250 |
inputs=[bg_style],
|
| 251 |
+
outputs=[custom_bg_preview_img]
|
| 252 |
)
|
| 253 |
|
| 254 |
+
# Initialize with default background preview on load
|
| 255 |
demo.load(
|
| 256 |
+
cb_preset_bg_preview,
|
| 257 |
inputs=[bg_style],
|
| 258 |
+
outputs=[custom_bg_preview_img]
|
| 259 |
)
|
| 260 |
|
| 261 |
return demo
|