Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,6 @@ import spaces
|
|
4 |
from transformers import AutoModelForImageSegmentation
|
5 |
import torch
|
6 |
from torchvision import transforms
|
7 |
-
import moviepy as mp
|
8 |
from pydub import AudioSegment
|
9 |
from PIL import Image
|
10 |
import numpy as np
|
@@ -13,6 +12,7 @@ import tempfile
|
|
13 |
import uuid
|
14 |
import time
|
15 |
from concurrent.futures import ThreadPoolExecutor
|
|
|
16 |
|
17 |
torch.set_float32_matmul_precision("medium")
|
18 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
@@ -53,7 +53,7 @@ def process_frame(frame, bg_type, bg, fast_mode, bg_frame_index, background_fram
|
|
53 |
def fn(vid, bg_type="Color", bg_image=None, bg_video=None, color="#00FF00", fps=0, video_handling="slow_down", fast_mode=True, max_workers=6):
|
54 |
try:
|
55 |
start_time = time.time() # Start the timer
|
56 |
-
video =
|
57 |
if fps == 0:
|
58 |
fps = video.fps
|
59 |
|
@@ -64,12 +64,12 @@ def fn(vid, bg_type="Color", bg_image=None, bg_video=None, color="#00FF00", fps=
|
|
64 |
yield gr.update(visible=True), gr.update(visible=False), f"Processing started... Elapsed time: 0 seconds"
|
65 |
|
66 |
if bg_type == "Video":
|
67 |
-
background_video =
|
68 |
if background_video.duration < video.duration:
|
69 |
if video_handling == "slow_down":
|
70 |
-
background_video = background_video.fx(
|
71 |
else: # video_handling == "loop"
|
72 |
-
background_video =
|
73 |
background_frames = list(background_video.iter_frames(fps=fps))
|
74 |
else:
|
75 |
background_frames = None
|
@@ -85,7 +85,7 @@ def fn(vid, bg_type="Color", bg_image=None, bg_video=None, color="#00FF00", fps=
|
|
85 |
elapsed_time = time.time() - start_time
|
86 |
yield result, None, f"Processing frame {i+1}/{len(frames)}... Elapsed time: {elapsed_time:.2f} seconds"
|
87 |
|
88 |
-
processed_video =
|
89 |
processed_video = processed_video.set_audio(audio)
|
90 |
|
91 |
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as temp_file:
|
|
|
4 |
from transformers import AutoModelForImageSegmentation
|
5 |
import torch
|
6 |
from torchvision import transforms
|
|
|
7 |
from pydub import AudioSegment
|
8 |
from PIL import Image
|
9 |
import numpy as np
|
|
|
12 |
import uuid
|
13 |
import time
|
14 |
from concurrent.futures import ThreadPoolExecutor
|
15 |
+
from moviepy import VideoFileClip, vfx, concatenate_videoclips, ImageSequenceClip
|
16 |
|
17 |
torch.set_float32_matmul_precision("medium")
|
18 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
53 |
def fn(vid, bg_type="Color", bg_image=None, bg_video=None, color="#00FF00", fps=0, video_handling="slow_down", fast_mode=True, max_workers=6):
|
54 |
try:
|
55 |
start_time = time.time() # Start the timer
|
56 |
+
video = VideoFileClip(vid)
|
57 |
if fps == 0:
|
58 |
fps = video.fps
|
59 |
|
|
|
64 |
yield gr.update(visible=True), gr.update(visible=False), f"Processing started... Elapsed time: 0 seconds"
|
65 |
|
66 |
if bg_type == "Video":
|
67 |
+
background_video = VideoFileClip(bg_video)
|
68 |
if background_video.duration < video.duration:
|
69 |
if video_handling == "slow_down":
|
70 |
+
background_video = background_video.fx(vfx.speedx, factor=video.duration / background_video.duration)
|
71 |
else: # video_handling == "loop"
|
72 |
+
background_video = concatenate_videoclips([background_video] * int(video.duration / background_video.duration + 1))
|
73 |
background_frames = list(background_video.iter_frames(fps=fps))
|
74 |
else:
|
75 |
background_frames = None
|
|
|
85 |
elapsed_time = time.time() - start_time
|
86 |
yield result, None, f"Processing frame {i+1}/{len(frames)}... Elapsed time: {elapsed_time:.2f} seconds"
|
87 |
|
88 |
+
processed_video = ImageSequenceClip(processed_frames, fps=fps)
|
89 |
processed_video = processed_video.set_audio(audio)
|
90 |
|
91 |
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as temp_file:
|