Update streamlit_app.py
Browse files- streamlit_app.py +44 -91
streamlit_app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# streamlit_ui.py -
|
| 2 |
|
| 3 |
import streamlit as st
|
| 4 |
import os
|
|
@@ -49,7 +49,6 @@ def check_gpu():
|
|
| 49 |
if cuda_available:
|
| 50 |
logger.info(f"CUDA Version: {torch.version.cuda}")
|
| 51 |
logger.info(f"Device Count: {torch.cuda.device_count()}")
|
| 52 |
-
logger.info(f"Current Device: {torch.cuda.current_device()}")
|
| 53 |
logger.info(f"Device Name: {torch.cuda.get_device_name(0)}")
|
| 54 |
|
| 55 |
torch.cuda.set_device(0)
|
|
@@ -100,14 +99,12 @@ def initialize_session_state():
|
|
| 100 |
'bg_color': "#00FF00",
|
| 101 |
'cached_color': None,
|
| 102 |
'color_display_cache': None,
|
| 103 |
-
'
|
| 104 |
-
'processed_video_path': None, # Stage 2 output
|
| 105 |
'processing': False,
|
| 106 |
'gpu_available': None,
|
| 107 |
'last_video_id': None,
|
| 108 |
'last_bg_image_id': None,
|
| 109 |
-
'last_error': None
|
| 110 |
-
'current_stage': None
|
| 111 |
}
|
| 112 |
for key, value in defaults.items():
|
| 113 |
if key not in st.session_state:
|
|
@@ -129,21 +126,6 @@ def main():
|
|
| 129 |
st.success(f"GPU: {torch.cuda.get_device_name(0)}")
|
| 130 |
else:
|
| 131 |
st.error("GPU: Not Available")
|
| 132 |
-
|
| 133 |
-
st.markdown("---")
|
| 134 |
-
st.subheader("Pipeline Status")
|
| 135 |
-
|
| 136 |
-
if st.session_state.transparent_video_path:
|
| 137 |
-
st.success("Stage 1: Transparent video ready")
|
| 138 |
-
else:
|
| 139 |
-
st.info("Stage 1: Not started")
|
| 140 |
-
|
| 141 |
-
if st.session_state.processed_video_path:
|
| 142 |
-
st.success("Stage 2: Final video ready")
|
| 143 |
-
elif st.session_state.transparent_video_path:
|
| 144 |
-
st.info("Stage 2: Ready to composite")
|
| 145 |
-
else:
|
| 146 |
-
st.info("Stage 2: Waiting for Stage 1")
|
| 147 |
|
| 148 |
# Display last error if exists
|
| 149 |
if st.session_state.last_error:
|
|
@@ -171,8 +153,7 @@ def main():
|
|
| 171 |
st.session_state.uploaded_video = uploaded
|
| 172 |
st.session_state.last_video_id = current_video_id
|
| 173 |
st.session_state.video_bytes_cache = None
|
| 174 |
-
st.session_state.
|
| 175 |
-
st.session_state.processed_video_path = None
|
| 176 |
st.session_state.last_error = None
|
| 177 |
|
| 178 |
# Video preview with anti-shake placeholder
|
|
@@ -263,109 +244,81 @@ def main():
|
|
| 263 |
|
| 264 |
st.header("3. Process Video")
|
| 265 |
|
| 266 |
-
|
| 267 |
-
can_process_stage1 = (
|
| 268 |
st.session_state.uploaded_video is not None and
|
| 269 |
-
not st.session_state.processing and
|
| 270 |
-
st.session_state.transparent_video_path is None
|
| 271 |
-
)
|
| 272 |
-
|
| 273 |
-
if st.button("Stage 1: Create Transparent Video", disabled=not can_process_stage1, use_container_width=True):
|
| 274 |
-
logger.info("=" * 60)
|
| 275 |
-
logger.info("STAGE 1 STARTED")
|
| 276 |
-
logger.info("=" * 60)
|
| 277 |
-
|
| 278 |
-
st.session_state.processing = True
|
| 279 |
-
st.session_state.current_stage = 1
|
| 280 |
-
st.session_state.last_error = None
|
| 281 |
-
|
| 282 |
-
try:
|
| 283 |
-
transparent_path = stage1_create_transparent_video(st.session_state.uploaded_video)
|
| 284 |
-
|
| 285 |
-
if transparent_path and os.path.exists(transparent_path):
|
| 286 |
-
st.session_state.transparent_video_path = transparent_path
|
| 287 |
-
logger.info(f"Stage 1 SUCCESS: {transparent_path}")
|
| 288 |
-
st.success("Stage 1 complete! Transparent video created.")
|
| 289 |
-
else:
|
| 290 |
-
error_msg = "Stage 1 failed to create transparent video"
|
| 291 |
-
logger.error(error_msg)
|
| 292 |
-
st.session_state.last_error = error_msg
|
| 293 |
-
st.error(error_msg)
|
| 294 |
-
|
| 295 |
-
except Exception as e:
|
| 296 |
-
error_msg = f"Stage 1 Error: {str(e)}"
|
| 297 |
-
logger.error(error_msg)
|
| 298 |
-
logger.error(traceback.format_exc())
|
| 299 |
-
st.session_state.last_error = error_msg
|
| 300 |
-
st.error(error_msg)
|
| 301 |
-
|
| 302 |
-
finally:
|
| 303 |
-
st.session_state.processing = False
|
| 304 |
-
st.session_state.current_stage = None
|
| 305 |
-
|
| 306 |
-
# Stage 2: Composite with Background
|
| 307 |
-
can_process_stage2 = (
|
| 308 |
-
st.session_state.transparent_video_path is not None and
|
| 309 |
not st.session_state.processing
|
| 310 |
)
|
| 311 |
|
| 312 |
-
if st.button("
|
| 313 |
logger.info("=" * 60)
|
| 314 |
-
logger.info("
|
| 315 |
logger.info("=" * 60)
|
| 316 |
|
| 317 |
st.session_state.processing = True
|
| 318 |
-
st.session_state.
|
| 319 |
st.session_state.last_error = None
|
| 320 |
|
| 321 |
try:
|
|
|
|
| 322 |
background = None
|
| 323 |
if bg_type == "Image" and st.session_state.bg_image_cache is not None:
|
| 324 |
background = st.session_state.bg_image_cache
|
|
|
|
| 325 |
elif bg_type == "Color":
|
| 326 |
background = st.session_state.bg_color
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 327 |
|
|
|
|
|
|
|
| 328 |
final_path = stage2_composite_background(
|
| 329 |
-
|
| 330 |
background,
|
| 331 |
bg_type.lower()
|
| 332 |
)
|
| 333 |
|
| 334 |
-
if final_path
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
st.session_state.
|
| 342 |
-
|
|
|
|
|
|
|
|
|
|
| 343 |
|
| 344 |
except Exception as e:
|
| 345 |
-
error_msg = f"
|
| 346 |
-
logger.error(
|
| 347 |
logger.error(traceback.format_exc())
|
| 348 |
st.session_state.last_error = error_msg
|
| 349 |
st.error(error_msg)
|
| 350 |
|
| 351 |
finally:
|
| 352 |
st.session_state.processing = False
|
| 353 |
-
st.session_state.current_stage = None
|
| 354 |
|
| 355 |
-
# Show
|
| 356 |
-
if st.session_state.
|
| 357 |
st.markdown("---")
|
| 358 |
-
st.markdown("###
|
| 359 |
|
| 360 |
try:
|
| 361 |
-
|
| 362 |
-
video_bytes = f.read()
|
| 363 |
-
|
| 364 |
-
st.video(video_bytes)
|
| 365 |
st.download_button(
|
| 366 |
-
label="Download
|
| 367 |
-
data=
|
| 368 |
-
file_name="
|
| 369 |
mime="video/mp4",
|
| 370 |
use_container_width=True
|
| 371 |
)
|
|
|
|
| 1 |
+
# streamlit_ui.py - Simplified single-button workflow
|
| 2 |
|
| 3 |
import streamlit as st
|
| 4 |
import os
|
|
|
|
| 49 |
if cuda_available:
|
| 50 |
logger.info(f"CUDA Version: {torch.version.cuda}")
|
| 51 |
logger.info(f"Device Count: {torch.cuda.device_count()}")
|
|
|
|
| 52 |
logger.info(f"Device Name: {torch.cuda.get_device_name(0)}")
|
| 53 |
|
| 54 |
torch.cuda.set_device(0)
|
|
|
|
| 99 |
'bg_color': "#00FF00",
|
| 100 |
'cached_color': None,
|
| 101 |
'color_display_cache': None,
|
| 102 |
+
'processed_video_bytes': None,
|
|
|
|
| 103 |
'processing': False,
|
| 104 |
'gpu_available': None,
|
| 105 |
'last_video_id': None,
|
| 106 |
'last_bg_image_id': None,
|
| 107 |
+
'last_error': None
|
|
|
|
| 108 |
}
|
| 109 |
for key, value in defaults.items():
|
| 110 |
if key not in st.session_state:
|
|
|
|
| 126 |
st.success(f"GPU: {torch.cuda.get_device_name(0)}")
|
| 127 |
else:
|
| 128 |
st.error("GPU: Not Available")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
# Display last error if exists
|
| 131 |
if st.session_state.last_error:
|
|
|
|
| 153 |
st.session_state.uploaded_video = uploaded
|
| 154 |
st.session_state.last_video_id = current_video_id
|
| 155 |
st.session_state.video_bytes_cache = None
|
| 156 |
+
st.session_state.processed_video_bytes = None
|
|
|
|
| 157 |
st.session_state.last_error = None
|
| 158 |
|
| 159 |
# Video preview with anti-shake placeholder
|
|
|
|
| 244 |
|
| 245 |
st.header("3. Process Video")
|
| 246 |
|
| 247 |
+
can_process = (
|
|
|
|
| 248 |
st.session_state.uploaded_video is not None and
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
not st.session_state.processing
|
| 250 |
)
|
| 251 |
|
| 252 |
+
if st.button("Process Video", disabled=not can_process, use_container_width=True):
|
| 253 |
logger.info("=" * 60)
|
| 254 |
+
logger.info("VIDEO PROCESSING STARTED")
|
| 255 |
logger.info("=" * 60)
|
| 256 |
|
| 257 |
st.session_state.processing = True
|
| 258 |
+
st.session_state.processed_video_bytes = None
|
| 259 |
st.session_state.last_error = None
|
| 260 |
|
| 261 |
try:
|
| 262 |
+
# Determine background
|
| 263 |
background = None
|
| 264 |
if bg_type == "Image" and st.session_state.bg_image_cache is not None:
|
| 265 |
background = st.session_state.bg_image_cache
|
| 266 |
+
logger.info("Using IMAGE background")
|
| 267 |
elif bg_type == "Color":
|
| 268 |
background = st.session_state.bg_color
|
| 269 |
+
logger.info(f"Using COLOR background: {background}")
|
| 270 |
+
|
| 271 |
+
# Stage 1: Create transparent video (0-70%)
|
| 272 |
+
logger.info("Starting Stage 1: Transparent video creation...")
|
| 273 |
+
transparent_path = stage1_create_transparent_video(st.session_state.uploaded_video)
|
| 274 |
+
|
| 275 |
+
if not transparent_path or not os.path.exists(transparent_path):
|
| 276 |
+
raise RuntimeError("Stage 1 failed: Transparent video not created")
|
| 277 |
+
|
| 278 |
+
logger.info(f"Stage 1 complete: {transparent_path}")
|
| 279 |
|
| 280 |
+
# Stage 2: Composite with background (70-100%)
|
| 281 |
+
logger.info("Starting Stage 2: Compositing with background...")
|
| 282 |
final_path = stage2_composite_background(
|
| 283 |
+
transparent_path,
|
| 284 |
background,
|
| 285 |
bg_type.lower()
|
| 286 |
)
|
| 287 |
|
| 288 |
+
if not final_path or not os.path.exists(final_path):
|
| 289 |
+
raise RuntimeError("Stage 2 failed: Final video not created")
|
| 290 |
+
|
| 291 |
+
logger.info(f"Stage 2 complete: {final_path}")
|
| 292 |
+
|
| 293 |
+
# Load final video into session state
|
| 294 |
+
with open(final_path, 'rb') as f:
|
| 295 |
+
st.session_state.processed_video_bytes = f.read()
|
| 296 |
+
|
| 297 |
+
logger.info(f"Processing SUCCESS: {len(st.session_state.processed_video_bytes)/1e6:.2f}MB")
|
| 298 |
+
st.success("Video processing complete!")
|
| 299 |
+
st.session_state.last_error = None
|
| 300 |
|
| 301 |
except Exception as e:
|
| 302 |
+
error_msg = f"Processing Error: {str(e)}\n\nCheck logs for details."
|
| 303 |
+
logger.error(f"Exception: {str(e)}")
|
| 304 |
logger.error(traceback.format_exc())
|
| 305 |
st.session_state.last_error = error_msg
|
| 306 |
st.error(error_msg)
|
| 307 |
|
| 308 |
finally:
|
| 309 |
st.session_state.processing = False
|
|
|
|
| 310 |
|
| 311 |
+
# Show processed video if available
|
| 312 |
+
if st.session_state.processed_video_bytes is not None:
|
| 313 |
st.markdown("---")
|
| 314 |
+
st.markdown("### Processed Video")
|
| 315 |
|
| 316 |
try:
|
| 317 |
+
st.video(st.session_state.processed_video_bytes)
|
|
|
|
|
|
|
|
|
|
| 318 |
st.download_button(
|
| 319 |
+
label="Download Processed Video",
|
| 320 |
+
data=st.session_state.processed_video_bytes,
|
| 321 |
+
file_name="processed_video.mp4",
|
| 322 |
mime="video/mp4",
|
| 323 |
use_container_width=True
|
| 324 |
)
|