MogensR commited on
Commit
007d28d
Β·
verified Β·
1 Parent(s): 30b6279

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -47
app.py CHANGED
@@ -4,17 +4,8 @@
4
  import torch
5
  import tempfile
6
  import os
7
- import logging
8
- from pathlib import Path
9
  import time
10
-
11
- # Configure logging
12
- logging.basicConfig(
13
- level=logging.INFO,
14
- format='[%(asctime)s] %(levelname)s: %(message)s',
15
- datefmt='%H:%M:%S'
16
- )
17
- logger = logging.getLogger(__name__)
18
 
19
  # Import model loaders
20
  from model_loaders import load_sam2, load_matanyone, pose
@@ -25,7 +16,6 @@ def log_and_progress(progress_callback, stage, progress, message):
25
  """Unified logging and progress reporting"""
26
  timestamp = time.strftime("%H:%M:%S")
27
  log_msg = f"[{timestamp}] Stage {stage} ({progress:.0%}): {message}"
28
- logger.info(log_msg)
29
  print(log_msg, flush=True)
30
  progress_callback(stage, progress, message)
31
 
@@ -35,18 +25,18 @@ def process_video(input_path, output_path, progress_callback):
35
  # ============================================================
36
  # STAGE 1: SEGMENTATION (Load models, read video, segment all frames)
37
  # ============================================================
38
- logger.info("="*60)
39
- logger.info("STAGE 1: PERSON SEGMENTATION")
40
- logger.info("="*60)
41
  stage1_start = time.time()
42
 
43
  # 1.1: Load SAM2
44
  log_and_progress(progress_callback, 1, 0.0, "Loading SAM2 model...")
45
  try:
46
  sam_predictor = load_sam2()
47
- logger.info(f"βœ… SAM2 loaded successfully")
48
  except Exception as e:
49
- logger.error(f"❌ SAM2 loading failed: {e}")
50
  raise
51
 
52
  # 1.2: Load video
@@ -61,9 +51,9 @@ def process_video(input_path, output_path, progress_callback):
61
  height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
62
  total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
63
 
64
- logger.info(f"πŸ“Ή Video: {width}x{height} @ {fps:.2f}fps, {total_frames} frames")
65
  except Exception as e:
66
- logger.error(f"❌ Video opening failed: {e}")
67
  raise
68
 
69
  # 1.3: Read all frames
@@ -75,7 +65,7 @@ def process_video(input_path, output_path, progress_callback):
75
  break
76
  frames.append(frame)
77
  cap.release()
78
- logger.info(f"βœ… Read {len(frames)} frames")
79
 
80
  # 1.4: Segment all frames
81
  log_and_progress(progress_callback, 1, 0.2, "Starting person segmentation...")
@@ -117,31 +107,31 @@ def process_video(input_path, output_path, progress_callback):
117
  masks.append(np.zeros((h, w), dtype=bool))
118
 
119
  except Exception as e:
120
- logger.error(f"❌ Frame {i+1} segmentation failed: {e}")
121
  h, w = frame.shape[:2]
122
  masks.append(np.zeros((h, w), dtype=bool))
123
 
124
  stage1_time = time.time() - stage1_start
125
- logger.info("="*60)
126
- logger.info(f"βœ… STAGE 1 COMPLETE in {stage1_time:.1f}s")
127
- logger.info(f" Segmented {len(masks)} frames")
128
- logger.info("="*60)
129
 
130
  # ============================================================
131
  # STAGE 2: MATTING (Refine all masks, smooth, write video)
132
  # ============================================================
133
- logger.info("="*60)
134
- logger.info("STAGE 2: HIGH-QUALITY MATTING")
135
- logger.info("="*60)
136
  stage2_start = time.time()
137
 
138
  # 2.1: Load MatAnyone
139
  log_and_progress(progress_callback, 2, 0.0, "Loading MatAnyone model...")
140
  try:
141
  matanyone = load_matanyone()
