John2J commited on
Commit
6ed52a0
·
verified ·
1 Parent(s): c83b94d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -5
app.py CHANGED
@@ -160,9 +160,10 @@ def clear_clicks(video_state):
160
  def load_uploaded_mask_video(video_path, mask_video_path, n_frames, video_state):
161
  """Load a user-supplied mask video (white = object) and use it directly,
162
  skipping click segmentation and SAM2 tracking."""
163
- src = video_state["video_path"] or video_path
164
- if mask_video_path is None or src is None:
165
- return video_state, None
 
166
 
167
  # --- load source frames (same logic as track_video) ---
168
  vr = VideoReader(src, ctx=cpu(0))
@@ -203,7 +204,13 @@ def load_uploaded_mask_video(video_path, mask_video_path, n_frames, video_state)
203
  return video_state, Image.fromarray(preview)
204
 
205
  def get_source_info(video_state):
206
- vr = VideoReader(video_state["video_path"], ctx=cpu(0))
 
 
 
 
 
 
207
  fps = float(vr.get_avg_fps())
208
  h, w = vr[0].shape[:2]
209
  del vr
@@ -234,6 +241,8 @@ MAX_PROC_SIDE = None # None = native resolution; set e.g. 1280 if you hit OOM
234
 
235
  @spaces.GPU(duration=300) # 1080p is heavy — give it time
236
  def inference_and_return_video(dilation_iterations, num_inference_steps, video_state):
 
 
237
  if video_state["origin_images"] is None or video_state["masks"] is None:
238
  return None
239
 
@@ -484,7 +493,7 @@ with gr.Blocks() as demo:
484
  remove_video = gr.Video(label="Remove Results", elem_id="my-video")
485
  remove_btn.click(
486
  inference_and_return_video,
487
- inputs=[dilation_slider, inference_steps_slider, video_state],
488
  outputs=remove_video
489
  )
490
 
 
160
  def load_uploaded_mask_video(video_path, mask_video_path, n_frames, video_state):
161
  """Load a user-supplied mask video (white = object) and use it directly,
162
  skipping click segmentation and SAM2 tracking."""
163
+ src = video_state.get("video_path") or video_path
164
+ if src is None or mask_video_path is None:
165
+ raise gr.Error("Upload both the source video and the mask video first.")
166
+ video_state["video_path"] = src # <-- persist for the Remove step
167
 
168
  # --- load source frames (same logic as track_video) ---
169
  vr = VideoReader(src, ctx=cpu(0))
 
204
  return video_state, Image.fromarray(preview)
205
 
206
  def get_source_info(video_state):
207
+ path = video_state.get("video_path")
208
+ if path is None:
209
+ raise gr.Error(
210
+ "No source video found. Upload a video, then click "
211
+ "'Extract First Frame' or 'Use Uploaded Mask' before Remove."
212
+ )
213
+ vr = VideoReader(path, ctx=cpu(0))
214
  fps = float(vr.get_avg_fps())
215
  h, w = vr[0].shape[:2]
216
  del vr
 
241
 
242
  @spaces.GPU(duration=300) # 1080p is heavy — give it time
243
  def inference_and_return_video(dilation_iterations, num_inference_steps, video_state):
244
+ if video_state.get("video_path") is None:
245
+ video_state["video_path"] = video_path
246
  if video_state["origin_images"] is None or video_state["masks"] is None:
247
  return None
248
 
 
493
  remove_video = gr.Video(label="Remove Results", elem_id="my-video")
494
  remove_btn.click(
495
  inference_and_return_video,
496
+ inputs=[dilation_slider, inference_steps_slider, video_input, video_state],
497
  outputs=remove_video
498
  )
499