Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -26,6 +26,7 @@ transform_image = transforms.Compose(
|
|
26 |
]
|
27 |
)
|
28 |
|
|
|
29 |
@spaces.GPU
|
30 |
def fn(vid, bg_type="Color", bg_image=None, color="#00FF00", fps=0):
|
31 |
# Load the video using moviepy
|
@@ -38,68 +39,37 @@ def fn(vid, bg_type="Color", bg_image=None, color="#00FF00", fps=0):
|
|
38 |
# Extract audio from the video
|
39 |
audio = video.audio
|
40 |
|
41 |
-
#
|
42 |
-
|
43 |
-
total_duration = video.duration
|
44 |
-
start_time = 0
|
45 |
-
|
46 |
-
progress = f'<div class="progress-container"><div class="progress-bar" style="--current: {start_time}; --total: {total_duration};"></div></div>'
|
47 |
|
|
|
48 |
processed_frames = []
|
49 |
-
yield gr.update(visible=True), gr.update(visible=False)
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
if bg_type == "Color":
|
59 |
-
processed_image = process(pil_image, color)
|
60 |
-
else:
|
61 |
-
processed_image = process(pil_image, bg_image)
|
62 |
-
processed_frames.append(np.array(processed_image))
|
63 |
-
yield processed_image, None, progress
|
64 |
-
|
65 |
-
# Save processed frames for the current chunk
|
66 |
-
temp_dir = "temp"
|
67 |
-
os.makedirs(temp_dir, exist_ok=True)
|
68 |
-
for i, frame in enumerate(processed_frames):
|
69 |
-
Image.fromarray(frame).save(os.path.join(temp_dir, f"frame_{start_time}_{i}.png"))
|
70 |
-
|
71 |
-
# Clear processed frames for the current chunk
|
72 |
-
processed_frames = []
|
73 |
-
progress = f'<div class="progress-container"><div class="progress-bar" style="--current: {start_time}; --total: {total_duration};"></div></div>'
|
74 |
-
|
75 |
-
yield None, None, progress
|
76 |
-
|
77 |
-
start_time += chunk_duration
|
78 |
-
|
79 |
-
# Load all saved frames
|
80 |
-
all_frames = []
|
81 |
-
for filename in sorted(os.listdir(temp_dir)):
|
82 |
-
if filename.startswith("frame_") and filename.endswith(".png"):
|
83 |
-
frame = np.array(Image.open(os.path.join(temp_dir, filename)))
|
84 |
-
all_frames.append(frame)
|
85 |
|
86 |
# Create a new video from the processed frames
|
87 |
-
processed_video = mp.ImageSequenceClip(
|
88 |
|
89 |
# Add the original audio back to the processed video
|
90 |
processed_video = processed_video.set_audio(audio)
|
91 |
|
92 |
# Save the processed video to a temporary file
|
93 |
-
|
|
|
|
|
|
|
94 |
processed_video.write_videofile(temp_filepath, codec="libx264")
|
95 |
|
96 |
-
|
97 |
-
for filename in os.listdir(temp_dir):
|
98 |
-
os.remove(os.path.join(temp_dir, filename))
|
99 |
-
|
100 |
-
yield gr.update(visible=False), gr.update(visible=True), progress
|
101 |
# Return the path to the temporary file
|
102 |
-
yield processed_image, temp_filepath
|
103 |
|
104 |
|
105 |
def process(image, bg):
|
@@ -124,12 +94,7 @@ def process(image, bg):
|
|
124 |
return image
|
125 |
|
126 |
|
127 |
-
|
128 |
-
.progress-container {width: 100%;height: 30px;background-color: #f0f0f0;border-radius: 15px;overflow: hidden;margin-bottom: 20px}
|
129 |
-
.progress-bar {height: 100%;background-color: #4f46e5;width: calc(var(--current) / var(--total) * 100%);transition: width 0.5s ease-in-out}
|
130 |
-
"""
|
131 |
-
|
132 |
-
with gr.Blocks(css=css, theme="ocean") as demo:
|
133 |
with gr.Row():
|
134 |
in_video = gr.Video(label="Input Video")
|
135 |
stream_image = gr.Image(label="Streaming Output", visible=False)
|
@@ -155,12 +120,11 @@ with gr.Blocks(css=css, theme="ocean") as demo:
|
|
155 |
|
156 |
bg_type.change(update_visibility, inputs=bg_type, outputs=[color_picker, bg_image])
|
157 |
|
158 |
-
progress_bar = gr.Markdown(elem_id="progress")
|
159 |
|
160 |
examples = gr.Examples(
|
161 |
[["rickroll-2sec.mp4", "Image", "images.webp"], ["rickroll-2sec.mp4", "Color", None]],
|
162 |
inputs=[in_video, bg_type, bg_image],
|
163 |
-
outputs=[stream_image, out_video
|
164 |
fn=fn,
|
165 |
cache_examples=True,
|
166 |
cache_mode="eager",
|
@@ -170,7 +134,7 @@ with gr.Blocks(css=css, theme="ocean") as demo:
|
|
170 |
submit_button.click(
|
171 |
fn,
|
172 |
inputs=[in_video, bg_type, bg_image, color_picker, fps_slider],
|
173 |
-
outputs=[stream_image, out_video
|
174 |
)
|
175 |
|
176 |
if __name__ == "__main__":
|
|
|
26 |
]
|
27 |
)
|
28 |
|
29 |
+
|
30 |
@spaces.GPU
|
31 |
def fn(vid, bg_type="Color", bg_image=None, color="#00FF00", fps=0):
|
32 |
# Load the video using moviepy
|
|
|
39 |
# Extract audio from the video
|
40 |
audio = video.audio
|
41 |
|
42 |
+
# Extract frames at the specified FPS
|
43 |
+
frames = video.iter_frames(fps=fps)
|
|
|
|
|
|
|
|
|
44 |
|
45 |
+
# Process each frame for background removal
|
46 |
processed_frames = []
|
47 |
+
yield gr.update(visible=True), gr.update(visible=False)
|
48 |
+
for frame in frames:
|
49 |
+
pil_image = Image.fromarray(frame)
|
50 |
+
if bg_type == "Color":
|
51 |
+
processed_image = process(pil_image, color)
|
52 |
+
else:
|
53 |
+
processed_image = process(pil_image, bg_image)
|
54 |
+
processed_frames.append(np.array(processed_image))
|
55 |
+
yield processed_image, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
# Create a new video from the processed frames
|
58 |
+
processed_video = mp.ImageSequenceClip(processed_frames, fps=fps)
|
59 |
|
60 |
# Add the original audio back to the processed video
|
61 |
processed_video = processed_video.set_audio(audio)
|
62 |
|
63 |
# Save the processed video to a temporary file
|
64 |
+
temp_dir = "temp"
|
65 |
+
os.makedirs(temp_dir, exist_ok=True)
|
66 |
+
unique_filename = str(uuid.uuid4()) + ".mp4"
|
67 |
+
temp_filepath = os.path.join(temp_dir, unique_filename)
|
68 |
processed_video.write_videofile(temp_filepath, codec="libx264")
|
69 |
|
70 |
+
yield gr.update(visible=False), gr.update(visible=True)
|
|
|
|
|
|
|
|
|
71 |
# Return the path to the temporary file
|
72 |
+
yield processed_image, temp_filepath
|
73 |
|
74 |
|
75 |
def process(image, bg):
|
|
|
94 |
return image
|
95 |
|
96 |
|
97 |
+
with gr.Blocks(theme=gr.themes.Ocean(primary_hue="blue")) as demo:
|
|
|
|
|
|
|
|
|
|
|
98 |
with gr.Row():
|
99 |
in_video = gr.Video(label="Input Video")
|
100 |
stream_image = gr.Image(label="Streaming Output", visible=False)
|
|
|
120 |
|
121 |
bg_type.change(update_visibility, inputs=bg_type, outputs=[color_picker, bg_image])
|
122 |
|
|
|
123 |
|
124 |
examples = gr.Examples(
|
125 |
[["rickroll-2sec.mp4", "Image", "images.webp"], ["rickroll-2sec.mp4", "Color", None]],
|
126 |
inputs=[in_video, bg_type, bg_image],
|
127 |
+
outputs=[stream_image, out_video],
|
128 |
fn=fn,
|
129 |
cache_examples=True,
|
130 |
cache_mode="eager",
|
|
|
134 |
submit_button.click(
|
135 |
fn,
|
136 |
inputs=[in_video, bg_type, bg_image, color_picker, fps_slider],
|
137 |
+
outputs=[stream_image, out_video],
|
138 |
)
|
139 |
|
140 |
if __name__ == "__main__":
|