Update app.py
Browse files
app.py
CHANGED
|
@@ -50,11 +50,12 @@ def process_frame(frame, bg_type, bg, fast_mode, bg_frame_index, background_fram
|
|
| 50 |
return frame, bg_frame_index
|
| 51 |
|
| 52 |
@spaces.GPU
|
| 53 |
-
def fn(vid, bg_type="Color", bg_image=None, bg_video=None, color="#00FF00", video_handling="slow_down"):
|
| 54 |
try:
|
| 55 |
start_time = time.time() # Start the timer
|
| 56 |
video = VideoFileClip(vid)
|
| 57 |
-
|
|
|
|
| 58 |
|
| 59 |
audio = video.audio
|
| 60 |
frames = list(video.iter_frames(fps=fps))
|
|
@@ -75,9 +76,9 @@ def fn(vid, bg_type="Color", bg_image=None, bg_video=None, color="#00FF00", vide
|
|
| 75 |
|
| 76 |
bg_frame_index = 0 # Initialize background frame index
|
| 77 |
|
| 78 |
-
with ThreadPoolExecutor(max_workers=
|
| 79 |
# Pass bg_frame_index as part of the function arguments
|
| 80 |
-
futures = [executor.submit(process_frame, frames[i], bg_type, bg_image,
|
| 81 |
for i, future in enumerate(futures):
|
| 82 |
result, _ = future.result() # No need to update bg_frame_index here
|
| 83 |
processed_frames.append(result)
|
|
@@ -101,7 +102,7 @@ def fn(vid, bg_type="Color", bg_image=None, bg_video=None, color="#00FF00", vide
|
|
| 101 |
yield gr.update(visible=False), gr.update(visible=True)
|
| 102 |
yield None, f"Error processing video: {e}"
|
| 103 |
|
| 104 |
-
def process(image, bg, fast_mode=
|
| 105 |
image_size = image.size
|
| 106 |
input_images = transform_image(image).unsqueeze(0).to(device)
|
| 107 |
model = birefnet_lite if fast_mode else birefnet
|
|
@@ -123,37 +124,72 @@ def process(image, bg, fast_mode=True): # Default to fast_mode=True
|
|
| 123 |
image = Image.composite(image, background, mask)
|
| 124 |
return image
|
| 125 |
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
with gr.Row():
|
| 129 |
in_video = gr.Video(label="Input Video", interactive=True)
|
| 130 |
stream_image = gr.Image(label="Streaming Output", visible=False)
|
| 131 |
out_video = gr.Video(label="Final Output Video")
|
| 132 |
|
| 133 |
-
# Settings panels aligned below input video
|
| 134 |
-
with gr.
|
| 135 |
-
|
| 136 |
-
|
| 137 |
bg_type = gr.Radio(["Color", "Image", "Video"], label="Background Type", value="Color", interactive=True)
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
)
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
def update_visibility(bg_type):
|
| 159 |
if bg_type == "Color":
|
|
|
|
| 50 |
return frame, bg_frame_index
|
| 51 |
|
| 52 |
@spaces.GPU
|
| 53 |
+
def fn(vid, bg_type="Color", bg_image=None, bg_video=None, color="#00FF00", fps=0, video_handling="slow_down", fast_mode=True, max_workers=10):
|
| 54 |
try:
|
| 55 |
start_time = time.time() # Start the timer
|
| 56 |
video = VideoFileClip(vid)
|
| 57 |
+
if fps == 0:
|
| 58 |
+
fps = video.fps
|
| 59 |
|
| 60 |
audio = video.audio
|
| 61 |
frames = list(video.iter_frames(fps=fps))
|
|
|
|
| 76 |
|
| 77 |
bg_frame_index = 0 # Initialize background frame index
|
| 78 |
|
| 79 |
+
with ThreadPoolExecutor(max_workers=max_workers) as executor:
|
| 80 |
# Pass bg_frame_index as part of the function arguments
|
| 81 |
+
futures = [executor.submit(process_frame, frames[i], bg_type, bg_image, fast_mode, bg_frame_index + i, background_frames, color) for i in range(len(frames))]
|
| 82 |
for i, future in enumerate(futures):
|
| 83 |
result, _ = future.result() # No need to update bg_frame_index here
|
| 84 |
processed_frames.append(result)
|
|
|
|
| 102 |
yield gr.update(visible=False), gr.update(visible=True)
|
| 103 |
yield None, f"Error processing video: {e}"
|
| 104 |
|
| 105 |
+
def process(image, bg, fast_mode=False):
|
| 106 |
image_size = image.size
|
| 107 |
input_images = transform_image(image).unsqueeze(0).to(device)
|
| 108 |
model = birefnet_lite if fast_mode else birefnet
|
|
|
|
| 124 |
image = Image.composite(image, background, mask)
|
| 125 |
return image
|
| 126 |
|
| 127 |
+
# Create custom dark theme
|
| 128 |
+
dark_theme = gr.themes.Soft(
|
| 129 |
+
primary_hue="purple",
|
| 130 |
+
secondary_hue="blue",
|
| 131 |
+
neutral_hue="slate",
|
| 132 |
+
).set(
|
| 133 |
+
body_background_fill_dark="#0f0f23",
|
| 134 |
+
block_background_fill_dark="#1a1b2e",
|
| 135 |
+
block_border_color_dark="#16213e",
|
| 136 |
+
input_background_fill_dark="#16213e",
|
| 137 |
+
button_primary_background_fill_dark="#6366f1",
|
| 138 |
+
button_primary_background_fill_hover_dark="#4f46e5",
|
| 139 |
+
button_secondary_background_fill_dark="#374151",
|
| 140 |
+
button_secondary_background_fill_hover_dark="#4b5563"
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
with gr.Blocks(theme=dark_theme) as demo:
|
| 144 |
|
| 145 |
with gr.Row():
|
| 146 |
in_video = gr.Video(label="Input Video", interactive=True)
|
| 147 |
stream_image = gr.Image(label="Streaming Output", visible=False)
|
| 148 |
out_video = gr.Video(label="Final Output Video")
|
| 149 |
|
| 150 |
+
# Settings panels aligned below input video
|
| 151 |
+
with gr.Column():
|
| 152 |
+
# Row 1: Background Type and FPS (two smaller panels)
|
| 153 |
+
with gr.Row():
|
| 154 |
bg_type = gr.Radio(["Color", "Image", "Video"], label="Background Type", value="Color", interactive=True)
|
| 155 |
+
fps_slider = gr.Slider(
|
| 156 |
+
minimum=0,
|
| 157 |
+
maximum=60,
|
| 158 |
+
step=1,
|
| 159 |
+
value=0,
|
| 160 |
+
label="Output FPS (0 = original fps)",
|
| 161 |
+
interactive=True
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
# Row 2: Dynamic background options (full width)
|
| 165 |
+
color_picker = gr.ColorPicker(label="Background Color", value="#00FF00", visible=True, interactive=True)
|
| 166 |
+
bg_image = gr.Image(label="Background Image", type="filepath", visible=False, interactive=True)
|
| 167 |
+
bg_video = gr.Video(label="Background Video", visible=False, interactive=True)
|
| 168 |
+
|
| 169 |
+
# Row 3: Video handling options (only visible when Video is selected)
|
| 170 |
+
with gr.Row(visible=False) as video_handling_options:
|
| 171 |
+
video_handling_radio = gr.Radio(["slow_down", "loop"], label="Video Handling", value="slow_down", interactive=True)
|
| 172 |
+
|
| 173 |
+
# Row 4: Processing options (two smaller panels)
|
| 174 |
+
with gr.Row():
|
| 175 |
+
fast_mode_checkbox = gr.Checkbox(label="Fast Mode (Use BiRefNet_lite)", value=True, interactive=True)
|
| 176 |
+
max_workers_slider = gr.Slider(
|
| 177 |
+
minimum=1,
|
| 178 |
+
maximum=32,
|
| 179 |
+
step=1,
|
| 180 |
+
value=10,
|
| 181 |
+
label="Max Workers",
|
| 182 |
+
info="Parallel processing threads",
|
| 183 |
+
interactive=True
|
| 184 |
)
|
| 185 |
+
|
| 186 |
+
# Styled button with rocket emoji and rounded corners
|
| 187 |
+
submit_button = gr.Button(
|
| 188 |
+
"🚀 Change Background",
|
| 189 |
+
interactive=True,
|
| 190 |
+
variant="primary",
|
| 191 |
+
size="lg"
|
| 192 |
+
)
|
| 193 |
|
| 194 |
def update_visibility(bg_type):
|
| 195 |
if bg_type == "Color":
|