142
- logger.info(f"βœ… MatAnyone loaded successfully")
143
  except Exception as e:
144
- logger.error(f"❌ MatAnyone loading failed: {e}")
145
  raise
146
 
147
  # 2.2: Process all frames with MatAnyone
@@ -168,10 +158,10 @@ def process_video(input_path, output_path, progress_callback):
168
  alphas.append(alpha)
169
 
170
  except Exception as e:
171
- logger.error(f"❌ Frame {i+1} matting failed: {e}")
172
  alphas.append(np.zeros((frame.shape[0], frame.shape[1]), dtype=np.float32))
173
 
174
- logger.info(f"βœ… Matted {len(alphas)} frames")
175
 
176
  # 2.3: Temporal smoothing
177
  log_and_progress(progress_callback, 2, 0.65, "Applying temporal smoothing to eliminate jitter...")
@@ -181,6 +171,9 @@ def process_video(input_path, output_path, progress_callback):
181
  half_window = window_size // 2
182
 
183
  for i in range(len(alphas)):
 
 
 
184
  start_idx = max(0, i - half_window)
185
  end_idx = min(len(alphas), i + half_window + 1)
186
  window_alphas = alphas[start_idx:end_idx]
@@ -189,11 +182,11 @@ def process_video(input_path, output_path, progress_callback):
189
  smoothed = np.mean(window_alphas, axis=0)
190
  smoothed_alphas.append(smoothed)
191
 
192
- logger.info(f"βœ… Applied {window_size}-frame temporal smoothing")
193
  alphas = smoothed_alphas
194
 
195
  except Exception as e:
196
- logger.error(f"⚠️ Smoothing failed: {e}, using unsmoothed alphas")
197
 
198
  # 2.4: Write output video
199
  log_and_progress(progress_callback, 2, 0.75, "Writing output video...")
@@ -215,23 +208,23 @@ def process_video(input_path, output_path, progress_callback):
215
  out.write(output)
216
 
217
  out.release()
218
- logger.info(f"βœ… Video written to {output_path}")
219
 
220
  except Exception as e:
221
- logger.error(f"❌ Video writing failed: {e}")
222
  raise
223
 
224
  stage2_time = time.time() - stage2_start
225
  total_time = stage1_time + stage2_time
226
 
227
- logger.info("="*60)
228
- logger.info(f"βœ… STAGE 2 COMPLETE in {stage2_time:.1f}s")
229
- logger.info("="*60)
230
- logger.info(f"πŸŽ‰ TOTAL PROCESSING TIME: {total_time:.1f}s")
231
- logger.info(f" Stage 1 (Segmentation): {stage1_time:.1f}s")
232
- logger.info(f" Stage 2 (Matting): {stage2_time:.1f}s")
233
- logger.info(f" Average: {total_time/len(frames):.2f}s per frame")
234
- logger.info("="*60)
235
 
236
  log_and_progress(progress_callback, 2, 1.0, "Processing complete!")
237
  return output_path
@@ -251,6 +244,10 @@ def main():
251
  output_path = tempfile.mktemp(suffix='_output.mp4')
252
 
253
  if st.button("πŸš€ Process Video", type="primary"):
 
 
 
 
254
  # Progress tracking
255
  stage1_progress = st.progress(0, text="Stage 1: Initializing...")
256
  stage1_status = st.empty()
@@ -267,7 +264,6 @@ def update_progress(stage, progress, message):
267
  stage2_status.info(f"πŸ”„ {message}")
268
 
269
  try:
270
- logger.info("🎬 Starting video processing...")
271
  result_path = process_video(input_path, output_path, update_progress)
272
 
273
  stage1_status.success("βœ… Stage 1: Segmentation complete!")
@@ -288,9 +284,15 @@ def update_progress(stage, progress, message):
288
  st.video(result_path)
289
 
290
  except Exception as e:
