KingNish commited on
Commit
0bc476b
1 Parent(s): 06f0e60

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -41
app.py CHANGED
@@ -26,24 +26,16 @@ transform_image = transforms.Compose(
26
  ]
27
  )
28
 
 
29
 
30
  @spaces.GPU
31
  def fn(vid, bg_type="Color", bg_image=None, bg_video=None, color="#00FF00", fps=0, video_handling="slow_down"):
32
  try:
33
- # Load the video using moviepy
34
  video = mp.VideoFileClip(vid)
35
-
36
- # Load original fps if fps value is equal to 0
37
  if fps == 0:
38
  fps = video.fps
39
-
40
- # Extract audio from the video
41
  audio = video.audio
42
-
43
- # Extract frames at the specified FPS
44
  frames = video.iter_frames(fps=fps)
45
-
46
- # Process each frame for background removal
47
  processed_frames = []
48
  yield gr.update(visible=True), gr.update(visible=False)
49
 
@@ -52,52 +44,60 @@ def fn(vid, bg_type="Color", bg_image=None, bg_video=None, color="#00FF00", fps=
52
  if background_video.duration < video.duration:
53
  if video_handling == "slow_down":
54
  background_video = background_video.fx(mp.vfx.speedx, factor=video.duration / background_video.duration)
55
- else: # video_handling == "loop"
56
  background_video = mp.concatenate_videoclips([background_video] * int(video.duration / background_video.duration + 1))
57
- background_frames = list(background_video.iter_frames(fps=fps)) # Convert to list
58
  else:
59
  background_frames = None
60
 
61
- bg_frame_index = 0 # Initialize background frame index
 
62
 
63
  for i, frame in enumerate(frames):
64
- pil_image = Image.fromarray(frame)
65
- if bg_type == "Color":
66
- processed_image = process(pil_image, color)
67
- elif bg_type == "Image":
68
- processed_image = process(pil_image, bg_image)
69
- elif bg_type == "Video":
70
- if video_handling == "slow_down":
71
- background_frame = background_frames[bg_frame_index % len(background_frames)]
72
- bg_frame_index += 1
73
- background_image = Image.fromarray(background_frame)
74
- processed_image = process(pil_image, background_image)
75
- else: # video_handling == "loop"
76
- background_frame = background_frames[bg_frame_index % len(background_frames)]
77
- bg_frame_index += 1
78
- background_image = Image.fromarray(background_frame)
79
- processed_image = process(pil_image, background_image)
80
- else:
81
- processed_image = pil_image # Default to original image if no background is selected
82
-
83
- processed_frames.append(np.array(processed_image))
84
- yield processed_image, None
85
-
86
- # Create a new video from the processed frames
87
- processed_video = mp.ImageSequenceClip(processed_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_dir = "temp"
94
  os.makedirs(temp_dir, exist_ok=True)
95
  unique_filename = str(uuid.uuid4()) + ".mp4"
96
  temp_filepath = os.path.join(temp_dir, unique_filename)
97
- processed_video.write_videofile(temp_filepath, codec="libx264")
 
98
 
99
  yield gr.update(visible=False), gr.update(visible=True)
100
- # Return the path to the temporary file
101
  yield processed_image, temp_filepath
102
 
103
  except Exception as e:
@@ -106,7 +106,6 @@ def fn(vid, bg_type="Color", bg_image=None, bg_video=None, color="#00FF00", fps=
106
  yield None, f"Error processing video: {e}"
107
 
108
 
109
-
110
  def process(image, bg):
111
  image_size = image.size
112
  input_images = transform_image(image).unsqueeze(0).to("cuda")
 
26
  ]
27
  )
28
 
29
+ BATCH_SIZE = 3
30
 
31
  @spaces.GPU
32
  def fn(vid, bg_type="Color", bg_image=None, bg_video=None, color="#00FF00", fps=0, video_handling="slow_down"):
33
  try:
 
34
  video = mp.VideoFileClip(vid)
 
 
35
  if fps == 0:
36
  fps = video.fps
 
 
37
  audio = video.audio
 
 
38
  frames = video.iter_frames(fps=fps)
 
 
39
  processed_frames = []
40
  yield gr.update(visible=True), gr.update(visible=False)
41
 
 
44
  if background_video.duration < video.duration:
45
  if video_handling == "slow_down":
46
  background_video = background_video.fx(mp.vfx.speedx, factor=video.duration / background_video.duration)
47
+ else:
48
  background_video = mp.concatenate_videoclips([background_video] * int(video.duration / background_video.duration + 1))
49
+ background_frames = list(background_video.iter_frames(fps=fps))
50
  else:
51
  background_frames = None
52
 
53
+ bg_frame_index = 0
54
+ frame_batch = []
55
 
56
  for i, frame in enumerate(frames):
57
+ frame_batch.append(frame)
58
+ if len(frame_batch) == BATCH_SIZE or i == video.fps * video.duration -1: # Process batch or last frames
59
+ pil_images = [Image.fromarray(f) for f in frame_batch]
60
+
61
+
62
+ if bg_type == "Color":
63
+ processed_images = [process(img, color) for img in pil_images]
64
+ elif bg_type == "Image":
65
+ processed_images = [process(img, bg_image) for img in pil_images]
66
+ elif bg_type == "Video":
67
+ processed_images = []
68
+ for _ in range(len(frame_batch)):
69
+ if video_handling == "slow_down":
70
+ background_frame = background_frames[bg_frame_index % len(background_frames)]
71
+ bg_frame_index += 1
72
+ background_image = Image.fromarray(background_frame)
73
+ else: # video_handling == "loop"
74
+ background_frame = background_frames[bg_frame_index % len(background_frames)]
75
+ bg_frame_index += 1
76
+ background_image = Image.fromarray(background_frame)
 
 
 
 
77
 
78
+ processed_images.append(process(pil_images[_],background_image))
79
+
80
+
81
+ else:
82
+ processed_images = pil_images
83
+
84
+ for processed_image in processed_images:
85
+ processed_frames.append(np.array(processed_image))
86
+ yield processed_image, None
87
+ frame_batch = [] # Clear the batch
88
+
89
+
90
+ processed_video = mp.ImageSequenceClip(processed_frames, fps=fps)
91
  processed_video = processed_video.set_audio(audio)
92
 
 
93
  temp_dir = "temp"
94
  os.makedirs(temp_dir, exist_ok=True)
95
  unique_filename = str(uuid.uuid4()) + ".mp4"
96
  temp_filepath = os.path.join(temp_dir, unique_filename)
97
+
98
+ processed_video.write_videofile(temp_filepath, codec="libx264", logger=None)
99
 
100
  yield gr.update(visible=False), gr.update(visible=True)
 
101
  yield processed_image, temp_filepath
102
 
103
  except Exception as e:
 
106
  yield None, f"Error processing video: {e}"
107
 
108
 
 
109
  def process(image, bg):
110
  image_size = image.size
111
  input_images = transform_image(image).unsqueeze(0).to("cuda")