KingNish commited on
Commit
f1c7671
1 Parent(s): fb480c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -30
app.py CHANGED
@@ -11,6 +11,7 @@ import numpy as np
11
  import os
12
  import tempfile
13
  import uuid
 
14
 
15
  torch.set_float32_matmul_precision("highest")
16
 
@@ -28,6 +29,31 @@ transform_image = transforms.Compose(
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:
@@ -35,7 +61,7 @@ def fn(vid, bg_type="Color", bg_image=None, bg_video=None, color="#00FF00", fps=
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
 
@@ -52,40 +78,22 @@ def fn(vid, bg_type="Color", bg_image=None, bg_video=None, color="#00FF00", fps=
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)
 
11
  import os
12
  import tempfile
13
  import uuid
14
+ import threading
15
 
16
  torch.set_float32_matmul_precision("highest")
17
 
 
29
 
30
  BATCH_SIZE = 3
31
 
32
+ def process_batch(frame_batch, bg_type, bg_image, bg_video, color, fps, video_handling, bg_frame_index, background_frames):
33
+ pil_images = [Image.fromarray(f) for f in frame_batch]
34
+ processed_images = []
35
+
36
+ if bg_type == "Color":
37
+ processed_images = [process(img, color) for img in pil_images]
38
+ elif bg_type == "Image":
39
+ processed_images = [process(img, bg_image) for img in pil_images]
40
+ elif bg_type == "Video":
41
+ for _ in range(len(frame_batch)):
42
+ if video_handling == "slow_down":
43
+ background_frame = background_frames[int(bg_frame_index)]
44
+ bg_frame_index += len(background_frames) / (len(frame_batch) * (len(background_frames) / (fps*mp.VideoFileClip(bg_video).duration)))
45
+ background_image = Image.fromarray(background_frame)
46
+ else: # video_handling == "loop"
47
+ background_frame = background_frames[bg_frame_index % len(background_frames)]
48
+ bg_frame_index += 1
49
+ background_image = Image.fromarray(background_frame)
50
+
51
+ processed_images.append(process(pil_images[_], background_image))
52
+ else:
53
+ processed_images = pil_images
54
+
55
+ return processed_images, bg_frame_index
56
+
57
  @spaces.GPU
58
  def fn(vid, bg_type="Color", bg_image=None, bg_video=None, color="#00FF00", fps=0, video_handling="slow_down"):
59
  try:
 
61
  if fps == 0:
62
  fps = video.fps
63
  audio = video.audio
64
+ frames = list(video.iter_frames(fps=fps))
65
  processed_frames = []
66
  yield gr.update(visible=True), gr.update(visible=False)
67
 
 
78
 
79
  bg_frame_index = 0
80
  frame_batch = []
81
+ threads = []
82
+
83
 
84
  for i, frame in enumerate(frames):
85
  frame_batch.append(frame)
86
+ if len(frame_batch) == BATCH_SIZE or i == len(frames) - 1: # Process batch or last frames
87
+ thread = threading.Thread(target=lambda : processed_frames.extend(process_batch(frame_batch, bg_type, bg_image, bg_video, color, fps, video_handling, bg_frame_index, background_frames)[0]))
88
+ threads.append(thread)
89
+ thread.start()
90
+ frame_batch = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
+ for thread in threads:
93
+ thread.join()
 
 
94
 
95
+ for processed_image in processed_frames:
96
+ yield processed_image, None
97
 
98
  processed_video = mp.ImageSequenceClip(processed_frames, fps=fps)
99
  processed_video = processed_video.set_audio(audio)