Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -28,13 +28,13 @@ transform_image = transforms.Compose(
|
|
28 |
|
29 |
|
30 |
@spaces.GPU
|
31 |
-
def fn(vid, fps=0, color="#00FF00"):
|
32 |
# Load the video using moviepy
|
33 |
video = mp.VideoFileClip(vid)
|
34 |
|
35 |
# Load original fps if fps value is equal to 0
|
36 |
if fps == 0:
|
37 |
-
|
38 |
|
39 |
# Extract audio from the video
|
40 |
audio = video.audio
|
@@ -47,7 +47,10 @@ def fn(vid, fps=0, color="#00FF00"):
|
|
47 |
yield gr.update(visible=True), gr.update(visible=False)
|
48 |
for frame in frames:
|
49 |
pil_image = Image.fromarray(frame)
|
50 |
-
|
|
|
|
|
|
|
51 |
processed_frames.append(np.array(processed_image))
|
52 |
yield processed_image, None
|
53 |
|
@@ -63,13 +66,13 @@ def fn(vid, fps=0, color="#00FF00"):
|
|
63 |
unique_filename = str(uuid.uuid4()) + ".mp4"
|
64 |
temp_filepath = os.path.join(temp_dir, unique_filename)
|
65 |
processed_video.write_videofile(temp_filepath, codec="libx264")
|
66 |
-
|
67 |
yield gr.update(visible=False), gr.update(visible=True)
|
68 |
# Return the path to the temporary file
|
69 |
yield processed_image, temp_filepath
|
70 |
|
71 |
|
72 |
-
def process(image,
|
73 |
image_size = image.size
|
74 |
input_images = transform_image(image).unsqueeze(0).to("cuda")
|
75 |
# Prediction
|
@@ -79,11 +82,11 @@ def process(image, color_hex):
|
|
79 |
pred_pil = transforms.ToPILImage()(pred)
|
80 |
mask = pred_pil.resize(image_size)
|
81 |
|
82 |
-
#
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
|
88 |
# Composite the image onto the background using the mask
|
89 |
image = Image.composite(image, background, mask)
|
@@ -95,17 +98,43 @@ with gr.Blocks() as demo:
|
|
95 |
with gr.Row():
|
96 |
in_video = gr.Video(label="Input Video")
|
97 |
stream_image = gr.Image(label="Streaming Output", visible=False)
|
98 |
-
out_video = gr.Video(label="Final Output Video")
|
99 |
submit_button = gr.Button("Change Background")
|
100 |
with gr.Row():
|
101 |
-
fps_slider = gr.Slider(
|
102 |
-
|
103 |
-
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
submit_button.click(
|
107 |
-
fn,
|
|
|
|
|
108 |
)
|
109 |
-
|
110 |
if __name__ == "__main__":
|
111 |
demo.launch(show_error=True)
|
|
|
28 |
|
29 |
|
30 |
@spaces.GPU
|
31 |
+
def fn(vid, fps=0, bg_type="Color", color="#00FF00", bg_image=None):
|
32 |
# Load the video using moviepy
|
33 |
video = mp.VideoFileClip(vid)
|
34 |
|
35 |
# Load original fps if fps value is equal to 0
|
36 |
if fps == 0:
|
37 |
+
fps = video.fps
|
38 |
|
39 |
# Extract audio from the video
|
40 |
audio = video.audio
|
|
|
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 |
|
|
|
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):
|
76 |
image_size = image.size
|
77 |
input_images = transform_image(image).unsqueeze(0).to("cuda")
|
78 |
# Prediction
|
|
|
82 |
pred_pil = transforms.ToPILImage()(pred)
|
83 |
mask = pred_pil.resize(image_size)
|
84 |
|
85 |
+
if isinstance(bg, str): # If bg is a file path (image)
|
86 |
+
background = Image.open(bg).convert("RGBA").resize(image_size)
|
87 |
+
else: # If bg is a color hex code
|
88 |
+
color_rgb = tuple(int(bg[i : i + 2], 16) for i in (1, 3, 5))
|
89 |
+
background = Image.new("RGBA", image_size, color_rgb + (255,))
|
90 |
|
91 |
# Composite the image onto the background using the mask
|
92 |
image = Image.composite(image, background, mask)
|
|
|
98 |
with gr.Row():
|
99 |
in_video = gr.Video(label="Input Video")
|
100 |
stream_image = gr.Image(label="Streaming Output", visible=False)
|
101 |
+
out_video = gr.Video(label="Final Output Video")
|
102 |
submit_button = gr.Button("Change Background")
|
103 |
with gr.Row():
|
104 |
+
fps_slider = gr.Slider(
|
105 |
+
minimum=0,
|
106 |
+
maximum=60,
|
107 |
+
step=1,
|
108 |
+
value=0,
|
109 |
+
label="Output FPS (0 will inherit the original fps value)",
|
110 |
+
)
|
111 |
+
bg_type = gr.Radio(["Color", "Image"], label="Background Type", value="Color")
|
112 |
+
color_picker = gr.ColorPicker(label="Background Color", value="#00FF00", visible=True)
|
113 |
+
bg_image = gr.Image(label="Background Image", type="filepath", visible=False)
|
114 |
+
|
115 |
+
def update_visibility(bg_type):
|
116 |
+
if bg_type == "Color":
|
117 |
+
return gr.update(visible=True), gr.update(visible=False)
|
118 |
+
else:
|
119 |
+
return gr.update(visible=False), gr.update(visible=True)
|
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"],
|
126 |
+
inputs=in_video,
|
127 |
+
outputs=[stream_image, out_video],
|
128 |
+
fn=fn,
|
129 |
+
cache_examples=True,
|
130 |
+
cache_mode="eager",
|
131 |
+
)
|
132 |
|
133 |
submit_button.click(
|
134 |
+
fn,
|
135 |
+
inputs=[in_video, fps_slider, bg_type, color_picker, bg_image],
|
136 |
+
outputs=[stream_image, out_video],
|
137 |
)
|
138 |
+
|
139 |
if __name__ == "__main__":
|
140 |
demo.launch(show_error=True)
|