MogensR commited on
Commit
a43d32f
·
verified ·
1 Parent(s): 175db35

Update streamlit_app.py

Browse files
Files changed (1) hide show
  1. streamlit_app.py +88 -35
streamlit_app.py CHANGED
@@ -1,4 +1,13 @@
1
- # streamlit_ui.py - Simplified single-button workflow
 
 
 
 
 
 
 
 
 
2
 
3
  import streamlit as st
4
  import os
@@ -28,6 +37,10 @@ def custom_excepthook(type, value, tb):
28
  logger.error(f"Unhandled: {type.__name__}: {value}\n{''.join(traceback.format_tb(tb))}", exc_info=True)
29
  sys.excepthook = custom_excepthook
30
 
 
 
 
 
31
  # Streamlit Page Config
32
  st.set_page_config(
33
  page_title="Advanced Video Background Replacer",
@@ -36,7 +49,30 @@ def custom_excepthook(type, value, tb):
36
  initial_sidebar_state="expanded"
37
  )
38
 
39
- # GPU Diagnostic
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  def check_gpu():
41
  """Check GPU availability and log details"""
42
  logger.info("=" * 60)
@@ -67,27 +103,10 @@ def check_gpu():
67
  logger.info("=" * 60)
68
  return cuda_available
69
 
70
- # Custom CSS
71
- st.markdown("""
72
- <style>
73
- .main .block-container {
74
- padding-top: 2rem;
75
- padding-bottom: 2rem;
76
- }
77
- .stButton>button {
78
- width: 100%;
79
- background-color: #4CAF50;
80
- color: white;
81
- font-weight: bold;
82
- transition: all 0.3s;
83
- }
84
- .stButton>button:hover {
85
- background-color: #45a049;
86
- }
87
- </style>
88
- """, unsafe_allow_html=True)
89
 
90
- # Session State Initialization
91
  def initialize_session_state():
92
  """Initialize all session state variables"""
93
  defaults = {
@@ -113,13 +132,20 @@ def initialize_session_state():
113
  if st.session_state.gpu_available is None:
114
  st.session_state.gpu_available = check_gpu()
115
 
116
- # Main Application
 
 
 
117
  def main():
118
  st.title("Advanced Video Background Replacer")
119
  st.markdown("---")
120
 
121
  initialize_session_state()
122
 
 
 
 
 
123
  with st.sidebar:
124
  st.subheader("System Status")
125
  if st.session_state.gpu_available:
@@ -127,7 +153,10 @@ def main():
127
  else:
128
  st.error("GPU: Not Available")
129
 
130
- # Display last error if exists
 
 
 
131
  if st.session_state.last_error:
132
  with st.expander("⚠️ Last Error", expanded=False):
133
  st.error(st.session_state.last_error)
@@ -137,6 +166,10 @@ def main():
137
 
138
  col1, col2 = st.columns([1, 1], gap="large")
139
 
 
 
 
 
140
  with col1:
141
  st.header("1. Upload Video")
142
 
@@ -146,10 +179,10 @@ def main():
146
  key="video_uploader"
147
  )
148
 
149
- # Check if video changed using id()
150
  current_video_id = id(uploaded)
151
- if current_video_id != st.session_state.last_video_id:
152
- logger.info(f"New video: {uploaded.name if uploaded else 'None'}")
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
@@ -179,6 +212,10 @@ def main():
179
  else:
180
  st.session_state.video_preview_placeholder.empty()
181
 
 
 
 
 
182
  with col2:
183
  st.header("2. Background Settings")
184
 
@@ -189,6 +226,10 @@ def main():
189
  key="bg_type_radio"
190
  )
191
 
 
 
 
 
192
  if bg_type == "Image":
193
  bg_image = st.file_uploader(
194
  "Upload Background Image",
@@ -196,15 +237,12 @@ def main():
196
  key="bg_image_uploader"
197
  )
198
 
