KingNish commited on
Commit
7b27191
1 Parent(s): 9248f9f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -8
app.py CHANGED
@@ -64,6 +64,8 @@ cleanup_thread.start()
64
  @spaces.GPU
65
  def fn(vid, bg_type="Color", bg_image=None, bg_video=None, color="#00FF00", fps=0, video_handling="slow_down", fast_mode=False):
66
  try:
 
 
67
  # Load the video using moviepy
68
  video = mp.VideoFileClip(vid)
69
 
@@ -79,7 +81,7 @@ def fn(vid, bg_type="Color", bg_image=None, bg_video=None, color="#00FF00", fps=
79
 
80
  # Process each frame for background removal
81
  processed_frames = []
82
- yield gr.update(visible=True), gr.update(visible=False)
83
 
84
  if bg_type == "Video":
85
  background_video = mp.VideoFileClip(bg_video)
@@ -115,7 +117,8 @@ def fn(vid, bg_type="Color", bg_image=None, bg_video=None, color="#00FF00", fps=
115
  processed_image = pil_image # Default to original image if no background is selected
116
 
117
  processed_frames.append(np.array(processed_image))
118
- yield processed_image, None
 
119
 
120
  # Create a new video from the processed frames
121
  processed_video = mp.ImageSequenceClip(processed_frames, fps=fps)
@@ -130,14 +133,16 @@ def fn(vid, bg_type="Color", bg_image=None, bg_video=None, color="#00FF00", fps=
130
  temp_filepath = os.path.join(temp_dir, unique_filename)
131
  processed_video.write_videofile(temp_filepath, codec="libx264")
132
 
133
- yield gr.update(visible=False), gr.update(visible=True)
 
134
  # Return the path to the temporary file
135
- yield processed_image, temp_filepath
136
 
137
  except Exception as e:
138
  print(f"Error: {e}")
139
- yield gr.update(visible=False), gr.update(visible=True)
140
- yield None, f"Error processing video: {e}"
 
141
 
142
 
143
  def process(image, bg, fast_mode=False):
@@ -192,6 +197,7 @@ with gr.Blocks(theme=gr.themes.Ocean()) as demo:
192
  video_handling_radio = gr.Radio(["slow_down", "loop"], label="Video Handling", value="slow_down", interactive=True)
193
  fast_mode_checkbox = gr.Checkbox(label="Fast Mode (Use BiRefNet_lite)", value=False, interactive=True)
194
 
 
195
 
196
  def update_visibility(bg_type):
197
  if bg_type == "Color":
@@ -214,7 +220,7 @@ with gr.Blocks(theme=gr.themes.Ocean()) as demo:
214
  ["rickroll-2sec.mp4", "Color", None, None],
215
  ],
216
  inputs=[in_video, bg_type, bg_image, bg_video],
217
- outputs=[stream_image, out_video],
218
  fn=fn,
219
  cache_examples=True,
220
  cache_mode="eager",
@@ -224,7 +230,7 @@ with gr.Blocks(theme=gr.themes.Ocean()) as demo:
224
  submit_button.click(
225
  fn,
226
  inputs=[in_video, bg_type, bg_image, bg_video, color_picker, fps_slider, video_handling_radio, fast_mode_checkbox],
227
- outputs=[stream_image, out_video],
228
  )
229
 
230
  if __name__ == "__main__":
 
64
  @spaces.GPU
65
  def fn(vid, bg_type="Color", bg_image=None, bg_video=None, color="#00FF00", fps=0, video_handling="slow_down", fast_mode=False):
66
  try:
67
+ start_time = time.time() # Start the timer
68
+
69
  # Load the video using moviepy
70
  video = mp.VideoFileClip(vid)
71
 
 
81
 
82
  # Process each frame for background removal
83
  processed_frames = []
84
+ yield gr.update(visible=True), gr.update(visible=False), f"Processing started... Elapsed time: 0 seconds"
85
 
86
  if bg_type == "Video":
87
  background_video = mp.VideoFileClip(bg_video)
 
117
  processed_image = pil_image # Default to original image if no background is selected
118
 
119
  processed_frames.append(np.array(processed_image))
120
+ elapsed_time = time.time() - start_time
121
+ yield processed_image, None, f"Processing frame {i+1}... Elapsed time: {elapsed_time:.2f} seconds"
122
 
123
  # Create a new video from the processed frames
124
  processed_video = mp.ImageSequenceClip(processed_frames, fps=fps)
 
133
  temp_filepath = os.path.join(temp_dir, unique_filename)
134
  processed_video.write_videofile(temp_filepath, codec="libx264")
135
 
136
+ elapsed_time = time.time() - start_time
137
+ yield gr.update(visible=False), gr.update(visible=True), f"Processing complete! Elapsed time: {elapsed_time:.2f} seconds"
138
  # Return the path to the temporary file
139
+ yield processed_image, temp_filepath, f"Processing complete! Elapsed time: {elapsed_time:.2f} seconds"
140
 
141
  except Exception as e:
142
  print(f"Error: {e}")
143
+ elapsed_time = time.time() - start_time
144
+ yield gr.update(visible=False), gr.update(visible=True), f"Error processing video: {e}. Elapsed time: {elapsed_time:.2f} seconds"
145
+ yield None, f"Error processing video: {e}", f"Error processing video: {e}. Elapsed time: {elapsed_time:.2f} seconds"
146
 
147
 
148
  def process(image, bg, fast_mode=False):
 
197
  video_handling_radio = gr.Radio(["slow_down", "loop"], label="Video Handling", value="slow_down", interactive=True)
198
  fast_mode_checkbox = gr.Checkbox(label="Fast Mode (Use BiRefNet_lite)", value=False, interactive=True)
199
 
200
+ time_textbox = gr.Textbox(label="Time Elapsed", interactive=False) # Add time textbox
201
 
202
  def update_visibility(bg_type):
203
  if bg_type == "Color":
 
220
  ["rickroll-2sec.mp4", "Color", None, None],
221
  ],
222
  inputs=[in_video, bg_type, bg_image, bg_video],
223
+ outputs=[stream_image, out_video, time_textbox],
224
  fn=fn,
225
  cache_examples=True,
226
  cache_mode="eager",
 
230
  submit_button.click(
231
  fn,
232
  inputs=[in_video, bg_type, bg_image, bg_video, color_picker, fps_slider, video_handling_radio, fast_mode_checkbox],
233
+ outputs=[stream_image, out_video, time_textbox],
234
  )
235
 
236
  if __name__ == "__main__":