Update app.py
Browse files
app.py
CHANGED
@@ -39,14 +39,20 @@ def fn(vid, fps, color):
|
|
39 |
frames = video.iter_frames(fps=fps)
|
40 |
|
41 |
# Process each frame for background removal
|
42 |
-
|
|
|
43 |
for frame in frames:
|
44 |
pil_image = Image.fromarray(frame)
|
45 |
-
processed_image = process(pil_image, color)
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
# Create a new video from the processed frames
|
49 |
-
processed_video = mp.ImageSequenceClip(
|
50 |
|
51 |
# Add the original audio back to the processed video
|
52 |
processed_video = processed_video.set_audio(audio)
|
@@ -58,8 +64,13 @@ def fn(vid, fps, color):
|
|
58 |
temp_filepath = os.path.join(temp_dir, unique_filename)
|
59 |
processed_video.write_videofile(temp_filepath, codec="libx264")
|
60 |
|
61 |
-
#
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
|
65 |
def process(image, color_hex):
|
@@ -73,7 +84,7 @@ def process(image, color_hex):
|
|
73 |
mask = pred_pil.resize(image_size)
|
74 |
|
75 |
# Convert hex color to RGB tuple
|
76 |
-
color_rgb = tuple(int(color_hex[i
|
77 |
|
78 |
# Create a background image with the chosen color
|
79 |
background = Image.new("RGBA", image_size, color_rgb + (255,))
|
@@ -81,7 +92,7 @@ def process(image, color_hex):
|
|
81 |
# Composite the image onto the background using the mask
|
82 |
image = Image.composite(image, background, mask)
|
83 |
|
84 |
-
return image
|
85 |
|
86 |
|
87 |
def process_file(f, color="#00FF00"):
|
@@ -93,15 +104,24 @@ def process_file(f, color="#00FF00"):
|
|
93 |
return name_path
|
94 |
|
95 |
|
|
|
|
|
|
|
|
|
|
|
96 |
with gr.Blocks() as demo:
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
submit_button = gr.Button("
|
|
|
|
|
|
|
|
|
102 |
|
103 |
submit_button.click(
|
104 |
-
|
105 |
)
|
106 |
|
107 |
if __name__ == "__main__":
|
|
|
39 |
frames = video.iter_frames(fps=fps)
|
40 |
|
41 |
# Process each frame for background removal
|
42 |
+
processed_frames_no_bg = []
|
43 |
+
processed_frames_changed_bg = []
|
44 |
for frame in frames:
|
45 |
pil_image = Image.fromarray(frame)
|
46 |
+
processed_image, mask = process(pil_image, color) # Get both processed image and mask
|
47 |
+
processed_frames_no_bg.append(np.array(processed_image)) # Save no-background frame
|
48 |
+
|
49 |
+
# Compose with background for changed background video
|
50 |
+
background = Image.new("RGBA", pil_image.size, color + (255,))
|
51 |
+
composed_image = Image.composite(pil_image, background, mask)
|
52 |
+
processed_frames_changed_bg.append(np.array(composed_image))
|
53 |
|
54 |
# Create a new video from the processed frames
|
55 |
+
processed_video = mp.ImageSequenceClip(processed_frames_changed_bg, fps=fps)
|
56 |
|
57 |
# Add the original audio back to the processed video
|
58 |
processed_video = processed_video.set_audio(audio)
|
|
|
64 |
temp_filepath = os.path.join(temp_dir, unique_filename)
|
65 |
processed_video.write_videofile(temp_filepath, codec="libx264")
|
66 |
|
67 |
+
# Create and save no-background video
|
68 |
+
processed_video_no_bg = mp.ImageSequenceClip(processed_frames_no_bg, fps=fps)
|
69 |
+
processed_video_no_bg = processed_video_no_bg.set_audio(audio)
|
70 |
+
temp_filepath_no_bg = os.path.join(temp_dir, str(uuid.uuid4()) + ".webm")
|
71 |
+
processed_video_no_bg.write_videofile(temp_filepath_no_bg, codec="libvpx")
|
72 |
+
|
73 |
+
return temp_filepath_no_bg, temp_filepath
|
74 |
|
75 |
|
76 |
def process(image, color_hex):
|
|
|
84 |
mask = pred_pil.resize(image_size)
|
85 |
|
86 |
# Convert hex color to RGB tuple
|
87 |
+
color_rgb = tuple(int(color_hex[i:i + 2], 16) for i in (1, 3, 5))
|
88 |
|
89 |
# Create a background image with the chosen color
|
90 |
background = Image.new("RGBA", image_size, color_rgb + (255,))
|
|
|
92 |
# Composite the image onto the background using the mask
|
93 |
image = Image.composite(image, background, mask)
|
94 |
|
95 |
+
return image, mask # Return both the processed image and the mask
|
96 |
|
97 |
|
98 |
def process_file(f, color="#00FF00"):
|
|
|
104 |
return name_path
|
105 |
|
106 |
|
107 |
+
def change_color(in_video, fps_slider, color_picker):
|
108 |
+
no_bg_video_path, changed_bg_video_path = fn(in_video, fps_slider, color_picker)
|
109 |
+
return no_bg_video_path, changed_bg_video_path
|
110 |
+
|
111 |
+
|
112 |
with gr.Blocks() as demo:
|
113 |
+
with gr.Row():
|
114 |
+
in_video = gr.Video(label="Input Video")
|
115 |
+
no_bg_video = gr.Video(label="No BG Video") # Added for no-background video
|
116 |
+
out_video = gr.Video(label="Output Video") # This will be the changed-background video
|
117 |
+
submit_button = gr.Button("Change Background")
|
118 |
+
with gr.Row():
|
119 |
+
fps_slider = gr.Slider(minimum=1, maximum=60, step=1, value=12, label="Output FPS")
|
120 |
+
color_picker = gr.ColorPicker(label="Background Color", value="#00FF00")
|
121 |
+
|
122 |
|
123 |
submit_button.click(
|
124 |
+
change_color, inputs=[in_video, fps_slider, color_picker], outputs=[no_bg_video, out_video]
|
125 |
)
|
126 |
|
127 |
if __name__ == "__main__":
|