Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -11,7 +11,7 @@ import numpy as np
|
|
11 |
import os
|
12 |
import tempfile
|
13 |
import uuid
|
14 |
-
import
|
15 |
|
16 |
torch.set_float32_matmul_precision("highest")
|
17 |
|
@@ -28,75 +28,66 @@ transform_image = transforms.Compose(
|
|
28 |
)
|
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:
|
60 |
video = mp.VideoFileClip(vid)
|
|
|
|
|
|
|
|
|
61 |
if fps == 0:
|
62 |
fps = video.fps
|
63 |
-
|
64 |
-
frames =
|
65 |
processed_frames = []
|
66 |
yield gr.update(visible=True), gr.update(visible=False)
|
67 |
|
68 |
if bg_type == "Video":
|
69 |
background_video = mp.VideoFileClip(bg_video)
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
background_frames = list(background_video.iter_frames(fps=fps))
|
|
|
76 |
else:
|
77 |
background_frames = None
|
|
|
|
|
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 |
-
|
93 |
-
|
|
|
94 |
|
95 |
-
|
96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
processed_video = mp.ImageSequenceClip(processed_frames, fps=fps)
|
99 |
-
|
|
|
100 |
|
101 |
temp_dir = "temp"
|
102 |
os.makedirs(temp_dir, exist_ok=True)
|
|
|
11 |
import os
|
12 |
import tempfile
|
13 |
import uuid
|
14 |
+
from concurrent.futures import ThreadPoolExecutor
|
15 |
|
16 |
torch.set_float32_matmul_precision("highest")
|
17 |
|
|
|
28 |
)
|
29 |
|
30 |
BATCH_SIZE = 3
|
31 |
+
executor = ThreadPoolExecutor(max_workers=4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
@spaces.GPU
|
34 |
def fn(vid, bg_type="Color", bg_image=None, bg_video=None, color="#00FF00", fps=0, video_handling="slow_down"):
|
35 |
try:
|
36 |
video = mp.VideoFileClip(vid)
|
37 |
+
try:
|
38 |
+
audio = video.audio
|
39 |
+
except AttributeError:
|
40 |
+
audio = None
|
41 |
if fps == 0:
|
42 |
fps = video.fps
|
43 |
+
|
44 |
+
frames = video.iter_frames(fps=fps)
|
45 |
processed_frames = []
|
46 |
yield gr.update(visible=True), gr.update(visible=False)
|
47 |
|
48 |
if bg_type == "Video":
|
49 |
background_video = mp.VideoFileClip(bg_video)
|
50 |
+
|
51 |
+
if background_video.duration < video.duration and video_handling == "slow_down":
|
52 |
+
slow_down_factor = video.duration / background_video.duration
|
53 |
+
else:
|
54 |
+
slow_down_factor = 1
|
55 |
background_frames = list(background_video.iter_frames(fps=fps))
|
56 |
+
|
57 |
else:
|
58 |
background_frames = None
|
59 |
+
slow_down_factor = None
|
60 |
+
|
61 |
|
62 |
bg_frame_index = 0
|
63 |
frame_batch = []
|
|
|
64 |
|
65 |
|
66 |
for i, frame in enumerate(frames):
|
67 |
frame_batch.append(frame)
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
+
if len(frame_batch) == BATCH_SIZE or i == int(video.fps * video.duration) - 1:
|
70 |
+
|
71 |
+
pil_images = [Image.fromarray(f) for f in frame_batch]
|
72 |
|
73 |
+
if bg_type == "Video":
|
74 |
+
processed_images = list(executor.map(process, pil_images, [get_background_image(bg_type, bg_image, background_frames, bg_frame_index + j, video_handling, slow_down_factor) for j in range(len(pil_images))]))
|
75 |
+
bg_frame_index += len(frame_batch)
|
76 |
+
elif bg_type == "Color":
|
77 |
+
processed_images = list(executor.map(process, pil_images, [color] * len(pil_images)))
|
78 |
+
elif bg_type == "Image":
|
79 |
+
processed_images = list(executor.map(process, pil_images, [bg_image] * len(pil_images)))
|
80 |
+
else:
|
81 |
+
processed_images = pil_images
|
82 |
+
|
83 |
+
for processed_image in processed_images:
|
84 |
+
processed_frames.append(np.array(processed_image))
|
85 |
+
yield processed_image, None
|
86 |
+
frame_batch = []
|
87 |
|
88 |
processed_video = mp.ImageSequenceClip(processed_frames, fps=fps)
|
89 |
+
if audio:
|
90 |
+
processed_video = processed_video.set_audio(audio)
|
91 |
|
92 |
temp_dir = "temp"
|
93 |
os.makedirs(temp_dir, exist_ok=True)
|