File size: 14,587 Bytes
dd1ef11
 
239315b
 
 
 
 
 
 
 
66fefac
 
 
 
dd1ef11
85287ea
239315b
 
 
dd1ef11
239315b
 
 
 
 
 
 
66fefac
239315b
66fefac
239315b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66fefac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
239315b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66fefac
 
 
 
 
239315b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66fefac
239315b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66fefac
239315b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85287ea
239315b
 
 
 
66fefac
239315b
66fefac
 
 
239315b
 
 
66fefac
 
 
 
 
 
239315b
 
dd1ef11
239315b
66fefac
239315b
 
 
66fefac
239315b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66fefac
 
 
 
239315b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66fefac
239315b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66fefac
239315b
 
 
 
 
 
 
 
66fefac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
239315b
 
 
 
 
 
 
64677e7
239315b
 
66fefac
f0f27f4
239315b
 
 
 
 
66fefac
 
 
 
 
f0f27f4
85287ea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/usr/bin/env python3
"""
UI Components for BackgroundFX Pro (Hugging Face Spaces, CSP-safe)

- Clean, modern layout with tabs
- Keeps existing functionality:
    * Load models
    * Process video (single-stage / two-stage switch, previews, etc.)
    * Status panel
- Adds lightweight "AI Background" generator (procedural, no heavy deps)
- NEW:
    * Preview of uploaded custom background
    * Preview of the video's first frame when a video is uploaded
    * Background style keys aligned with utils.cv_processing.PROFESSIONAL_BACKGROUNDS
"""

from __future__ import annotations

import os
import time
import random
from pathlib import Path
from typing import Optional, Tuple, Dict, Any, List

import gradio as gr
from PIL import Image, ImageFilter, ImageOps
import numpy as np
import cv2

# Import core wrappers (core/app.py only imports UI from inside main(), no circular import)
from core.app import (
    load_models_with_validation,
    process_video_fixed,
    get_model_status,
    get_cache_status,
    PROCESS_CANCELLED,
)

# --------------------------
# Helpers: file paths, io
# --------------------------

TMP_DIR = Path("/tmp/bgfx")
TMP_DIR.mkdir(parents=True, exist_ok=True)


def _save_pil(img: Image.Image, stem: str = "gen_bg", ext: str = "png") -> str:
    ts = int(time.time() * 1000)
    p = TMP_DIR / f"{stem}_{ts}.{ext}"
    img.save(p)
    return str(p)


def _pil_from_path(path: str) -> Optional[Image.Image]:
    try:
        return Image.open(path).convert("RGB")
    except Exception:
        return None


def _first_frame(path: str, max_side: int = 960) -> Optional[Image.Image]:
    """Extract the first frame of a video for preview."""
    try:
        cap = cv2.VideoCapture(path)
        ok, frame = cap.read()
        cap.release()
        if not ok or frame is None:
            return None
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        h, w = frame.shape[:2]
        scale = min(1.0, max_side / max(h, w))
        if scale < 1.0:
            frame = cv2.resize(frame, (int(w * scale), int(h * scale)), interpolation=cv2.INTER_AREA)
        return Image.fromarray(frame)
    except Exception:
        return None


# --------------------------
# Lightweight "AI" background generator
# --------------------------

_PALETTES = {
    "office": [(240, 245, 250), (210, 220, 230), (180, 190, 200)],
    "studio": [(18, 18, 20), (32, 32, 36), (58, 60, 64)],
    "sunset": [(255, 183, 77), (255, 138, 101), (244, 143, 177)],
    "forest": [(46, 125, 50), (102, 187, 106), (165, 214, 167)],
    "ocean": [(33, 150, 243), (3, 169, 244), (0, 188, 212)],
    "minimal": [(245, 246, 248), (230, 232, 236), (214, 218, 224)],
    "warm": [(255, 224, 178), (255, 204, 128), (255, 171, 145)],
    "cool": [(197, 202, 233), (179, 229, 252), (178, 235, 242)],
    "royal": [(63, 81, 181), (121, 134, 203), (159, 168, 218)],
}

