MogensR commited on
Commit
f642dbf
Β·
verified Β·
1 Parent(s): 52bee8a

Update streamlit_app.py

Browse files
Files changed (1) hide show
  1. streamlit_app.py +16 -2
streamlit_app.py CHANGED
@@ -83,7 +83,7 @@ def initialize_session_state():
83
  if st.session_state.gpu_available is None:
84
  st.session_state.gpu_available = check_gpu(logger)
85
 
86
- def process_video(uploaded_video, background, bg_type):
87
  run_id = uuid.uuid4().hex[:8]
88
  logger.info("=" * 80)
89
  logger.info(f"[RUN {run_id}] VIDEO PROCESSING STARTED at {time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime())}")
@@ -94,12 +94,17 @@ def process_video(uploaded_video, background, bg_type):
94
  st.session_state.last_error = None
95
  t0 = time.time()
96
  try:
 
 
97
  suffix = Path(uploaded_video.name).suffix or ".mp4"
98
  with NamedTemporaryFile(delete=False, suffix=suffix) as tmp_vid:
99
  uploaded_video.seek(0)
100
  tmp_vid.write(uploaded_video.read())
101
  tmp_vid_path = tmp_vid.name
102
  logger.info(f"[RUN {run_id}] Temporary video path: {tmp_vid_path}")
 
 
 
103
  transparent_path, audio_path = stage1_create_transparent_video(
104
  tmp_vid_path,
105
  sam2_predictor=sam2_predictor,
@@ -108,7 +113,9 @@ def process_video(uploaded_video, background, bg_type):
108
  if not transparent_path or not os.path.exists(transparent_path):
109
  raise RuntimeError("Stage 1 failed: Transparent video not created")
110
  logger.info(f"[RUN {run_id}] Stage 1 completed: Transparent path={transparent_path}, Audio path={audio_path}")
111
- # For "color", background is "#RRGGBB", for others it's PIL.Image
 
 
112
  final_path = stage2_composite_background(
113
  transparent_path,
114
  audio_path,
@@ -118,10 +125,15 @@ def process_video(uploaded_video, background, bg_type):
118
  if not final_path or not os.path.exists(final_path):
119
  raise RuntimeError("Stage 2 failed: Final video not created")
120
  logger.info(f"[RUN {run_id}] Stage 2 completed: Final path={final_path}")
 
 
 
121
  with open(final_path, 'rb') as f:
122
  st.session_state.processed_video_bytes = f.read()
123
  total = time.time() - t0
124
  logger.info(f"[RUN {run_id}] SUCCESS size={len(st.session_state.processed_video_bytes)/1e6:.2f}MB, total Ξ”={total:.2f}s")
 
 
125
  return True
126
  except Exception as e:
127
  total = time.time() - t0
@@ -129,6 +141,8 @@ def process_video(uploaded_video, background, bg_type):
129
  logger.error(error_msg)
130
  logger.error(traceback.format_exc())
131
  st.session_state.last_error = error_msg
 
 
132
  return False
133
  finally:
134
  st.session_state.processing = False
 
83
  if st.session_state.gpu_available is None:
84
  st.session_state.gpu_available = check_gpu(logger)
85
 
86
+ def process_video(uploaded_video, background, bg_type, progress_callback=None):
87
  run_id = uuid.uuid4().hex[:8]
88
  logger.info("=" * 80)
89
  logger.info(f"[RUN {run_id}] VIDEO PROCESSING STARTED at {time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime())}")
 
94
  st.session_state.last_error = None
95
  t0 = time.time()
96
  try:
97
+ if progress_callback:
98
+ progress_callback("πŸ“₯ Uploading video...")
99
  suffix = Path(uploaded_video.name).suffix or ".mp4"
100
  with NamedTemporaryFile(delete=False, suffix=suffix) as tmp_vid:
101
  uploaded_video.seek(0)
102
  tmp_vid.write(uploaded_video.read())
103
  tmp_vid_path = tmp_vid.name
104
  logger.info(f"[RUN {run_id}] Temporary video path: {tmp_vid_path}")
105
+
106
+ if progress_callback:
107
+ progress_callback("πŸš€ Stage 1: Creating transparent video (matting & segmentation)...")
108
  transparent_path, audio_path = stage1_create_transparent_video(
109
  tmp_vid_path,
110
  sam2_predictor=sam2_predictor,
 
113
  if not transparent_path or not os.path.exists(transparent_path):
114
  raise RuntimeError("Stage 1 failed: Transparent video not created")
115
  logger.info(f"[RUN {run_id}] Stage 1 completed: Transparent path={transparent_path}, Audio path={audio_path}")
116
+
117
+ if progress_callback:
118
+ progress_callback("🎨 Stage 2: Compositing with background and restoring audio...")
119
  final_path = stage2_composite_background(
120
  transparent_path,
121
  audio_path,
 
125
  if not final_path or not os.path.exists(final_path):
126
  raise RuntimeError("Stage 2 failed: Final video not created")
127
  logger.info(f"[RUN {run_id}] Stage 2 completed: Final path={final_path}")
128
+
129
+ if progress_callback:
130
+ progress_callback("πŸ“€ Loading final video for download...")
131
  with open(final_path, 'rb') as f:
132
  st.session_state.processed_video_bytes = f.read()
133
  total = time.time() - t0
134
  logger.info(f"[RUN {run_id}] SUCCESS size={len(st.session_state.processed_video_bytes)/1e6:.2f}MB, total Ξ”={total:.2f}s")
135
+ if progress_callback:
136
+ progress_callback("βœ… All done!")
137
  return True
138
  except Exception as e:
139
  total = time.time() - t0
 
141
  logger.error(error_msg)
142
  logger.error(traceback.format_exc())
143
  st.session_state.last_error = error_msg
144
+ if progress_callback:
145
+ progress_callback(f"❌ ERROR: {str(e)}")
146
  return False
147
  finally:
148
  st.session_state.processing = False