Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -13,6 +13,7 @@ import tempfile
|
|
13 |
import uuid
|
14 |
import time
|
15 |
import threading
|
|
|
16 |
|
17 |
torch.set_float32_matmul_precision("medium")
|
18 |
|
@@ -61,10 +62,16 @@ cleanup_thread = threading.Thread(target=cleanup_temp_files, daemon=True)
|
|
61 |
cleanup_thread.start()
|
62 |
|
63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
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=
|
66 |
try:
|
67 |
-
start_time = time.time()
|
68 |
|
69 |
# Load the video using moviepy
|
70 |
video = mp.VideoFileClip(vid)
|
@@ -96,29 +103,44 @@ def fn(vid, bg_type="Color", bg_image=None, bg_video=None, color="#00FF00", fps=
|
|
96 |
|
97 |
bg_frame_index = 0 # Initialize background frame index
|
98 |
|
|
|
|
|
|
|
|
|
99 |
for i, frame in enumerate(frames):
|
100 |
pil_image = Image.fromarray(frame)
|
101 |
if bg_type == "Color":
|
102 |
-
|
103 |
elif bg_type == "Image":
|
104 |
-
|
105 |
elif bg_type == "Video":
|
106 |
if video_handling == "slow_down":
|
107 |
background_frame = background_frames[bg_frame_index % len(background_frames)]
|
108 |
bg_frame_index += 1
|
109 |
background_image = Image.fromarray(background_frame)
|
110 |
-
processed_image = process(pil_image, background_image, fast_mode)
|
111 |
else: # video_handling == "loop"
|
112 |
background_frame = background_frames[bg_frame_index % len(background_frames)]
|
113 |
bg_frame_index += 1
|
114 |
background_image = Image.fromarray(background_frame)
|
115 |
-
processed_image = process(pil_image, background_image, fast_mode)
|
116 |
else:
|
117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
|
119 |
-
processed_frames.append(np.array(processed_image))
|
120 |
elapsed_time = time.time() - start_time
|
121 |
-
|
|
|
122 |
|
123 |
# Create a new video from the processed frames
|
124 |
processed_video = mp.ImageSequenceClip(processed_frames, fps=fps)
|
@@ -136,7 +158,7 @@ def fn(vid, bg_type="Color", bg_image=None, bg_video=None, color="#00FF00", fps=
|
|
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
|
140 |
|
141 |
except Exception as e:
|
142 |
print(f"Error: {e}")
|
@@ -195,7 +217,7 @@ with gr.Blocks(theme=gr.themes.Ocean()) as demo:
|
|
195 |
bg_video = gr.Video(label="Background Video", visible=False, interactive=True)
|
196 |
with gr.Column(visible=False) as video_handling_options:
|
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=
|
199 |
|
200 |
time_textbox = gr.Textbox(label="Time Elapsed", interactive=False) # Add time textbox
|
201 |
|
|
|
13 |
import uuid
|
14 |
import time
|
15 |
import threading
|
16 |
+
from queue import Queue
|
17 |
|
18 |
torch.set_float32_matmul_precision("medium")
|
19 |
|
|
|
62 |
cleanup_thread.start()
|
63 |
|
64 |
|
65 |
+
# Function to process frames in a separate thread
|
66 |
+
def process_frame(image, bg, fast_mode, result_queue):
|
67 |
+
processed_image = process(image, bg, fast_mode)
|
68 |
+
result_queue.put(processed_image)
|
69 |
+
|
70 |
+
|
71 |
@spaces.GPU
|
72 |
+
def fn(vid, bg_type="Color", bg_image=None, bg_video=None, color="#00FF00", fps=0, video_handling="slow_down", fast_mode=True):
|
73 |
try:
|
74 |
+
start_time = time.time()
|
75 |
|
76 |
# Load the video using moviepy
|
77 |
video = mp.VideoFileClip(vid)
|
|
|
103 |
|
104 |
bg_frame_index = 0 # Initialize background frame index
|
105 |
|
106 |
+
threads = []
|
107 |
+
result_queue = Queue()
|
108 |
+
frame_batch_size = 4 # Process 4 frames at a time
|
109 |
+
|
110 |
for i, frame in enumerate(frames):
|
111 |
pil_image = Image.fromarray(frame)
|
112 |
if bg_type == "Color":
|
113 |
+
background_image = color # Use color directly for threads
|
114 |
elif bg_type == "Image":
|
115 |
+
background_image = bg_image
|
116 |
elif bg_type == "Video":
|
117 |
if video_handling == "slow_down":
|
118 |
background_frame = background_frames[bg_frame_index % len(background_frames)]
|
119 |
bg_frame_index += 1
|
120 |
background_image = Image.fromarray(background_frame)
|
|
|
121 |
else: # video_handling == "loop"
|
122 |
background_frame = background_frames[bg_frame_index % len(background_frames)]
|
123 |
bg_frame_index += 1
|
124 |
background_image = Image.fromarray(background_frame)
|
|
|
125 |
else:
|
126 |
+
background_image = None # Default to no background
|
127 |
+
|
128 |
+
# Start a new thread to process the frame
|
129 |
+
thread = threading.Thread(target=process_frame, args=(pil_image, background_image, fast_mode, result_queue))
|
130 |
+
threads.append(thread)
|
131 |
+
thread.start()
|
132 |
+
|
133 |
+
# If we have enough threads running or it's the last frame, wait for results
|
134 |
+
if len(threads) == frame_batch_size or i == len(list(frames)) - 1:
|
135 |
+
for thread in threads:
|
136 |
+
thread.join()
|
137 |
+
while not result_queue.empty():
|
138 |
+
processed_frames.append(np.array(result_queue.get()))
|
139 |
+
threads = [] # Reset the threads list
|
140 |
|
|
|
141 |
elapsed_time = time.time() - start_time
|
142 |
+
# Yield the first processed image from the current batch
|
143 |
+
yield processed_frames[-len(threads) if threads else -frame_batch_size], None, f"Processing frame {i+1}... Elapsed time: {elapsed_time:.2f} seconds"
|
144 |
|
145 |
# Create a new video from the processed frames
|
146 |
processed_video = mp.ImageSequenceClip(processed_frames, fps=fps)
|
|
|
158 |
elapsed_time = time.time() - start_time
|
159 |
yield gr.update(visible=False), gr.update(visible=True), f"Processing complete! Elapsed time: {elapsed_time:.2f} seconds"
|
160 |
# Return the path to the temporary file
|
161 |
+
yield processed_frames[-1], temp_filepath, f"Processing complete! Elapsed time: {elapsed_time:.2f} seconds"
|
162 |
|
163 |
except Exception as e:
|
164 |
print(f"Error: {e}")
|
|
|
217 |
bg_video = gr.Video(label="Background Video", visible=False, interactive=True)
|
218 |
with gr.Column(visible=False) as video_handling_options:
|
219 |
video_handling_radio = gr.Radio(["slow_down", "loop"], label="Video Handling", value="slow_down", interactive=True)
|
220 |
+
fast_mode_checkbox = gr.Checkbox(label="Fast Mode (Use BiRefNet_lite)", value=True, interactive=True)
|
221 |
|
222 |
time_textbox = gr.Textbox(label="Time Elapsed", interactive=False) # Add time textbox
|
223 |
|