Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -39,37 +39,67 @@ def fn(vid, bg_type="Color", bg_image=None, color="#00FF00", fps=0):
|
|
39 |
# Extract audio from the video
|
40 |
audio = video.audio
|
41 |
|
42 |
-
#
|
43 |
-
|
|
|
|
|
44 |
|
45 |
-
# Process each frame for background removal
|
46 |
processed_frames = []
|
47 |
-
yield gr.update(visible=True), gr.update(visible=False)
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
# Create a new video from the processed frames
|
58 |
-
processed_video = mp.ImageSequenceClip(
|
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 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
71 |
# Return the path to the temporary file
|
72 |
-
yield processed_image, temp_filepath
|
73 |
|
74 |
|
75 |
def process(image, bg):
|
@@ -120,11 +150,12 @@ with gr.Blocks() as demo:
|
|
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,7 +165,7 @@ with gr.Blocks() as demo:
|
|
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__":
|
|
|
39 |
# Extract audio from the video
|
40 |
audio = video.audio
|
41 |
|
42 |
+
# Process video in chunks of 1 second
|
43 |
+
chunk_duration = 1 # seconds
|
44 |
+
total_duration = video.duration
|
45 |
+
start_time = 0
|
46 |
|
|
|
47 |
processed_frames = []
|
48 |
+
yield gr.update(visible=True), gr.update(visible=False), gr.update(value=0)
|
49 |
+
|
50 |
+
while start_time < total_duration:
|
51 |
+
end_time = min(start_time + chunk_duration, total_duration)
|
52 |
+
chunk = video.subclip(start_time, end_time)
|
53 |
+
chunk_frames = chunk.iter_frames(fps=fps)
|
54 |
+
|
55 |
+
for frame in chunk_frames:
|
56 |
+
pil_image = Image.fromarray(frame)
|
57 |
+
if bg_type == "Color":
|
58 |
+
processed_image = process(pil_image, color)
|
59 |
+
else:
|
60 |
+
processed_image = process(pil_image, bg_image)
|
61 |
+
processed_frames.append(np.array(processed_image))
|
62 |
+
yield processed_image, None, None
|
63 |
+
|
64 |
+
# Save processed frames for the current chunk
|
65 |
+
temp_dir = "temp"
|
66 |
+
os.makedirs(temp_dir, exist_ok=True)
|
67 |
+
for i, frame in enumerate(processed_frames):
|
68 |
+
Image.fromarray(frame).save(os.path.join(temp_dir, f"frame_{start_time}_{i}.png"))
|
69 |
+
|
70 |
+
# Clear processed frames for the current chunk
|
71 |
+
processed_frames = []
|
72 |
+
|
73 |
+
# Update progress bar
|
74 |
+
progress = start_time / total_duration
|
75 |
+
yield None, None, gr.update(value=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(all_frames, fps=fps)
|
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 |
+
temp_filepath = os.path.join(temp_dir, "processed_video.mp4")
|
|
|
|
|
|
|
94 |
processed_video.write_videofile(temp_filepath, codec="libx264")
|
95 |
|
96 |
+
# Clean up temporary files
|
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), gr.update(value=1)
|
101 |
# Return the path to the temporary file
|
102 |
+
yield processed_image, temp_filepath, None
|
103 |
|
104 |
|
105 |
def process(image, bg):
|
|
|
150 |
|
151 |
bg_type.change(update_visibility, inputs=bg_type, outputs=[color_picker, bg_image])
|
152 |
|
153 |
+
progress_bar = gr.ProgressBar(label="Processing Video")
|
154 |
|
155 |
examples = gr.Examples(
|
156 |
[["rickroll-2sec.mp4", "Image", "images.webp"], ["rickroll-2sec.mp4", "Color", None]],
|
157 |
inputs=[in_video, bg_type, bg_image],
|
158 |
+
outputs=[stream_image, out_video, progress_bar],
|
159 |
fn=fn,
|
160 |
cache_examples=True,
|
161 |
cache_mode="eager",
|
|
|
165 |
submit_button.click(
|
166 |
fn,
|
167 |
inputs=[in_video, bg_type, bg_image, color_picker, fps_slider],
|
168 |
+
outputs=[stream_image, out_video, progress_bar],
|
169 |
)
|
170 |
|
171 |
if __name__ == "__main__":
|