KingNish commited on
Commit
293e082
1 Parent(s): 10b6ea4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -19
app.py CHANGED
@@ -39,16 +39,14 @@ def fn(vid, fps, color):
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)
47
- processed_frames_no_bg.append(np.array(mask))
48
- processed_frames_changed_bg.append(np.array(processed_image))
49
 
50
  # Create a new video from the processed frames
51
- processed_video = mp.ImageSequenceClip(processed_frames_changed_bg, fps=fps)
52
 
53
  # Add the original audio back to the processed video
54
  processed_video = processed_video.set_audio(audio)
@@ -60,13 +58,8 @@ def fn(vid, fps, color):
60
  temp_filepath = os.path.join(temp_dir, unique_filename)
61
  processed_video.write_videofile(temp_filepath, codec="libx264")
62
 
63
- # Create and save no-background video
64
- processed_video_no_bg = mp.ImageSequenceClip(processed_frames_no_bg, fps=fps)
65
- processed_video_no_bg = processed_video_no_bg.set_audio(audio)
66
- temp_filepath_no_bg = os.path.join(temp_dir, str(uuid.uuid4()) + ".webm")
67
- processed_video_no_bg.write_videofile(temp_filepath_no_bg, codec="libvpx")
68
-
69
- return temp_filepath_no_bg, temp_filepath
70
 
71
 
72
  def process(image, color_hex):
@@ -80,7 +73,7 @@ def process(image, color_hex):
80
  mask = pred_pil.resize(image_size)
81
 
82
  # Convert hex color to RGB tuple
83
- color_rgb = tuple(int(color_hex[i:i + 2], 16) for i in (1, 3, 5))
84
 
85
  # Create a background image with the chosen color
86
  background = Image.new("RGBA", image_size, color_rgb + (255,))
@@ -88,22 +81,29 @@ def process(image, color_hex):
88
  # Composite the image onto the background using the mask
89
  image = Image.composite(image, background, mask)
90
 
91
- return image, mask # Return both the processed image and the mask
 
 
 
 
 
 
 
 
 
92
 
93
 
94
  with gr.Blocks() as demo:
95
  with gr.Row():
96
  in_video = gr.Video(label="Input Video")
97
- no_bg_video = gr.Video(label="No BG Video") # Added for no-background video
98
- out_video = gr.Video(label="Output Video") # This will be the changed-background video
99
  submit_button = gr.Button("Change Background")
100
  with gr.Row():
101
  fps_slider = gr.Slider(minimum=1, maximum=60, step=1, value=12, label="Output FPS")
102
  color_picker = gr.ColorPicker(label="Background Color", value="#00FF00")
103
 
104
-
105
  submit_button.click(
106
- fn, inputs=[in_video, fps_slider, color_picker], outputs=[no_bg_video, out_video]
107
  )
108
 
109
  if __name__ == "__main__":
 
39
  frames = video.iter_frames(fps=fps)
40
 
41
  # Process each frame for background removal
42
+ processed_frames = []
 
43
  for frame in frames:
44
  pil_image = Image.fromarray(frame)
45
+ processed_image = process(pil_image, color)
46
+ processed_frames.append(np.array(processed_image))
 
47
 
48
  # Create a new video from the processed frames
49
+ processed_video = mp.ImageSequenceClip(processed_frames, fps=fps)
50
 
51
  # Add the original audio back to the processed video
52
  processed_video = processed_video.set_audio(audio)
 
58
  temp_filepath = os.path.join(temp_dir, unique_filename)
59
  processed_video.write_videofile(temp_filepath, codec="libx264")
60
 
61
+ # Return the path to the temporary file
62
+ return temp_filepath
 
 
 
 
 
63
 
64
 
65
  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 : i + 2], 16) for i in (1, 3, 5))
77
 
78
  # Create a background image with the chosen color
79
  background = Image.new("RGBA", image_size, color_rgb + (255,))
 
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"):
88
+ name_path = f.rsplit(".", 1)[0] + ".png"
89
+ im = load_img(f, output_type="pil")
90
+ im = im.convert("RGB")
91
+ transparent = process(im, color)
92
+ transparent.save(name_path)
93
+ return name_path
94
 
95
 
96
  with gr.Blocks() as demo:
97
  with gr.Row():
98
  in_video = gr.Video(label="Input Video")
99
+ out_video = gr.Video(label="Output Video")
 
100
  submit_button = gr.Button("Change Background")
101
  with gr.Row():
102
  fps_slider = gr.Slider(minimum=1, maximum=60, step=1, value=12, label="Output FPS")
103
  color_picker = gr.ColorPicker(label="Background Color", value="#00FF00")
104
 
 
105
  submit_button.click(
106
+ fn, inputs=[in_video, fps_slider, color_picker], outputs=out_video
107
  )
108
 
109
  if __name__ == "__main__":