291
- logger.exception("Processing failed")
 
 
 
 
 
 
292
  st.error(f"❌ Processing failed: {str(e)}")
293
- st.error("Check the logs above for details")
294
 
295
  finally:
296
  # Cleanup
@@ -302,4 +304,4 @@ def update_progress(stage, progress, message):
302
  pass
303
 
304
  if __name__ == "__main__":
305
- main()
 
4
  import torch
5
  import tempfile
6
  import os
 
 
7
  import time
8
+ from pathlib import Path
 
 
 
 
 
 
 
9
 
10
  # Import model loaders
11
  from model_loaders import load_sam2, load_matanyone, pose
 
16
  """Unified logging and progress reporting"""
17
  timestamp = time.strftime("%H:%M:%S")
18
  log_msg = f"[{timestamp}] Stage {stage} ({progress:.0%}): {message}"
 
19
  print(log_msg, flush=True)
20
  progress_callback(stage, progress, message)
21
 
 
25
  # ============================================================
26
  # STAGE 1: SEGMENTATION (Load models, read video, segment all frames)
27
  # ============================================================
28
+ print("="*60, flush=True)
29
+ print("STAGE 1: PERSON SEGMENTATION", flush=True)
30
+ print("="*60, flush=True)
31
  stage1_start = time.time()
32
 
33
  # 1.1: Load SAM2
34
  log_and_progress(progress_callback, 1, 0.0, "Loading SAM2 model...")
35
  try:
36
  sam_predictor = load_sam2()
37
+ print(f"βœ… SAM2 loaded successfully", flush=True)
38
  except Exception as e:
39
+ print(f"❌ SAM2 loading failed: {e}", flush=True)
40
  raise
41
 
42
  # 1.2: Load video
 
51
  height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
52
  total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
53
 
54
+ print(f"πŸ“Ή Video: {width}x{height} @ {fps:.2f}fps, {total_frames} frames", flush=True)
55
  except Exception as e:
56
+ print(f"❌ Video opening failed: {e}", flush=True)
57
  raise
58
 
59
  # 1.3: Read all frames
 
65
  break
66
  frames.append(frame)
67
  cap.release()
68
+ print(f"βœ… Read {len(frames)} frames", flush=True)
69
 
70
  # 1.4: Segment all frames
71
  log_and_progress(progress_callback, 1, 0.2, "Starting person segmentation...")
 
107
  masks.append(np.zeros((h, w), dtype=bool))
108
 
109
  except Exception as e:
110
+ print(f"❌ Frame {i+1} segmentation failed: {e}", flush=True)
111
  h, w = frame.shape[:2]
112
  masks.append(np.zeros((h, w), dtype=bool))
113
 
114
  stage1_time = time.time() - stage1_start
115
+ print("="*60, flush=True)
116
+ print(f"βœ… STAGE 1 COMPLETE in {stage1_time:.1f}s", flush=True)
117
+ print(f" Segmented {len(masks)} frames", flush=True)
118
+ print("="*60, flush=True)
119
 
120
  # ============================================================
121
  # STAGE 2: MATTING (Refine all masks, smooth, write video)
122
  # ============================================================
123
+ print("="*60, flush=True)
124
+ print("STAGE 2: HIGH-QUALITY MATTING", flush=True)
125
+ print("="*60, flush=True)
126
  stage2_start = time.time()
127
 
128
  # 2.1: Load MatAnyone
129
  log_and_progress(progress_callback, 2, 0.0, "Loading MatAnyone model...")
130
  try:
131
  matanyone = load_matanyone()
132
+ print(f"βœ… MatAnyone loaded successfully", flush=True)
133
  except Exception as e:
134
+ print(f"❌ MatAnyone loading failed: {e}", flush=True)
135
  raise
136
 
137
  # 2.2: Process all frames with MatAnyone
 
158
  alphas.append(alpha)
159
 
160
  except Exception as e:
