Update ui_components.py
Browse files- ui_components.py +109 -27
ui_components.py
CHANGED
|
@@ -2,6 +2,7 @@
|
|
| 2 |
"""
|
| 3 |
FIXED UI Components for Video Background Replacement
|
| 4 |
Updated to use the fixed core processing functions with working SAM2 + MatAnyone
|
|
|
|
| 5 |
"""
|
| 6 |
|
| 7 |
import gradio as gr
|
|
@@ -13,9 +14,17 @@
|
|
| 13 |
from typing import Optional
|
| 14 |
|
| 15 |
# Import FIXED core processing functions
|
| 16 |
-
from app import load_models_with_validation, process_video_fixed, get_model_status
|
| 17 |
from utilities import PROFESSIONAL_BACKGROUNDS, create_procedural_background
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
logger = logging.getLogger(__name__)
|
| 20 |
|
| 21 |
# ============================================================================ #
|
|
@@ -66,12 +75,17 @@ def update_cache_status():
|
|
| 66 |
"""Update cache status display with actual model state - FIXED"""
|
| 67 |
try:
|
| 68 |
status = get_model_status()
|
|
|
|
| 69 |
|
| 70 |
sam2_status = f"β
Loaded & Validated" if status['sam2'] == 'Ready' else "β Not loaded"
|
| 71 |
matanyone_status = f"β
Loaded & Validated" if status['matanyone'] == 'Ready' else "β Not loaded"
|
| 72 |
models_status = "β
Models Validated" if status['validated'] else "β Not validated"
|
|
|
|
| 73 |
|
| 74 |
-
return f"SAM2: {sam2_status}\
|
|
|
|
|
|
|
|
|
|
| 75 |
except Exception as e:
|
| 76 |
return f"Status check error: {str(e)}"
|
| 77 |
|
|
@@ -87,14 +101,15 @@ def gradio_progress_wrapper(progress_obj, pct, desc):
|
|
| 87 |
pass
|
| 88 |
|
| 89 |
# ============================================================================ #
|
| 90 |
-
# MAIN PROCESSING FUNCTION FOR UI (FIXED)
|
| 91 |
# ============================================================================ #
|
| 92 |
def process_video_enhanced_fixed(
|
| 93 |
video_path, bg_method, custom_img, prof_choice, grad_type,
|
| 94 |
color1, color2, color3, use_third, ai_prompt, ai_style, ai_img,
|
|
|
|
| 95 |
progress: Optional[gr.Progress] = None
|
| 96 |
):
|
| 97 |
-
"""FIXED enhanced video processing function
|
| 98 |
if not video_path:
|
| 99 |
return None, "No video file provided."
|
| 100 |
|
|
@@ -102,14 +117,22 @@ def progress_callback(pct, desc):
|
|
| 102 |
gradio_progress_wrapper(progress, pct, desc)
|
| 103 |
|
| 104 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
if bg_method == "upload":
|
| 106 |
if custom_img and os.path.exists(custom_img):
|
| 107 |
-
return process_video_fixed(video_path, "custom", custom_img,
|
|
|
|
|
|
|
| 108 |
return None, "No image uploaded. Please upload a background image."
|
| 109 |
|
| 110 |
elif bg_method == "professional":
|
| 111 |
if prof_choice and prof_choice in PROFESSIONAL_BACKGROUNDS:
|
| 112 |
-
return process_video_fixed(video_path, prof_choice, None,
|
|
|
|
|
|
|
| 113 |
return None, f"Invalid professional background: {prof_choice}"
|
| 114 |
|
| 115 |
elif bg_method == "colors":
|
|
@@ -127,13 +150,17 @@ def progress_callback(pct, desc):
|
|
| 127 |
gradient_bg = create_professional_background(bg_config, 1920, 1080)
|
| 128 |
temp_path = f"/tmp/gradient_{int(time.time())}.png"
|
| 129 |
cv2.imwrite(temp_path, gradient_bg)
|
| 130 |
-
return process_video_fixed(video_path, "custom", temp_path,
|
|
|
|
|
|
|
| 131 |
except Exception as e:
|
| 132 |
return None, f"Error creating gradient: {str(e)}"
|
| 133 |
|
| 134 |
elif bg_method == "ai":
|
| 135 |
if ai_img and os.path.exists(ai_img):
|
| 136 |
-
return process_video_fixed(video_path, "custom", ai_img,
|
|
|
|
|
|
|
| 137 |
return None, "No AI background generated. Click 'Generate Background' first."
|
| 138 |
|
| 139 |
else:
|
|
@@ -151,10 +178,10 @@ def progress_callback(pct, desc):
|
|
| 151 |
return load_models_with_validation(progress_callback)
|
| 152 |
|
| 153 |
# ============================================================================ #
|
| 154 |
-
# MAIN INTERFACE CREATION (FIXED)
|
| 155 |
# ============================================================================ #
|
| 156 |
def create_interface():
|
| 157 |
-
"""Create the complete Gradio interface with FIXED processing"""
|
| 158 |
|
| 159 |
with gr.Blocks(
|
| 160 |
title="FIXED High-Quality Video Background Replacement",
|
|
@@ -163,11 +190,28 @@ def create_interface():
|
|
| 163 |
.gradio-container { max-width: 1200px !important; }
|
| 164 |
.progress-bar { background: linear-gradient(90deg, #3498db, #2ecc71) !important; }
|
| 165 |
.status-box { background-color: #f8f9fa; border: 1px solid #dee2e6; border-radius: 6px; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
"""
|
| 167 |
) as demo:
|
| 168 |
gr.Markdown("# FIXED Cinema-Quality Video Background Replacement")
|
| 169 |
gr.Markdown("**Upload a video β Choose a background β Get professional results with VALIDATED AI**")
|
| 170 |
gr.Markdown("*Powered by PROPERLY INTEGRATED SAM2 + MatAnyone with comprehensive validation*")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
gr.Markdown("---")
|
| 172 |
|
| 173 |
with gr.Row():
|
|
@@ -239,11 +283,35 @@ def create_interface():
|
|
| 239 |
outputs=[upload_group, professional_group, colors_group, ai_group]
|
| 240 |
)
|
| 241 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
gr.Markdown("### Processing Controls")
|
| 243 |
gr.Markdown("*First load and validate the AI models, then process your video*")
|
| 244 |
with gr.Row():
|
| 245 |
load_models_btn = gr.Button("Step 1: Load & Validate AI Models", variant="secondary")
|
| 246 |
-
process_btn = gr.Button("Step 2: Process Video
|
| 247 |
|
| 248 |
status_text = gr.Textbox(
|
| 249 |
label="System Status",
|
|
@@ -258,19 +326,19 @@ def create_interface():
|
|
| 258 |
label="AI Models Validation Status",
|
| 259 |
value=update_cache_status(),
|
| 260 |
interactive=False,
|
| 261 |
-
lines=
|
| 262 |
elem_classes=["status-box"]
|
| 263 |
)
|
| 264 |
|
| 265 |
with gr.Column(scale=1):
|
| 266 |
gr.Markdown("### Your Results")
|
| 267 |
-
gr.Markdown("*
|
| 268 |
-
video_output = gr.Video(label="Your
|
| 269 |
result_text = gr.Textbox(
|
| 270 |
label="Processing Results",
|
| 271 |
interactive=False,
|
| 272 |
-
lines=
|
| 273 |
-
placeholder="
|
| 274 |
elem_classes=["status-box"]
|
| 275 |
)
|
| 276 |
|
|
@@ -305,11 +373,13 @@ def create_interface():
|
|
| 305 |
outputs=[ai_generated_image, status_text]
|
| 306 |
)
|
| 307 |
|
|
|
|
| 308 |
process_btn.click(
|
| 309 |
fn=process_video_enhanced_fixed,
|
| 310 |
inputs=[video_input, background_method, custom_background, professional_choice,
|
| 311 |
gradient_type, color1, color2, color3, use_third_color,
|
| 312 |
-
ai_prompt, ai_style, ai_generated_image
|
|
|
|
| 313 |
outputs=[video_output, result_text]
|
| 314 |
)
|
| 315 |
|
|
@@ -319,19 +389,31 @@ def create_interface():
|
|
| 319 |
outputs=[validation_status]
|
| 320 |
)
|
| 321 |
|
| 322 |
-
with gr.Accordion("
|
| 323 |
gr.Markdown("""
|
| 324 |
-
###
|
| 325 |
-
|
| 326 |
-
**
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 332 |
""")
|
| 333 |
|
| 334 |
gr.Markdown("---")
|
| 335 |
-
gr.Markdown("*
|
| 336 |
|
| 337 |
return demo
|
|
|
|
| 2 |
"""
|
| 3 |
FIXED UI Components for Video Background Replacement
|
| 4 |
Updated to use the fixed core processing functions with working SAM2 + MatAnyone
|
| 5 |
+
Now includes TWO-STAGE processing option
|
| 6 |
"""
|
| 7 |
|
| 8 |
import gradio as gr
|
|
|
|
| 14 |
from typing import Optional
|
| 15 |
|
| 16 |
# Import FIXED core processing functions
|
| 17 |
+
from app import load_models_with_validation, process_video_fixed, get_model_status, get_cache_status
|
| 18 |
from utilities import PROFESSIONAL_BACKGROUNDS, create_procedural_background
|
| 19 |
|
| 20 |
+
# Check if two-stage is available
|
| 21 |
+
try:
|
| 22 |
+
from two_stage_processor import CHROMA_PRESETS
|
| 23 |
+
TWO_STAGE_AVAILABLE = True
|
| 24 |
+
except ImportError:
|
| 25 |
+
TWO_STAGE_AVAILABLE = False
|
| 26 |
+
CHROMA_PRESETS = {'standard': {}}
|
| 27 |
+
|
| 28 |
logger = logging.getLogger(__name__)
|
| 29 |
|
| 30 |
# ============================================================================ #
|
|
|
|
| 75 |
"""Update cache status display with actual model state - FIXED"""
|
| 76 |
try:
|
| 77 |
status = get_model_status()
|
| 78 |
+
cache = get_cache_status()
|
| 79 |
|
| 80 |
sam2_status = f"β
Loaded & Validated" if status['sam2'] == 'Ready' else "β Not loaded"
|
| 81 |
matanyone_status = f"β
Loaded & Validated" if status['matanyone'] == 'Ready' else "β Not loaded"
|
| 82 |
models_status = "β
Models Validated" if status['validated'] else "β Not validated"
|
| 83 |
+
two_stage_status = "β
Available" if cache.get('two_stage_available', False) else "β Not available"
|
| 84 |
|
| 85 |
+
return (f"SAM2: {sam2_status}\n"
|
| 86 |
+
f"MatAnyone: {matanyone_status}\n"
|
| 87 |
+
f"Two-Stage: {two_stage_status}\n"
|
| 88 |
+
f"Status: {models_status}")
|
| 89 |
except Exception as e:
|
| 90 |
return f"Status check error: {str(e)}"
|
| 91 |
|
|
|
|
| 101 |
pass
|
| 102 |
|
| 103 |
# ============================================================================ #
|
| 104 |
+
# MAIN PROCESSING FUNCTION FOR UI (FIXED WITH TWO-STAGE)
|
| 105 |
# ============================================================================ #
|
| 106 |
def process_video_enhanced_fixed(
|
| 107 |
video_path, bg_method, custom_img, prof_choice, grad_type,
|
| 108 |
color1, color2, color3, use_third, ai_prompt, ai_style, ai_img,
|
| 109 |
+
use_two_stage, chroma_preset, # NEW PARAMETERS
|
| 110 |
progress: Optional[gr.Progress] = None
|
| 111 |
):
|
| 112 |
+
"""FIXED enhanced video processing function with two-stage option"""
|
| 113 |
if not video_path:
|
| 114 |
return None, "No video file provided."
|
| 115 |
|
|
|
|
| 117 |
gradio_progress_wrapper(progress, pct, desc)
|
| 118 |
|
| 119 |
try:
|
| 120 |
+
# Determine the processing mode message
|
| 121 |
+
mode_msg = "TWO-STAGE Green Screen" if use_two_stage else "Single-Stage"
|
| 122 |
+
logger.info(f"Processing with {mode_msg} mode")
|
| 123 |
+
|
| 124 |
if bg_method == "upload":
|
| 125 |
if custom_img and os.path.exists(custom_img):
|
| 126 |
+
return process_video_fixed(video_path, "custom", custom_img,
|
| 127 |
+
progress_callback, use_two_stage=use_two_stage,
|
| 128 |
+
chroma_preset=chroma_preset)
|
| 129 |
return None, "No image uploaded. Please upload a background image."
|
| 130 |
|
| 131 |
elif bg_method == "professional":
|
| 132 |
if prof_choice and prof_choice in PROFESSIONAL_BACKGROUNDS:
|
| 133 |
+
return process_video_fixed(video_path, prof_choice, None,
|
| 134 |
+
progress_callback, use_two_stage=use_two_stage,
|
| 135 |
+
chroma_preset=chroma_preset)
|
| 136 |
return None, f"Invalid professional background: {prof_choice}"
|
| 137 |
|
| 138 |
elif bg_method == "colors":
|
|
|
|
| 150 |
gradient_bg = create_professional_background(bg_config, 1920, 1080)
|
| 151 |
temp_path = f"/tmp/gradient_{int(time.time())}.png"
|
| 152 |
cv2.imwrite(temp_path, gradient_bg)
|
| 153 |
+
return process_video_fixed(video_path, "custom", temp_path,
|
| 154 |
+
progress_callback, use_two_stage=use_two_stage,
|
| 155 |
+
chroma_preset=chroma_preset)
|
| 156 |
except Exception as e:
|
| 157 |
return None, f"Error creating gradient: {str(e)}"
|
| 158 |
|
| 159 |
elif bg_method == "ai":
|
| 160 |
if ai_img and os.path.exists(ai_img):
|
| 161 |
+
return process_video_fixed(video_path, "custom", ai_img,
|
| 162 |
+
progress_callback, use_two_stage=use_two_stage,
|
| 163 |
+
chroma_preset=chroma_preset)
|
| 164 |
return None, "No AI background generated. Click 'Generate Background' first."
|
| 165 |
|
| 166 |
else:
|
|
|
|
| 178 |
return load_models_with_validation(progress_callback)
|
| 179 |
|
| 180 |
# ============================================================================ #
|
| 181 |
+
# MAIN INTERFACE CREATION (FIXED WITH TWO-STAGE)
|
| 182 |
# ============================================================================ #
|
| 183 |
def create_interface():
|
| 184 |
+
"""Create the complete Gradio interface with FIXED processing and TWO-STAGE option"""
|
| 185 |
|
| 186 |
with gr.Blocks(
|
| 187 |
title="FIXED High-Quality Video Background Replacement",
|
|
|
|
| 190 |
.gradio-container { max-width: 1200px !important; }
|
| 191 |
.progress-bar { background: linear-gradient(90deg, #3498db, #2ecc71) !important; }
|
| 192 |
.status-box { background-color: #f8f9fa; border: 1px solid #dee2e6; border-radius: 6px; }
|
| 193 |
+
.two-stage-box {
|
| 194 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 195 |
+
color: white;
|
| 196 |
+
padding: 10px;
|
| 197 |
+
border-radius: 8px;
|
| 198 |
+
margin: 10px 0;
|
| 199 |
+
}
|
| 200 |
"""
|
| 201 |
) as demo:
|
| 202 |
gr.Markdown("# FIXED Cinema-Quality Video Background Replacement")
|
| 203 |
gr.Markdown("**Upload a video β Choose a background β Get professional results with VALIDATED AI**")
|
| 204 |
gr.Markdown("*Powered by PROPERLY INTEGRATED SAM2 + MatAnyone with comprehensive validation*")
|
| 205 |
+
|
| 206 |
+
# Two-stage mode banner if available
|
| 207 |
+
if TWO_STAGE_AVAILABLE:
|
| 208 |
+
gr.HTML("""
|
| 209 |
+
<div class="two-stage-box">
|
| 210 |
+
<strong>π¬ NEW: Two-Stage Green Screen Mode Available!</strong><br>
|
| 211 |
+
Professional cinema-grade chroma key processing for perfect edges
|
| 212 |
+
</div>
|
| 213 |
+
""")
|
| 214 |
+
|
| 215 |
gr.Markdown("---")
|
| 216 |
|
| 217 |
with gr.Row():
|
|
|
|
| 283 |
outputs=[upload_group, professional_group, colors_group, ai_group]
|
| 284 |
)
|
| 285 |
|
| 286 |
+
gr.Markdown("### Processing Options")
|
| 287 |
+
|
| 288 |
+
# Two-stage processing options
|
| 289 |
+
with gr.Accordion("π¬ Advanced: Two-Stage Processing", open=False):
|
| 290 |
+
use_two_stage = gr.Checkbox(
|
| 291 |
+
label="Enable Two-Stage Green Screen Processing",
|
| 292 |
+
value=False,
|
| 293 |
+
info="Creates green screen intermediate for cinema-quality edges"
|
| 294 |
+
)
|
| 295 |
+
chroma_preset = gr.Dropdown(
|
| 296 |
+
choices=list(CHROMA_PRESETS.keys()) if TWO_STAGE_AVAILABLE else ["standard"],
|
| 297 |
+
value="standard",
|
| 298 |
+
label="Chroma Key Preset",
|
| 299 |
+
info="Edge quality: standard=balanced, tight=sharp, soft=smooth",
|
| 300 |
+
visible=False
|
| 301 |
+
)
|
| 302 |
+
|
| 303 |
+
# Show/hide chroma preset based on two-stage checkbox
|
| 304 |
+
use_two_stage.change(
|
| 305 |
+
fn=lambda x: gr.update(visible=x),
|
| 306 |
+
inputs=use_two_stage,
|
| 307 |
+
outputs=chroma_preset
|
| 308 |
+
)
|
| 309 |
+
|
| 310 |
gr.Markdown("### Processing Controls")
|
| 311 |
gr.Markdown("*First load and validate the AI models, then process your video*")
|
| 312 |
with gr.Row():
|
| 313 |
load_models_btn = gr.Button("Step 1: Load & Validate AI Models", variant="secondary")
|
| 314 |
+
process_btn = gr.Button("Step 2: Process Video", variant="primary")
|
| 315 |
|
| 316 |
status_text = gr.Textbox(
|
| 317 |
label="System Status",
|
|
|
|
| 326 |
label="AI Models Validation Status",
|
| 327 |
value=update_cache_status(),
|
| 328 |
interactive=False,
|
| 329 |
+
lines=4,
|
| 330 |
elem_classes=["status-box"]
|
| 331 |
)
|
| 332 |
|
| 333 |
with gr.Column(scale=1):
|
| 334 |
gr.Markdown("### Your Results")
|
| 335 |
+
gr.Markdown("*Processed video with validated SAM2 + MatAnyone will appear here*")
|
| 336 |
+
video_output = gr.Video(label="Your Processed Video", height=400)
|
| 337 |
result_text = gr.Textbox(
|
| 338 |
label="Processing Results",
|
| 339 |
interactive=False,
|
| 340 |
+
lines=10,
|
| 341 |
+
placeholder="Processing status and detailed results will appear here...",
|
| 342 |
elem_classes=["status-box"]
|
| 343 |
)
|
| 344 |
|
|
|
|
| 373 |
outputs=[ai_generated_image, status_text]
|
| 374 |
)
|
| 375 |
|
| 376 |
+
# Updated process button with two-stage parameters
|
| 377 |
process_btn.click(
|
| 378 |
fn=process_video_enhanced_fixed,
|
| 379 |
inputs=[video_input, background_method, custom_background, professional_choice,
|
| 380 |
gradient_type, color1, color2, color3, use_third_color,
|
| 381 |
+
ai_prompt, ai_style, ai_generated_image,
|
| 382 |
+
use_two_stage, chroma_preset], # Added two-stage parameters
|
| 383 |
outputs=[video_output, result_text]
|
| 384 |
)
|
| 385 |
|
|
|
|
| 389 |
outputs=[validation_status]
|
| 390 |
)
|
| 391 |
|
| 392 |
+
with gr.Accordion("Quality & Features", open=False):
|
| 393 |
gr.Markdown("""
|
| 394 |
+
### Cinema-Quality Features:
|
| 395 |
+
|
| 396 |
+
**Single-Stage Mode (Default):**
|
| 397 |
+
- Original β Final Background
|
| 398 |
+
- Fast processing with good quality
|
| 399 |
+
- Edge threshold: 140, feather: 1px
|
| 400 |
+
- Morphological edge cleaning
|
| 401 |
+
|
| 402 |
+
**Two-Stage Mode (Professional):**
|
| 403 |
+
- Stage 1: Original β Green Screen
|
| 404 |
+
- Stage 2: Green Screen β Final (Chroma Key)
|
| 405 |
+
- Perfect edges with spill suppression
|
| 406 |
+
- Adjustable tolerance and softness
|
| 407 |
+
- Cinema-grade quality
|
| 408 |
+
|
| 409 |
+
**Core Technology:**
|
| 410 |
+
- SAM2 with multi-point strategy
|
| 411 |
+
- MatAnyone refinement every 3 frames
|
| 412 |
+
- Enhanced OpenCV fallbacks
|
| 413 |
+
- H.264 CRF 18, AAC 192kbps audio
|
| 414 |
""")
|
| 415 |
|
| 416 |
gr.Markdown("---")
|
| 417 |
+
gr.Markdown("*Cinema-Quality Video Background Replacement β Validated SAM2 + MatAnyone pipeline*")
|
| 418 |
|
| 419 |
return demo
|