Update ui.py
Browse files
ui.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
-
Modern UI for Video Background Replacer
|
| 4 |
-
- Clean, responsive layout
|
| 5 |
- File uploaders, previews, and processing controls
|
| 6 |
-
-
|
| 7 |
-
-
|
| 8 |
"""
|
| 9 |
import streamlit as st
|
| 10 |
import os
|
|
@@ -16,7 +16,6 @@
|
|
| 16 |
|
| 17 |
logger = logging.getLogger("Advanced Video Background Replacer")
|
| 18 |
|
| 19 |
-
# --- UI Helpers ---
|
| 20 |
def tail_file(path: str, lines: int = 400) -> str:
|
| 21 |
if not os.path.exists(path):
|
| 22 |
return "(log file not found)"
|
|
@@ -36,6 +35,75 @@ def read_file_bytes(path: str) -> bytes:
|
|
| 36 |
except Exception:
|
| 37 |
return b""
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
def render_ui(process_video_func):
|
| 40 |
try:
|
| 41 |
# --- Sidebar: System Status & Logs ---
|
|
@@ -91,59 +159,10 @@ def render_ui(process_video_func):
|
|
| 91 |
video_preview_placeholder.error(f"Cannot display video: {e}")
|
| 92 |
else:
|
| 93 |
video_preview_placeholder.empty()
|
|
|
|
| 94 |
# --- Column 2: Background & Processing ---
|
| 95 |
with col2:
|
| 96 |
-
|
| 97 |
-
bg_type = st.radio(
|
| 98 |
-
"Select Background Type:",
|
| 99 |
-
["Image", "Color"],
|
| 100 |
-
horizontal=True,
|
| 101 |
-
key="bg_type_radio"
|
| 102 |
-
)
|
| 103 |
-
background = None
|
| 104 |
-
# --- Background Image Upload ---
|
| 105 |
-
if bg_type == "Image":
|
| 106 |
-
bg_image = st.file_uploader(
|
| 107 |
-
"Upload Background Image",
|
| 108 |
-
type=["jpg", "png", "jpeg"],
|
| 109 |
-
key="bg_image_uploader"
|
| 110 |
-
)
|
| 111 |
-
bg_preview_placeholder = st.empty()
|
| 112 |
-
background_pil = None
|
| 113 |
-
if bg_image is not None:
|
| 114 |
-
try:
|
| 115 |
-
bg_image.seek(0)
|
| 116 |
-
background_pil = Image.open(bg_image).convert("RGB")
|
| 117 |
-
with bg_preview_placeholder.container():
|
| 118 |
-
st.image(background_pil, caption="Selected Background", use_column_width=True)
|
| 119 |
-
except Exception as e:
|
| 120 |
-
logger.error(f"[UI] Background image error: {e}", exc_info=True)
|
| 121 |
-
bg_preview_placeholder.error(f"Cannot display image: {e}")
|
| 122 |
-
background_pil = None
|
| 123 |
-
else:
|
| 124 |
-
bg_preview_placeholder.empty()
|
| 125 |
-
background = background_pil
|
| 126 |
-
else:
|
| 127 |
-
selected_color = st.color_picker(
|
| 128 |
-
"Choose Background Color",
|
| 129 |
-
st.session_state.get('bg_color', "#00FF00"),
|
| 130 |
-
key="color_picker"
|
| 131 |
-
)
|
| 132 |
-
if selected_color != st.session_state.get('cached_color'):
|
| 133 |
-
st.session_state.bg_color = selected_color
|
| 134 |
-
st.session_state.cached_color = selected_color
|
| 135 |
-
color_rgb = tuple(int(selected_color.lstrip('#')[i:i+2], 16) for i in (0, 2, 4))
|
| 136 |
-
color_display = np.zeros((100, 100, 3), dtype=np.uint8)
|
| 137 |
-
color_display[:, :] = color_rgb
|
| 138 |
-
st.session_state.color_display_cache = color_display
|
| 139 |
-
color_preview_placeholder = st.empty()
|
| 140 |
-
if st.session_state.get('color_display_cache') is not None:
|
| 141 |
-
with color_preview_placeholder.container():
|
| 142 |
-
st.image(st.session_state.color_display_cache, caption="Selected Color", width=200)
|
| 143 |
-
else:
|
| 144 |
-
color_preview_placeholder.empty()
|
| 145 |
-
background = selected_color # "#RRGGBB"
|
| 146 |
-
# --- Process Button ---
|
| 147 |
st.header("3. Process Video")
|
| 148 |
can_process = (
|
| 149 |
uploaded_video is not None
|
|
@@ -157,7 +176,7 @@ def render_ui(process_video_func):
|
|
| 157 |
st.error("Please upload a background image")
|
| 158 |
return
|
| 159 |
with st.spinner("Processing video..."):
|
| 160 |
-
#
|
| 161 |
success = process_video_func(uploaded_video, background, bg_type.lower())
|
| 162 |
if success:
|
| 163 |
st.success("Video processing complete!")
|
|
@@ -166,6 +185,7 @@ def render_ui(process_video_func):
|
|
| 166 |
except Exception as e:
|
| 167 |
logger.error(f"[UI] Process video error: {e}", exc_info=True)
|
| 168 |
st.error(f"Processing error: {str(e)}. Check logs for details.")
|
|
|
|
| 169 |
# --- Results Display ---
|
| 170 |
if st.session_state.get('processed_video_bytes') is not None:
|
| 171 |
st.markdown("---")
|
|
@@ -182,6 +202,7 @@ def render_ui(process_video_func):
|
|
| 182 |
except Exception as e:
|
| 183 |
logger.error(f"[UI] Display error: {e}", exc_info=True)
|
| 184 |
st.error(f"Display error: {e}")
|
|
|
|
| 185 |
# --- Error Display ---
|
| 186 |
if st.session_state.get('last_error'):
|
| 187 |
with st.expander("⚠️ Last Error", expanded=True):
|
|
@@ -191,4 +212,4 @@ def render_ui(process_video_func):
|
|
| 191 |
st.rerun()
|
| 192 |
except Exception as e:
|
| 193 |
logger.error(f"[UI] Render UI error: {e}", exc_info=True)
|
| 194 |
-
st.error(f"UI rendering error: {str(e)}. Check logs for details.")
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
+
Modern UI for Video Background Replacer (PRO)
|
| 4 |
+
- Clean, responsive Streamlit layout
|
| 5 |
- File uploaders, previews, and processing controls
|
| 6 |
+
- Supports: Color, Image upload, Stock image, AI Prompt background
|
| 7 |
+
- Log viewer, session state, robust error handling
|
| 8 |
"""
|
| 9 |
import streamlit as st
|
| 10 |
import os
|
|
|
|
| 16 |
|
| 17 |
logger = logging.getLogger("Advanced Video Background Replacer")
|
| 18 |
|
|
|
|
| 19 |
def tail_file(path: str, lines: int = 400) -> str:
|
| 20 |
if not os.path.exists(path):
|
| 21 |
return "(log file not found)"
|
|
|
|
| 35 |
except Exception:
|
| 36 |
return b""
|
| 37 |
|
| 38 |
+
def _render_background_settings():
|
| 39 |
+
# Available stock images (replace with your real logic or API)
|
| 40 |
+
stock_images = {
|
| 41 |
+
"Sunset Beach": "stock_images/sunset_beach.jpg",
|
| 42 |
+
"Urban Office": "stock_images/urban_office.jpg",
|
| 43 |
+
"Studio Lighting": "stock_images/studio_light.jpg",
|
| 44 |
+
}
|
| 45 |
+
st.header("2. Background Settings")
|
| 46 |
+
bg_type = st.radio(
|
| 47 |
+
"Select Background Type:",
|
| 48 |
+
["Image", "Color", "Stock", "AI Prompt"],
|
| 49 |
+
horizontal=True,
|
| 50 |
+
key="bg_type_radio"
|
| 51 |
+
)
|
| 52 |
+
background = None
|
| 53 |
+
|
| 54 |
+
if bg_type == "Image":
|
| 55 |
+
bg_image = st.file_uploader(
|
| 56 |
+
"Upload Background Image",
|
| 57 |
+
type=["jpg", "png", "jpeg"],
|
| 58 |
+
key="bg_image_uploader"
|
| 59 |
+
)
|
| 60 |
+
if bg_image is not None:
|
| 61 |
+
bg_image.seek(0)
|
| 62 |
+
background = Image.open(bg_image).convert("RGB")
|
| 63 |
+
st.image(background, caption="Selected Background", use_column_width=True)
|
| 64 |
+
|
| 65 |
+
elif bg_type == "Color":
|
| 66 |
+
selected_color = st.color_picker(
|
| 67 |
+
"Choose Background Color",
|
| 68 |
+
st.session_state.get('bg_color', "#00FF00"),
|
| 69 |
+
key="color_picker"
|
| 70 |
+
)
|
| 71 |
+
background = selected_color
|
| 72 |
+
color_preview = np.full(
|
| 73 |
+
(100, 100, 3),
|
| 74 |
+
tuple(int(selected_color.lstrip('#')[i:i+2], 16) for i in (0, 2, 4)),
|
| 75 |
+
dtype=np.uint8
|
| 76 |
+
)
|
| 77 |
+
st.image(color_preview, caption="Selected Color", width=200)
|
| 78 |
+
|
| 79 |
+
elif bg_type == "Stock":
|
| 80 |
+
stock_choice = st.selectbox(
|
| 81 |
+
"Choose a professional stock background:",
|
| 82 |
+
list(stock_images.keys()),
|
| 83 |
+
key="stock_image_select"
|
| 84 |
+
)
|
| 85 |
+
stock_img_path = stock_images[stock_choice]
|
| 86 |
+
background = Image.open(stock_img_path).convert("RGB")
|
| 87 |
+
st.image(background, caption=stock_choice, use_column_width=True)
|
| 88 |
+
|
| 89 |
+
elif bg_type == "AI Prompt":
|
| 90 |
+
prompt = st.text_input("Describe the background to generate (AI):", key="ai_bg_prompt")
|
| 91 |
+
ai_ready = False
|
| 92 |
+
if st.button("Generate Background", key="gen_bg_btn") and prompt:
|
| 93 |
+
# TODO: Plug in real AI image generator
|
| 94 |
+
# For now, stub with a purple placeholder
|
| 95 |
+
background = Image.new("RGB", (512, 320), (64, 32, 96))
|
| 96 |
+
st.session_state.generated_bg = background
|
| 97 |
+
st.success("AI-generated background (stub). Replace with your generator!")
|
| 98 |
+
ai_ready = True
|
| 99 |
+
elif "generated_bg" in st.session_state:
|
| 100 |
+
background = st.session_state.generated_bg
|
| 101 |
+
ai_ready = True
|
| 102 |
+
if ai_ready and background is not None:
|
| 103 |
+
st.image(background, caption="Generated Background", use_column_width=True)
|
| 104 |
+
|
| 105 |
+
return background, bg_type
|
| 106 |
+
|
| 107 |
def render_ui(process_video_func):
|
| 108 |
try:
|
| 109 |
# --- Sidebar: System Status & Logs ---
|
|
|
|
| 159 |
video_preview_placeholder.error(f"Cannot display video: {e}")
|
| 160 |
else:
|
| 161 |
video_preview_placeholder.empty()
|
| 162 |
+
|
| 163 |
# --- Column 2: Background & Processing ---
|
| 164 |
with col2:
|
| 165 |
+
background, bg_type = _render_background_settings()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
st.header("3. Process Video")
|
| 167 |
can_process = (
|
| 168 |
uploaded_video is not None
|
|
|
|
| 176 |
st.error("Please upload a background image")
|
| 177 |
return
|
| 178 |
with st.spinner("Processing video..."):
|
| 179 |
+
# bg_type always lowercase for pipeline
|
| 180 |
success = process_video_func(uploaded_video, background, bg_type.lower())
|
| 181 |
if success:
|
| 182 |
st.success("Video processing complete!")
|
|
|
|
| 185 |
except Exception as e:
|
| 186 |
logger.error(f"[UI] Process video error: {e}", exc_info=True)
|
| 187 |
st.error(f"Processing error: {str(e)}. Check logs for details.")
|
| 188 |
+
|
| 189 |
# --- Results Display ---
|
| 190 |
if st.session_state.get('processed_video_bytes') is not None:
|
| 191 |
st.markdown("---")
|
|
|
|
| 202 |
except Exception as e:
|
| 203 |
logger.error(f"[UI] Display error: {e}", exc_info=True)
|
| 204 |
st.error(f"Display error: {e}")
|
| 205 |
+
|
| 206 |
# --- Error Display ---
|
| 207 |
if st.session_state.get('last_error'):
|
| 208 |
with st.expander("⚠️ Last Error", expanded=True):
|
|
|
|
| 212 |
st.rerun()
|
| 213 |
except Exception as e:
|
| 214 |
logger.error(f"[UI] Render UI error: {e}", exc_info=True)
|
| 215 |
+
st.error(f"UI rendering error: {str(e)}. Check logs for details.")
|