def _palette_from_prompt(prompt: str) -> List[tuple]:
    p = (prompt or "").lower()
    for key, pal in _PALETTES.items():
        if key in p:
            return pal
    random.seed(hash(p) % (2**32 - 1))
    return [tuple(random.randint(90, 200) for _ in range(3)) for _ in range(3)]


def _perlin_like_noise(h: int, w: int, octaves: int = 4) -> np.ndarray:
    acc = np.zeros((h, w), dtype=np.float32)
    for o in range(octaves):
        scale = 2 ** o
        small = np.random.rand(h // scale + 1, w // scale + 1).astype(np.float32)
        small = Image.fromarray((small * 255).astype(np.uint8)).resize((w, h), Image.BILINEAR)
        arr = np.array(small).astype(np.float32) / 255.0
        acc += arr / (o + 1)
    acc = acc / max(1e-6, acc.max())
    return acc


def _blend_palette(noise: np.ndarray, palette: List[tuple]) -> Image.Image:
    h, w = noise.shape
    img = np.zeros((h, w, 3), dtype=np.float32)
    thresholds = [0.33, 0.66]
    c0, c1, c2 = [np.array(c, dtype=np.float32) for c in palette]
    mask0 = noise < thresholds[0]
    mask1 = (noise >= thresholds[0]) & (noise < thresholds[1])
    mask2 = noise >= thresholds[1]
    img[mask0] = c0
    img[mask1] = c1
    img[mask2] = c2
    img = np.clip(img, 0, 255).astype(np.uint8)
    return Image.fromarray(img)


def generate_ai_background(
    prompt: str,
    width: int = 1280,
    height: int = 720,
    bokeh: float = 0.0,
    vignette: float = 0.15,
    contrast: float = 1.05,
) -> Tuple[Image.Image, str]:
    palette = _palette_from_prompt(prompt)
    noise = _perlin_like_noise(height, width, octaves=4)
    img = _blend_palette(noise, palette)

    if bokeh > 0:
        img = img.filter(ImageFilter.GaussianBlur(radius=max(0, min(50, bokeh))))

    if vignette > 0:
        y, x = np.ogrid[:height, :width]
        cx, cy = width / 2, height / 2
        r = np.sqrt((x - cx) ** 2 + (y - cy) ** 2)
        mask = 1 - np.clip(r / (max(width, height) / 1.2), 0, 1)
        mask = mask ** 2
        mask = (mask * (1 - vignette) + (1 - (1 - vignette))).astype(np.float32)
        base = np.array(img).astype(np.float32) / 255.0
        out = np.empty_like(base)
        for c in range(3):
            out[..., c] = base[..., c] * mask
        img = Image.fromarray(np.clip(out * 255, 0, 255).astype(np.uint8))

    if contrast != 1.0:
        img = ImageOps.autocontrast(img, cutoff=1)
        arr = np.array(img).astype(np.float32)
        mean = arr.mean(axis=(0, 1), keepdims=True)
        arr = (arr - mean) * float(contrast) + mean
        img = Image.fromarray(np.clip(arr, 0, 255).astype(np.uint8))

    path = _save_pil(img, stem="ai_bg", ext="png")
    return img, path


# --------------------------
# Gradio UI
# --------------------------

CSS = """
:root { --radius: 16px; }
.gradio-container { max-width: 1080px !important; margin: auto !important; }
#hero .prose { font-size: 15px; }
.card { border-radius: var(--radius); border: 1px solid rgba(0,0,0,.08); padding: 16px; background: linear-gradient(180deg, rgba(255,255,255,.9), rgba(248,250,252,.9)); box-shadow: 0 10px 30px rgba(0,0,0,.06); }
.footer-note { opacity: 0.7; font-size: 12px; }
.sm { font-size: 13px; opacity: 0.85; }
#statusbox { min-height: 120px; }
"""

def create_interface() -> gr.Blocks:
    with gr.Blocks(title="🎬 BackgroundFX Pro", css=CSS, analytics_enabled=False, theme=gr.themes.Soft()) as demo:
        # ---------- HERO ----------
        with gr.Row(elem_id="hero"):
            gr.Markdown(
                "## 🎬 BackgroundFX Pro\n"
                "Polished matting & background replacement for video. Runs on Hugging Face Spaces.\n"
                "Tip: **Load models** before processing for best results."
            )

        with gr.Tab("🏁 Quick Start"):
            with gr.Row():
                with gr.Column(scale=1):
                    # Inputs
                    video = gr.Video(label="Upload Video")
                    video_preview = gr.Image(label="Video First Frame (Preview)", interactive=False)

                    # Align keys with utils.cv_processing.PROFESSIONAL_BACKGROUNDS
                    bg_style = gr.Dropdown(
                        label="Background Style",
                        choices=[
                            "minimalist",
                            "office_modern",
                            "studio_blue",
                            "studio_green",
                            "warm_gradient",
                            "tech_dark",
                        ],
                        value="minimalist",
                    )
                    custom_bg = gr.File(label="Custom Background (Optional)", file_types=["image"])
                    custom_bg_preview = gr.Image(label="Custom Background Preview", interactive=False)

                    with gr.Accordion("Advanced", open=False):
                        use_two_stage = gr.Checkbox(label="Use Two-Stage Pipeline", value=False)
                        chroma_preset = gr.Dropdown(label="Chroma Preset", choices=["standard"], value="standard")
                        preview_mask = gr.Checkbox(label="Preview Mask (no audio remix)", value=False)
                        preview_greenscreen = gr.Checkbox(label="Preview Greenscreen (no audio remix)", value=False)

                    with gr.Row():
                        btn_load = gr.Button("πŸ”„ Load Models", variant="secondary")
                        btn_run = gr.Button("🎬 Process Video", variant="primary")
                        btn_cancel = gr.Button("⏹️ Cancel", variant="secondary")

                with gr.Column(scale=1):
                    out_video = gr.Video(label="Processed Output", interactive=False)
                    statusbox = gr.Textbox(label="Status", lines=8, elem_id="statusbox")
                    with gr.Row():
                        btn_refresh = gr.Button("πŸ” Refresh Status", variant="secondary")
                        btn_clear = gr.Button("🧹 Clear", variant="secondary")

        # ---------- AI BACKGROUND ----------
        with gr.Tab("🧠 AI Background (Lightweight)"):
            with gr.Row():
                with gr.Column(scale=1):
                    prompt = gr.Textbox(
                        label="Describe the vibe (e.g., 'modern office', 'soft sunset studio')",
                        value="modern office"
                    )
                    with gr.Row():
                        gen_width = gr.Slider(640, 1920, value=1280, step=10, label="Width")
                        gen_height = gr.Slider(360, 1080, value=720, step=10, label="Height")
                    with gr.Row():
                        bokeh = gr.Slider(0, 30, value=8, step=1, label="Bokeh Blur")
                        vignette = gr.Slider(0.0, 0.6, value=0.15, step=0.01, label="Vignette")
                        contrast = gr.Slider(0.8, 1.4, value=1.05, step=0.01, label="Contrast")

                    btn_gen_bg = gr.Button("✨ Generate Background", variant="primary")

                with gr.Column(scale=1):
                    gen_preview = gr.Image(label="Generated Background", interactive=False)
                    gen_path = gr.Textbox(label="Saved Path", interactive=False)
                    use_gen_as_custom = gr.Button("πŸ“Œ Use As Custom Background", variant="secondary")

        # ---------- STATUS ----------
        with gr.Tab("πŸ“ˆ Status & Settings"):
            with gr.Row():
                with gr.Column(scale=1, elem_classes=["card"]):
                    model_status = gr.JSON(label="Model Status")
                with gr.Column(scale=1, elem_classes=["card"]):
                    cache_status = gr.JSON(label="Cache / System Status")
            gr.Markdown("<div class='footer-note'>If models fail to load, fallbacks keep the UI responsive. Check logs for details.</div>")

        # ---------- CALLBACKS ----------
        # Load Models
        def _cb_load_models() -> str:
            return load_models_with_validation()

        # Process
        def _cb_process(
            vid: str,
            style: str,
            custom_file: dict | None,
            use_two: bool,
            chroma: str,
            prev_mask: bool,
            prev_green: bool,
        ):
            if PROCESS_CANCELLED.is_set():
                PROCESS_CANCELLED.clear()
            custom_path = None
            if isinstance(custom_file, dict) and custom_file.get("name"):
                # Gradio passes {"name": "/tmp/...", "size": int, ...}
                custom_path = custom_file["name"]
            return process_video_fixed(
                video_path=vid,
                background_choice=style,
                custom_background_path=custom_path,
                progress_callback=None,
                use_two_stage=use_two,
                chroma_preset=chroma,
                preview_mask=prev_mask,
                preview_greenscreen=prev_green,
            )

        # Cancel processing
        def _cb_cancel() -> str:
            try:
                PROCESS_CANCELLED.set()
                return "Cancellation requested."
            except Exception as e:
                return f"Cancel failed: {e}"

        # Refresh status
        def _cb_status() -> Tuple[Dict[str, Any], Dict[str, Any]]:
            try:
                return get_model_status(), get_cache_status()
            except Exception as e:
                return {"error": str(e)}, {"error": str(e)}

        # Clear
        def _cb_clear():
            return None, "", None, "", None

        # AI background generation
        def _cb_generate_bg(prompt_text: str, w: int, h: int, b: float, v: float, c: float):
            img, path = generate_ai_background(prompt_text, width=int(w), height=int(h), bokeh=b, vignette=v, contrast=c)
            return img, path

        # Use AI gen as custom
        def _cb_use_gen_bg(path_text: str):
            return (
                {"name": path_text, "size": os.path.getsize(path_text)}
                if path_text and os.path.exists(path_text) else None
            )

        # Video change -> extract first frame
        def _cb_video_changed(vid_path: str):
            if not vid_path:
                return None
            img = _first_frame(vid_path)
            return img

        # Custom background change -> preview image
        def _cb_custom_bg_preview(file_obj: dict | None):
            try:
                if isinstance(file_obj, dict) and file_obj.get("name") and os.path.exists(file_obj["name"]):
                    pil = _pil_from_path(file_obj["name"])
                    return pil
            except Exception:
                pass
            return None

        # Wire events
        btn_load.click(_cb_load_models, outputs=statusbox)
        btn_run.click(
            _cb_process,
            inputs=[video, bg_style, custom_bg, use_two_stage, chroma_preset, preview_mask, preview_greenscreen],
            outputs=[out_video, statusbox],
        )
        btn_cancel.click(_cb_cancel, outputs=statusbox)
        btn_refresh.click(_cb_status, outputs=[model_status, cache_status])
        btn_clear.click(_cb_clear, outputs=[out_video, statusbox, gen_preview, gen_path, custom_bg_preview])

        btn_gen_bg.click(
            _cb_generate_bg,
            inputs=[prompt, gen_width, gen_height, bokeh, vignette, contrast],
            outputs=[gen_preview, gen_path],
        )
        use_gen_as_custom.click(_cb_use_gen_bg, inputs=[gen_path], outputs=[custom_bg])

        # Live previews
        video.change(_cb_video_changed, inputs=[video], outputs=[video_preview])
        custom_bg.change(_cb_custom_bg_preview, inputs=[custom_bg], outputs=[custom_bg_preview])

    return demo