Update streamlit_app.py
Browse files- streamlit_app.py +101 -86
streamlit_app.py
CHANGED
|
@@ -88,97 +88,25 @@ def initialize_session_state():
|
|
| 88 |
defaults = {
|
| 89 |
'uploaded_video': None,
|
| 90 |
'video_bytes_cache': None,
|
|
|
|
| 91 |
'bg_image': None,
|
| 92 |
'bg_image_cache': None,
|
| 93 |
'bg_image_name': None,
|
|
|
|
| 94 |
'bg_color': "#00FF00",
|
| 95 |
'cached_color': None,
|
| 96 |
'color_display_cache': None,
|
| 97 |
'processed_video_path': None,
|
| 98 |
'processing': False,
|
| 99 |
'progress': 0,
|
| 100 |
-
'progress_text': "Ready"
|
|
|
|
|
|
|
| 101 |
}
|
| 102 |
for key, value in defaults.items():
|
| 103 |
if key not in st.session_state:
|
| 104 |
st.session_state[key] = value
|
| 105 |
|
| 106 |
-
# --- Video Upload Handling ---
|
| 107 |
-
def handle_video_upload():
|
| 108 |
-
"""Handle video file upload"""
|
| 109 |
-
uploaded = st.file_uploader(
|
| 110 |
-
"📹 Upload Video",
|
| 111 |
-
type=["mp4", "mov", "avi"],
|
| 112 |
-
key="video_uploader"
|
| 113 |
-
)
|
| 114 |
-
# Only update if actually changed
|
| 115 |
-
if uploaded is not None and uploaded != st.session_state.uploaded_video:
|
| 116 |
-
st.session_state.uploaded_video = uploaded
|
| 117 |
-
st.session_state.video_bytes_cache = None # Clear cache when new video uploaded
|
| 118 |
-
|
| 119 |
-
# --- Video Preview ---
|
| 120 |
-
def show_video_preview():
|
| 121 |
-
"""Show video preview in the UI"""
|
| 122 |
-
st.markdown("### Video Preview")
|
| 123 |
-
if st.session_state.uploaded_video is not None:
|
| 124 |
-
# Cache video bytes in session state to prevent reloading
|
| 125 |
-
if st.session_state.video_bytes_cache is None:
|
| 126 |
-
st.session_state.video_bytes_cache = st.session_state.uploaded_video.getvalue()
|
| 127 |
-
st.session_state.uploaded_video.seek(0)
|
| 128 |
-
st.video(st.session_state.video_bytes_cache)
|
| 129 |
-
|
| 130 |
-
# --- Background Selection ---
|
| 131 |
-
def handle_background_selection():
|
| 132 |
-
"""Handle background selection UI"""
|
| 133 |
-
st.markdown("### Background Options")
|
| 134 |
-
bg_type = st.radio(
|
| 135 |
-
"Select Background Type:",
|
| 136 |
-
["Image", "Color", "Blur"],
|
| 137 |
-
horizontal=True,
|
| 138 |
-
index=0,
|
| 139 |
-
key="bg_type_radio"
|
| 140 |
-
)
|
| 141 |
-
|
| 142 |
-
if bg_type == "Image":
|
| 143 |
-
bg_image = st.file_uploader(
|
| 144 |
-
"🖼️ Upload Background Image",
|
| 145 |
-
type=["jpg", "png", "jpeg"],
|
| 146 |
-
key="bg_image_uploader"
|
| 147 |
-
)
|
| 148 |
-
# Only process if new image uploaded
|
| 149 |
-
if bg_image is not None:
|
| 150 |
-
if (st.session_state.bg_image_cache is None or
|
| 151 |
-
st.session_state.get('bg_image_name') != bg_image.name):
|
| 152 |
-
st.session_state.bg_image = Image.open(bg_image)
|
| 153 |
-
st.session_state.bg_image_cache = st.session_state.bg_image
|
| 154 |
-
st.session_state.bg_image_name = bg_image.name
|
| 155 |
-
|
| 156 |
-
# Display cached image
|
| 157 |
-
st.image(
|
| 158 |
-
st.session_state.bg_image_cache,
|
| 159 |
-
caption="Selected Background",
|
| 160 |
-
use_container_width=True
|
| 161 |
-
)
|
| 162 |
-
|
| 163 |
-
elif bg_type == "Color":
|
| 164 |
-
st.session_state.bg_color = st.color_picker(
|
| 165 |
-
"🎨 Choose Background Color",
|
| 166 |
-
st.session_state.bg_color,
|
| 167 |
-
key="color_picker"
|
| 168 |
-
)
|
| 169 |
-
# Cache color display to prevent regeneration
|
| 170 |
-
if (st.session_state.color_display_cache is None or
|
| 171 |
-
st.session_state.get('cached_color') != st.session_state.bg_color):
|
| 172 |
-
color_rgb = tuple(int(st.session_state.bg_color.lstrip('#')[i:i+2], 16) for i in (0, 2, 4))
|
| 173 |
-
color_display = np.zeros((100, 100, 3), dtype=np.uint8)
|
| 174 |
-
color_display[:, :] = color_rgb[::-1] # RGB to BGR for OpenCV
|
| 175 |
-
st.session_state.color_display_cache = color_display
|
| 176 |
-
st.session_state.cached_color = st.session_state.bg_color
|
| 177 |
-
|
| 178 |
-
st.image(st.session_state.color_display_cache, caption="Selected Color", width=200)
|
| 179 |
-
|
| 180 |
-
return bg_type.lower()
|
| 181 |
-
|
| 182 |
# --- Video Processing ---
|
| 183 |
def process_video(input_file, background, bg_type="image"):
|
| 184 |
"""
|
|
@@ -286,15 +214,106 @@ def main():
|
|
| 286 |
|
| 287 |
with col1:
|
| 288 |
st.header("1. Upload Video")
|
| 289 |
-
|
| 290 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 291 |
|
| 292 |
with col2:
|
| 293 |
st.header("2. Background Settings")
|
| 294 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 295 |
|
| 296 |
st.header("3. Process & Download")
|
| 297 |
-
|
| 298 |
with st.form("process_form"):
|
| 299 |
submitted = st.form_submit_button(
|
| 300 |
"🚀 Process Video",
|
|
@@ -306,18 +325,16 @@ def main():
|
|
| 306 |
st.session_state.processing = True
|
| 307 |
with st.spinner("Processing video (this may take a few minutes)..."):
|
| 308 |
try:
|
| 309 |
-
# Prepare background based on type
|
| 310 |
background = None
|
| 311 |
if bg_type == "image" and st.session_state.bg_image_cache is not None:
|
| 312 |
background = st.session_state.bg_image_cache
|
| 313 |
elif bg_type == "color" and 'bg_color' in st.session_state:
|
| 314 |
background = st.session_state.bg_color
|
| 315 |
|
| 316 |
-
# Process the video
|
| 317 |
output_path = process_video(
|
| 318 |
st.session_state.uploaded_video,
|
| 319 |
background,
|
| 320 |
-
bg_type=bg_type
|
| 321 |
)
|
| 322 |
|
| 323 |
if output_path and os.path.exists(output_path):
|
|
@@ -331,7 +348,6 @@ def main():
|
|
| 331 |
finally:
|
| 332 |
st.session_state.processing = False
|
| 333 |
|
| 334 |
-
# Show processed video if available (outside form for stability)
|
| 335 |
if st.session_state.processed_video_path:
|
| 336 |
st.markdown("### Processed Video")
|
| 337 |
try:
|
|
@@ -350,6 +366,5 @@ def main():
|
|
| 350 |
st.error(f"Error displaying video: {str(e)}")
|
| 351 |
logger.error(f"Error displaying video: {str(e)}", exc_info=True)
|
| 352 |
|
| 353 |
-
# --- Entry Point ---
|
| 354 |
if __name__ == "__main__":
|
| 355 |
main()
|
|
|
|
| 88 |
defaults = {
|
| 89 |
'uploaded_video': None,
|
| 90 |
'video_bytes_cache': None,
|
| 91 |
+
'video_preview_placeholder': None,
|
| 92 |
'bg_image': None,
|
| 93 |
'bg_image_cache': None,
|
| 94 |
'bg_image_name': None,
|
| 95 |
+
'bg_preview_placeholder': None,
|
| 96 |
'bg_color': "#00FF00",
|
| 97 |
'cached_color': None,
|
| 98 |
'color_display_cache': None,
|
| 99 |
'processed_video_path': None,
|
| 100 |
'processing': False,
|
| 101 |
'progress': 0,
|
| 102 |
+
'progress_text': "Ready",
|
| 103 |
+
'last_video_id': None,
|
| 104 |
+
'last_bg_image_id': None
|
| 105 |
}
|
| 106 |
for key, value in defaults.items():
|
| 107 |
if key not in st.session_state:
|
| 108 |
st.session_state[key] = value
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
# --- Video Processing ---
|
| 111 |
def process_video(input_file, background, bg_type="image"):
|
| 112 |
"""
|
|
|
|
| 214 |
|
| 215 |
with col1:
|
| 216 |
st.header("1. Upload Video")
|
| 217 |
+
|
| 218 |
+
uploaded = st.file_uploader(
|
| 219 |
+
"📹 Upload Video",
|
| 220 |
+
type=["mp4", "mov", "avi"],
|
| 221 |
+
key="video_uploader"
|
| 222 |
+
)
|
| 223 |
+
|
| 224 |
+
# Check if video actually changed using id()
|
| 225 |
+
current_video_id = id(uploaded)
|
| 226 |
+
if current_video_id != st.session_state.last_video_id:
|
| 227 |
+
st.session_state.uploaded_video = uploaded
|
| 228 |
+
st.session_state.last_video_id = current_video_id
|
| 229 |
+
st.session_state.video_bytes_cache = None
|
| 230 |
+
|
| 231 |
+
# Video preview section
|
| 232 |
+
st.markdown("### Video Preview")
|
| 233 |
+
if st.session_state.video_preview_placeholder is None:
|
| 234 |
+
st.session_state.video_preview_placeholder = st.empty()
|
| 235 |
+
|
| 236 |
+
if st.session_state.uploaded_video is not None:
|
| 237 |
+
if st.session_state.video_bytes_cache is None:
|
| 238 |
+
st.session_state.video_bytes_cache = st.session_state.uploaded_video.getvalue()
|
| 239 |
+
st.session_state.uploaded_video.seek(0)
|
| 240 |
+
|
| 241 |
+
with st.session_state.video_preview_placeholder.container():
|
| 242 |
+
st.video(st.session_state.video_bytes_cache)
|
| 243 |
+
else:
|
| 244 |
+
st.session_state.video_preview_placeholder.empty()
|
| 245 |
|
| 246 |
with col2:
|
| 247 |
st.header("2. Background Settings")
|
| 248 |
+
|
| 249 |
+
bg_type = st.radio(
|
| 250 |
+
"Select Background Type:",
|
| 251 |
+
["Image", "Color", "Blur"],
|
| 252 |
+
horizontal=True,
|
| 253 |
+
index=0,
|
| 254 |
+
key="bg_type_radio"
|
| 255 |
+
)
|
| 256 |
+
|
| 257 |
+
if bg_type == "Image":
|
| 258 |
+
bg_image = st.file_uploader(
|
| 259 |
+
"🖼️ Upload Background Image",
|
| 260 |
+
type=["jpg", "png", "jpeg"],
|
| 261 |
+
key="bg_image_uploader"
|
| 262 |
+
)
|
| 263 |
+
|
| 264 |
+
# Check if image actually changed using id()
|
| 265 |
+
current_bg_id = id(bg_image)
|
| 266 |
+
if current_bg_id != st.session_state.last_bg_image_id:
|
| 267 |
+
st.session_state.last_bg_image_id = current_bg_id
|
| 268 |
+
if bg_image is not None:
|
| 269 |
+
st.session_state.bg_image_cache = Image.open(bg_image)
|
| 270 |
+
st.session_state.bg_image_name = bg_image.name
|
| 271 |
+
else:
|
| 272 |
+
st.session_state.bg_image_cache = None
|
| 273 |
+
|
| 274 |
+
# Background preview section
|
| 275 |
+
if st.session_state.bg_preview_placeholder is None:
|
| 276 |
+
st.session_state.bg_preview_placeholder = st.empty()
|
| 277 |
+
|
| 278 |
+
if st.session_state.bg_image_cache is not None:
|
| 279 |
+
with st.session_state.bg_preview_placeholder.container():
|
| 280 |
+
st.image(
|
| 281 |
+
st.session_state.bg_image_cache,
|
| 282 |
+
caption="Selected Background",
|
| 283 |
+
use_container_width=True
|
| 284 |
+
)
|
| 285 |
+
else:
|
| 286 |
+
st.session_state.bg_preview_placeholder.empty()
|
| 287 |
+
|
| 288 |
+
elif bg_type == "Color":
|
| 289 |
+
selected_color = st.color_picker(
|
| 290 |
+
"🎨 Choose Background Color",
|
| 291 |
+
st.session_state.bg_color,
|
| 292 |
+
key="color_picker"
|
| 293 |
+
)
|
| 294 |
+
|
| 295 |
+
# Update only if color actually changed
|
| 296 |
+
if selected_color != st.session_state.cached_color:
|
| 297 |
+
st.session_state.bg_color = selected_color
|
| 298 |
+
st.session_state.cached_color = selected_color
|
| 299 |
+
|
| 300 |
+
color_rgb = tuple(int(selected_color.lstrip('#')[i:i+2], 16) for i in (0, 2, 4))
|
| 301 |
+
color_display = np.zeros((100, 100, 3), dtype=np.uint8)
|
| 302 |
+
color_display[:, :] = color_rgb[::-1]
|
| 303 |
+
st.session_state.color_display_cache = color_display
|
| 304 |
+
|
| 305 |
+
# Color preview section
|
| 306 |
+
if st.session_state.bg_preview_placeholder is None:
|
| 307 |
+
st.session_state.bg_preview_placeholder = st.empty()
|
| 308 |
+
|
| 309 |
+
if st.session_state.color_display_cache is not None:
|
| 310 |
+
with st.session_state.bg_preview_placeholder.container():
|
| 311 |
+
st.image(st.session_state.color_display_cache, caption="Selected Color", width=200)
|
| 312 |
+
else:
|
| 313 |
+
st.session_state.bg_preview_placeholder.empty()
|
| 314 |
|
| 315 |
st.header("3. Process & Download")
|
| 316 |
+
|
| 317 |
with st.form("process_form"):
|
| 318 |
submitted = st.form_submit_button(
|
| 319 |
"🚀 Process Video",
|
|
|
|
| 325 |
st.session_state.processing = True
|
| 326 |
with st.spinner("Processing video (this may take a few minutes)..."):
|
| 327 |
try:
|
|
|
|
| 328 |
background = None
|
| 329 |
if bg_type == "image" and st.session_state.bg_image_cache is not None:
|
| 330 |
background = st.session_state.bg_image_cache
|
| 331 |
elif bg_type == "color" and 'bg_color' in st.session_state:
|
| 332 |
background = st.session_state.bg_color
|
| 333 |
|
|
|
|
| 334 |
output_path = process_video(
|
| 335 |
st.session_state.uploaded_video,
|
| 336 |
background,
|
| 337 |
+
bg_type=bg_type.lower()
|
| 338 |
)
|
| 339 |
|
| 340 |
if output_path and os.path.exists(output_path):
|
|
|
|
| 348 |
finally:
|
| 349 |
st.session_state.processing = False
|
| 350 |
|
|
|
|
| 351 |
if st.session_state.processed_video_path:
|
| 352 |
st.markdown("### Processed Video")
|
| 353 |
try:
|
|
|
|
| 366 |
st.error(f"Error displaying video: {str(e)}")
|
| 367 |
logger.error(f"Error displaying video: {str(e)}", exc_info=True)
|
| 368 |
|
|
|
|
| 369 |
if __name__ == "__main__":
|
| 370 |
main()
|