199
- # Check if image changed using id()
200
  current_bg_id = id(bg_image)
201
- if current_bg_id != st.session_state.last_bg_image_id:
202
- logger.info(f"New background: {bg_image.name if bg_image else 'None'}")
203
  st.session_state.last_bg_image_id = current_bg_id
204
- if bg_image is not None:
205
- st.session_state.bg_image_cache = Image.open(bg_image)
206
- else:
207
- st.session_state.bg_image_cache = None
208
 
209
  # Background preview with anti-shake placeholder
210
  if st.session_state.bg_preview_placeholder is None:
@@ -216,6 +254,10 @@ def main():
216
  else:
217
  st.session_state.bg_preview_placeholder.empty()
218
 
 
 
 
 
219
  elif bg_type == "Color":
220
  selected_color = st.color_picker(
221
  "Choose Background Color",
@@ -242,6 +284,10 @@ def main():
242
  else:
243
  st.session_state.bg_preview_placeholder.empty()
244
 
 
 
 
 
245
  st.header("3. Process Video")
246
 
247
  can_process = (
@@ -308,7 +354,10 @@ def main():
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")
@@ -326,5 +375,9 @@ def main():
326
  logger.error(f"Display error: {e}")
327
  st.error(f"Display error: {e}")
328
 
 
 
 
 
329
  if __name__ == "__main__":
330
  main()
 
1
+ # ==================================================================================
2
+ # STREAMLIT VIDEO BACKGROUND REPLACER - MAIN APPLICATION
3
+ # ==================================================================================
4
+ # Single-button workflow: Upload video + background → Process → Download result
5
+ # Uses SAM2 + MatAnyone pipeline with temporal smoothing
6
+ # ==================================================================================
7
+
8
+ # ==================================================================================
9
+ # CHAPTER 1: IMPORTS AND SETUP
10
+ # ==================================================================================
11
 
12
  import streamlit as st
13
  import os
 
37
  logger.error(f"Unhandled: {type.__name__}: {value}\n{''.join(traceback.format_tb(tb))}", exc_info=True)
38
  sys.excepthook = custom_excepthook
39
 
40
+ # ==================================================================================
41
+ # CHAPTER 2: STREAMLIT CONFIGURATION
42
+ # ==================================================================================
43
+
44
  # Streamlit Page Config
45
  st.set_page_config(
46
  page_title="Advanced Video Background Replacer",
 
49
  initial_sidebar_state="expanded"
50
  )
51
 
52
+ # Custom CSS
53
+ st.markdown("""
54
+ <style>
55
+ .main .block-container {
56
+ padding-top: 2rem;
57
+ padding-bottom: 2rem;
58
+ }
59
+ .stButton>button {
60
+ width: 100%;
61
+ background-color: #4CAF50;
62
+ color: white;
63
+ font-weight: bold;
64
+ transition: all 0.3s;
65
+ }
66
+ .stButton>button:hover {
67
+ background-color: #45a049;
68
+ }
69
+ </style>
70
+ """, unsafe_allow_html=True)
71
+
72
+ # ==================================================================================
73
+ # CHAPTER 3: GPU DIAGNOSTICS
74
+ # ==================================================================================
75
+
76
  def check_gpu():
77
  """Check GPU availability and log details"""
78
  logger.info("=" * 60)
 
103
  logger.info("=" * 60)
104
  return cuda_available
105
 
106
+ # ==================================================================================
107
+ # CHAPTER 4: SESSION STATE INITIALIZATION
108
+ # ==================================================================================
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
 
110
  def initialize_session_state():
111
  """Initialize all session state variables"""
112
  defaults = {
 
132
  if st.session_state.gpu_available is None:
133
  st.session_state.gpu_available = check_gpu()
134
 
135
+ # ==================================================================================
136
+ # CHAPTER 5: MAIN APPLICATION
137
+ # ==================================================================================
138
+
139
  def main():
140
  st.title("Advanced Video Background Replacer")
141
  st.markdown("---")
142
 
143
  initialize_session_state()
144
 
145
+ # ==================================================================================
146
+ # SIDEBAR: System Status
147
+ # ==================================================================================
148
+
149
  with st.sidebar:
150
  st.subheader("System Status")
151
  if st.session_state.gpu_available:
 
153
  else:
154
  st.error("GPU: Not Available")
155
 
156
+ # ==================================================================================
157
+ # ERROR DISPLAY
158
+ # ==================================================================================
159
+
160
  if st.session_state.last_error:
161
  with st.expander("⚠️ Last Error", expanded=False):
162
  st.error(st.session_state.last_error)
 
166
 
167
  col1, col2 = st.columns([1, 1], gap="large")
168
 
169
+ # ==================================================================================
170
+ # COLUMN 1: VIDEO UPLOAD
171
+ # ==================================================================================
172
+
173
  with col1:
174
  st.header("1. Upload Video")
175
 
 
179
  key="video_uploader"
180
  )
181
 
182
+ # FIX: Only update session state when uploaded is not None
183
  current_video_id = id(uploaded)
184
+ if uploaded is not None and current_video_id != st.session_state.last_video_id:
185
+ logger.info(f"New video: {uploaded.name}")
186
  st.session_state.uploaded_video = uploaded
187
  st.session_state.last_video_id = current_video_id
188
  st.session_state.video_bytes_cache = None
 
212
  else:
213
  st.session_state.video_preview_placeholder.empty()
214
 
215
+ # ==================================================================================
216
+ # COLUMN 2: BACKGROUND SETTINGS & PROCESSING
217
+ # ==================================================================================
218
+
219
  with col2:
220
  st.header("2. Background Settings")
221
 
 
226
  key="bg_type_radio"
227
  )
228
 
229
+ # ==================================================================================
230
+ # BACKGROUND IMAGE UPLOAD
231
+ # ==================================================================================
232
+
233
  if bg_type == "Image":
234
  bg_image = st.file_uploader(
235
  "Upload Background Image",
 
237
  key="bg_image_uploader"
238
  )
239
 
240
+ # FIX: Only update session state when bg_image is not None
241
  current_bg_id = id(bg_image)
242
+ if bg_image is not None and current_bg_id != st.session_state.last_bg_image_id:
243
+ logger.info(f"New background: {bg_image.name}")
244
  st.session_state.last_bg_image_id = current_bg_id
245
+ st.session_state.bg_image_cache = Image.open(bg_image)
 
 
 
246
 
247
  # Background preview with anti-shake placeholder
248
  if st.session_state.bg_preview_placeholder is None:
 
254
  else:
255
  st.session_state.bg_preview_placeholder.empty()
256
 
257
+ # ==================================================================================
258
+ # BACKGROUND COLOR PICKER
259
+ # ==================================================================================
260
+
261
  elif bg_type == "Color":
262
  selected_color = st.color_picker(
263
  "Choose Background Color",
 
284
  else:
285
  st.session_state.bg_preview_placeholder.empty()
286
 
287
+ # ==================================================================================
288
+ # VIDEO PROCESSING SECTION
289
+ # ==================================================================================
290
+
291
  st.header("3. Process Video")
292
 
293
  can_process = (
 
354
  finally:
355
  st.session_state.processing = False
356
 
357
+ # ==================================================================================
358
+ # RESULTS DISPLAY
359
+ # ==================================================================================
360
+
361
  if st.session_state.processed_video_bytes is not None:
362
  st.markdown("---")
363
  st.markdown("### Processed Video")
 
375
  logger.error(f"Display error: {e}")
376
  st.error(f"Display error: {e}")
377
 
378
+ # ==================================================================================
379
+ # CHAPTER 6: APPLICATION ENTRY POINT
380
+ # ==================================================================================
381
+
382
  if __name__ == "__main__":
383
  main()