Update app.py
Browse files
app.py
CHANGED
|
@@ -1,55 +1,98 @@
|
|
| 1 |
# app.py
|
| 2 |
-
import
|
| 3 |
-
from pathlib import Path
|
| 4 |
-
import tempfile
|
| 5 |
import os
|
|
|
|
|
|
|
|
|
|
| 6 |
from PIL import Image
|
| 7 |
-
from pipeline.two_stage_pipeline import process_two_stage
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
)
|
| 17 |
|
| 18 |
def progress_callback(msg, prog):
|
| 19 |
-
"""Streamlit-friendly progress reporting."""
|
| 20 |
-
if not
|
| 21 |
st.session_state.progress_bar = st.progress(0)
|
| 22 |
st.session_state.progress_text = st.empty()
|
| 23 |
st.session_state.progress_bar.progress(min(max(prog,0.0),1.0))
|
| 24 |
st.session_state.progress_text.text(msg)
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
video_file = st.file_uploader(
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
if video_file
|
|
|
|
| 31 |
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as vf:
|
| 32 |
vf.write(video_file.read())
|
| 33 |
video_path = vf.name
|
| 34 |
st.video(video_path)
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
|
|
|
| 49 |
if st.button("π Run Background Replacement"):
|
| 50 |
st.session_state.progress_bar = st.progress(0)
|
| 51 |
st.session_state.progress_text = st.empty()
|
| 52 |
-
with st.spinner("Processing video...
|
| 53 |
try:
|
| 54 |
out_path = process_two_stage(
|
| 55 |
video_path,
|
|
@@ -68,10 +111,15 @@ def progress_callback(msg, prog):
|
|
| 68 |
except Exception as e:
|
| 69 |
st.error(f"β Processing failed: {e}")
|
| 70 |
|
| 71 |
-
# Clean up temp files (
|
| 72 |
try:
|
| 73 |
-
os.
|
|
|
|
| 74 |
except Exception: pass
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# app.py
|
| 2 |
+
import sys
|
|
|
|
|
|
|
| 3 |
import os
|
| 4 |
+
import tempfile
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
import streamlit as st
|
| 7 |
from PIL import Image
|
|
|
|
| 8 |
|
| 9 |
+
# Force app root and pipeline to be importable (for HF docker)
|
| 10 |
+
sys.path.append(str(Path(__file__).parent.resolve()))
|
| 11 |
+
sys.path.append(str(Path(__file__).parent.resolve() / "pipeline"))
|
| 12 |
|
| 13 |
+
# === CUDA DIAGNOSTICS (LOGS) ===
|
| 14 |
+
try:
|
| 15 |
+
import torch
|
| 16 |
+
print("=== CUDA Info ===")
|
| 17 |
+
print("CUDA available:", torch.cuda.is_available())
|
| 18 |
+
print("CUDA device count:", torch.cuda.device_count())
|
| 19 |
+
print("torch version:", torch.__version__)
|
| 20 |
+
print("=================")
|
| 21 |
+
except Exception as e:
|
| 22 |
+
print(f"[WARN] Could not print CUDA info: {e}")
|
| 23 |
|
| 24 |
+
# === Import the two-stage pipeline ===
|
| 25 |
+
try:
|
| 26 |
+
from pipeline.two_stage_pipeline import process_two_stage
|
| 27 |
+
except ImportError as e:
|
| 28 |
+
st.error(f"Pipeline not found or misconfigured: {e}")
|
| 29 |
+
st.stop()
|
| 30 |
+
|
| 31 |
+
st.set_page_config(
|
| 32 |
+
page_title="BackgroundFX Pro",
|
| 33 |
+
layout="wide",
|
| 34 |
+
page_icon="π¬"
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
st.title("π¬ BackgroundFX Pro β AI Video Background Replacer")
|
| 38 |
+
st.write(
|
| 39 |
+
"Upload a video and a background image (optional). The system will remove the original background "
|
| 40 |
+
"using **SAM2** & **MatAnyone**, and composite the subject over your new background image."
|
| 41 |
)
|
| 42 |
|
| 43 |
def progress_callback(msg, prog):
|
| 44 |
+
"""Streamlit-friendly progress reporting (persistent)."""
|
| 45 |
+
if "progress_bar" not in st.session_state:
|
| 46 |
st.session_state.progress_bar = st.progress(0)
|
| 47 |
st.session_state.progress_text = st.empty()
|
| 48 |
st.session_state.progress_bar.progress(min(max(prog,0.0),1.0))
|
| 49 |
st.session_state.progress_text.text(msg)
|
| 50 |
|
| 51 |
+
# --- Uploaders ---
|
| 52 |
+
video_file = st.file_uploader(
|
| 53 |
+
"1οΈβ£ Upload video file (mp4, mov, avi, mkv)",
|
| 54 |
+
type=["mp4", "mov", "avi", "mkv"]
|
| 55 |
+
)
|
| 56 |
+
bg_file = st.file_uploader(
|
| 57 |
+
"2οΈβ£ Upload new background (jpg/png, optional)",
|
| 58 |
+
type=["jpg", "jpeg", "png"]
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
video_path, bg_path = None, None
|
| 62 |
|
| 63 |
+
if video_file:
|
| 64 |
+
# Save video to temp file
|
| 65 |
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as vf:
|
| 66 |
vf.write(video_file.read())
|
| 67 |
video_path = vf.name
|
| 68 |
st.video(video_path)
|
| 69 |
|
| 70 |
+
if bg_file:
|
| 71 |
+
# Save background to temp file
|
| 72 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as bf:
|
| 73 |
+
bf.write(bg_file.read())
|
| 74 |
+
bg_path = bf.name
|
| 75 |
+
background_img = Image.open(bg_path).convert("RGB")
|
| 76 |
+
st.image(background_img, caption="Background Image")
|
| 77 |
+
elif video_file:
|
| 78 |
+
# Fallback: green background sized to video or 1280x720
|
| 79 |
+
try:
|
| 80 |
+
import cv2
|
| 81 |
+
cap = cv2.VideoCapture(video_path)
|
| 82 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) or 1280
|
| 83 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) or 720
|
| 84 |
+
cap.release()
|
| 85 |
+
except Exception:
|
| 86 |
+
width, height = 1280, 720
|
| 87 |
+
background_img = Image.new("RGB", (width, height), (0,255,0))
|
| 88 |
+
st.info("No background image uploaded. Using default green background.")
|
| 89 |
|
| 90 |
+
# --- Processing ---
|
| 91 |
+
if video_path and background_img:
|
| 92 |
if st.button("π Run Background Replacement"):
|
| 93 |
st.session_state.progress_bar = st.progress(0)
|
| 94 |
st.session_state.progress_text = st.empty()
|
| 95 |
+
with st.spinner("Processing video... (May take several minutes for long videos)"):
|
| 96 |
try:
|
| 97 |
out_path = process_two_stage(
|
| 98 |
video_path,
|
|
|
|
| 111 |
except Exception as e:
|
| 112 |
st.error(f"β Processing failed: {e}")
|
| 113 |
|
| 114 |
+
# Clean up temp files (best effort)
|
| 115 |
try:
|
| 116 |
+
if video_path and os.path.exists(video_path):
|
| 117 |
+
os.unlink(video_path)
|
| 118 |
except Exception: pass
|
| 119 |
+
try:
|
| 120 |
+
if bg_path and os.path.exists(bg_path):
|
| 121 |
+
os.unlink(bg_path)
|
| 122 |
+
except Exception: pass
|
| 123 |
+
|
| 124 |
+
# HF Spaces tip
|
| 125 |
+
st.caption("Built for Hugging Face T4 Spaces β’ CUDA & GPU acceleration required β’ v2025.10")
|