161
+ print(f"❌ Frame {i+1} matting failed: {e}", flush=True)
162
  alphas.append(np.zeros((frame.shape[0], frame.shape[1]), dtype=np.float32))
163
 
164
+ print(f"βœ… Matted {len(alphas)} frames", flush=True)
165
 
166
  # 2.3: Temporal smoothing
167
  log_and_progress(progress_callback, 2, 0.65, "Applying temporal smoothing to eliminate jitter...")
 
171
  half_window = window_size // 2
172
 
173
  for i in range(len(alphas)):
174
+ if i % 100 == 0:
175
+ print(f"Smoothing frame {i}/{len(alphas)}...", flush=True)
176
+
177
  start_idx = max(0, i - half_window)
178
  end_idx = min(len(alphas), i + half_window + 1)
179
  window_alphas = alphas[start_idx:end_idx]
 
182
  smoothed = np.mean(window_alphas, axis=0)
183
  smoothed_alphas.append(smoothed)
184
 
185
+ print(f"βœ… Applied {window_size}-frame temporal smoothing", flush=True)
186
  alphas = smoothed_alphas
187
 
188
  except Exception as e:
189
+ print(f"⚠️ Smoothing failed: {e}, using unsmoothed alphas", flush=True)
190
 
191
  # 2.4: Write output video
192
  log_and_progress(progress_callback, 2, 0.75, "Writing output video...")
 
208
  out.write(output)
209
 
210
  out.release()
211
+ print(f"βœ… Video written to {output_path}", flush=True)
212
 
213
  except Exception as e:
214
+ print(f"❌ Video writing failed: {e}", flush=True)
215
  raise
216
 
217
  stage2_time = time.time() - stage2_start
218
  total_time = stage1_time + stage2_time
219
 
220
+ print("="*60, flush=True)
221
+ print(f"βœ… STAGE 2 COMPLETE in {stage2_time:.1f}s", flush=True)
222
+ print("="*60, flush=True)
223
+ print(f"πŸŽ‰ TOTAL PROCESSING TIME: {total_time:.1f}s", flush=True)
224
+ print(f" Stage 1 (Segmentation): {stage1_time:.1f}s", flush=True)
225
+ print(f" Stage 2 (Matting): {stage2_time:.1f}s", flush=True)
226
+ print(f" Average: {total_time/len(frames):.2f}s per frame", flush=True)
227
+ print("="*60, flush=True)
228
 
229
  log_and_progress(progress_callback, 2, 1.0, "Processing complete!")
230
  return output_path
 
244
  output_path = tempfile.mktemp(suffix='_output.mp4')
245
 
246
  if st.button("πŸš€ Process Video", type="primary"):
247
+ print(f"\n\n{'='*60}", flush=True)
248
+ print(f"🎬 NEW VIDEO PROCESSING STARTED", flush=True)
249
+ print(f"{'='*60}\n", flush=True)
250
+
251
  # Progress tracking
252
  stage1_progress = st.progress(0, text="Stage 1: Initializing...")
253
  stage1_status = st.empty()
 
264
  stage2_status.info(f"πŸ”„ {message}")
265
 
266
  try:
 
267
  result_path = process_video(input_path, output_path, update_progress)
268
 
269
  stage1_status.success("βœ… Stage 1: Segmentation complete!")
 
284
  st.video(result_path)
285
 
286
  except Exception as e:
287
+ print(f"\n{'='*60}", flush=True)
288
+ print(f"❌❌❌ PROCESSING FAILED ❌❌❌", flush=True)
289
+ print(f"Error: {str(e)}", flush=True)
290
+ print(f"{'='*60}\n", flush=True)
291
+ import traceback
292
+ traceback.print_exc()
293
+
294
  st.error(f"❌ Processing failed: {str(e)}")
295
+ st.error("Check the container logs for full details")
296
 
297
  finally:
298
  # Cleanup
 
304
  pass
305
 
306
  if __name__ == "__main__":
307
+ main()