Spaces:
Running
on
Zero
Running
on
Zero
Test
Browse files
app.py
CHANGED
@@ -11,125 +11,139 @@ import numpy as np
|
|
11 |
import os
|
12 |
import tempfile
|
13 |
import uuid
|
|
|
|
|
|
|
14 |
|
15 |
-
|
|
|
|
|
16 |
|
|
|
17 |
birefnet = AutoModelForImageSegmentation.from_pretrained(
|
18 |
"ZhengPeng7/BiRefNet", trust_remote_code=True
|
19 |
)
|
20 |
-
birefnet.to("cuda")
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
)
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
35 |
if fps == 0:
|
36 |
fps = video.fps
|
37 |
audio = video.audio
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
42 |
if bg_type == "Video":
|
43 |
-
|
44 |
-
if
|
45 |
if video_handling == "slow_down":
|
46 |
-
|
|
|
47 |
else:
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
62 |
if bg_type == "Color":
|
63 |
-
|
64 |
elif bg_type == "Image":
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
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 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
yield gr.update(visible=False), gr.update(visible=True)
|
101 |
-
yield
|
102 |
-
|
103 |
except Exception as e:
|
104 |
print(f"Error: {e}")
|
105 |
yield gr.update(visible=False), gr.update(visible=True)
|
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")
|
112 |
-
# Prediction
|
113 |
-
with torch.no_grad():
|
114 |
-
preds = birefnet(input_images)[-1].sigmoid().cpu()
|
115 |
-
pred = preds[0].squeeze()
|
116 |
-
pred_pil = transforms.ToPILImage()(pred)
|
117 |
-
mask = pred_pil.resize(image_size)
|
118 |
-
|
119 |
-
if isinstance(bg, str) and bg.startswith("#"):
|
120 |
-
color_rgb = tuple(int(bg[i:i+2], 16) for i in (1, 3, 5))
|
121 |
-
background = Image.new("RGBA", image_size, color_rgb + (255,))
|
122 |
-
elif isinstance(bg, Image.Image):
|
123 |
-
background = bg.convert("RGBA").resize(image_size)
|
124 |
-
else:
|
125 |
-
background = Image.open(bg).convert("RGBA").resize(image_size)
|
126 |
-
|
127 |
-
# Composite the image onto the background using the mask
|
128 |
-
image = Image.composite(image, background, mask)
|
129 |
-
|
130 |
-
return image
|
131 |
-
|
132 |
-
|
133 |
with gr.Blocks(theme=gr.themes.Ocean()) as demo:
|
134 |
with gr.Row():
|
135 |
in_video = gr.Video(label="Input Video", interactive=True)
|
|
|
11 |
import os
|
12 |
import tempfile
|
13 |
import uuid
|
14 |
+
from concurrent.futures import ThreadPoolExecutor
|
15 |
+
import torch.nn as nn
|
16 |
+
import torch.cuda.amp # for mixed precision training
|
17 |
|
18 |
+
# Enable tensor cores for faster computation
|
19 |
+
torch.set_float32_matmul_precision("high")
|
20 |
+
torch.backends.cudnn.benchmark = True # Enable cudnn autotuner
|
21 |
|
22 |
+
# Initialize model with optimization flags
|
23 |
birefnet = AutoModelForImageSegmentation.from_pretrained(
|
24 |
"ZhengPeng7/BiRefNet", trust_remote_code=True
|
25 |
)
|
26 |
+
birefnet.to("cuda").eval() # Ensure model is in eval mode
|
27 |
+
birefnet = torch.jit.script(birefnet) # JIT compilation for faster inference
|
28 |
+
|
29 |
+
# Pre-compile transforms for better performance
|
30 |
+
transform_image = transforms.Compose([
|
31 |
+
transforms.Resize((1024, 1024), antialias=True),
|
32 |
+
transforms.ToTensor(),
|
33 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
|
34 |
+
])
|
35 |
+
|
36 |
+
# Increased batch size for better GPU utilization
|
37 |
+
BATCH_SIZE = 8 # Increased from 3
|
38 |
+
NUM_WORKERS = 4 # For parallel processing
|
39 |
+
|
40 |
+
# Create a thread pool for parallel processing
|
41 |
+
executor = ThreadPoolExecutor(max_workers=NUM_WORKERS)
|
42 |
+
|
43 |
+
def process_batch(batch_data):
|
44 |
+
"""Process a batch of frames in parallel"""
|
45 |
+
images, backgrounds, image_sizes = zip(*batch_data)
|
46 |
+
|
47 |
+
# Stack images for batch processing
|
48 |
+
input_tensor = torch.stack(images).to("cuda")
|
49 |
+
|
50 |
+
# Use automatic mixed precision for faster computation
|
51 |
+
with torch.cuda.amp.autocast():
|
52 |
+
with torch.no_grad():
|
53 |
+
preds = birefnet(input_tensor)[-1].sigmoid().cpu()
|
54 |
+
|
55 |
+
processed_frames = []
|
56 |
+
for pred, bg, size in zip(preds, backgrounds, image_sizes):
|
57 |
+
mask = transforms.ToPILImage()(pred.squeeze()).resize(size)
|
58 |
+
|
59 |
+
if isinstance(bg, str) and bg.startswith("#"):
|
60 |
+
color_rgb = tuple(int(bg[i:i+2], 16) for i in (1, 3, 5))
|
61 |
+
background = Image.new("RGBA", size, color_rgb + (255,))
|
62 |
+
elif isinstance(bg, Image.Image):
|
63 |
+
background = bg.convert("RGBA").resize(size)
|
64 |
+
else:
|
65 |
+
background = Image.open(bg).convert("RGBA").resize(size)
|
66 |
+
|
67 |
+
# Use PIL's faster composite operation
|
68 |
+
image = Image.composite(images[0].resize(size), background, mask)
|
69 |
+
processed_frames.append(np.array(image))
|
70 |
+
|
71 |
+
return processed_frames
|
72 |
|
73 |
@spaces.GPU
|
74 |
def fn(vid, bg_type="Color", bg_image=None, bg_video=None, color="#00FF00", fps=0, video_handling="slow_down"):
|
75 |
try:
|
76 |
+
# Load video more efficiently
|
77 |
+
video = mp.VideoFileClip(vid, audio_buffersize=2000)
|
78 |
if fps == 0:
|
79 |
fps = video.fps
|
80 |
audio = video.audio
|
81 |
+
|
82 |
+
# Pre-calculate video parameters
|
83 |
+
total_frames = int(video.fps * video.duration)
|
84 |
+
frames = list(video.iter_frames(fps=fps)) # Load all frames at once
|
85 |
+
|
86 |
+
# Pre-process background if using video
|
87 |
if bg_type == "Video":
|
88 |
+
bg_video_clip = mp.VideoFileClip(bg_video)
|
89 |
+
if bg_video_clip.duration < video.duration:
|
90 |
if video_handling == "slow_down":
|
91 |
+
bg_video_clip = bg_video_clip.fx(mp.vfx.speedx,
|
92 |
+
factor=video.duration / bg_video_clip.duration)
|
93 |
else:
|
94 |
+
multiplier = int(video.duration / bg_video_clip.duration + 1)
|
95 |
+
bg_video_clip = mp.concatenate_videoclips([bg_video_clip] * multiplier)
|
96 |
+
background_frames = list(bg_video_clip.iter_frames(fps=fps))
|
97 |
+
|
98 |
+
# Process frames in batches
|
99 |
+
processed_frames = []
|
100 |
+
for i in range(0, len(frames), BATCH_SIZE):
|
101 |
+
batch_frames = frames[i:i + BATCH_SIZE]
|
102 |
+
batch_data = []
|
103 |
+
|
104 |
+
for j, frame in enumerate(batch_frames):
|
105 |
+
pil_image = Image.fromarray(frame)
|
106 |
+
image_size = pil_image.size
|
107 |
+
transformed_image = transform_image(pil_image)
|
108 |
+
|
109 |
if bg_type == "Color":
|
110 |
+
bg = color
|
111 |
elif bg_type == "Image":
|
112 |
+
bg = bg_image
|
113 |
+
else: # Video
|
114 |
+
frame_idx = (i + j) % len(background_frames)
|
115 |
+
bg = Image.fromarray(background_frames[frame_idx])
|
116 |
+
|
117 |
+
batch_data.append((transformed_image, bg, image_size))
|
118 |
+
|
119 |
+
# Process batch
|
120 |
+
batch_results = process_batch(batch_data)
|
121 |
+
processed_frames.extend(batch_results)
|
122 |
+
|
123 |
+
# Yield progress updates
|
124 |
+
if len(batch_results) > 0:
|
125 |
+
yield batch_results[-1], None
|
126 |
+
|
127 |
+
# Create output video
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
processed_video = mp.ImageSequenceClip(processed_frames, fps=fps)
|
129 |
+
if audio is not None:
|
130 |
+
processed_video = processed_video.set_audio(audio)
|
131 |
+
|
132 |
+
# Use temporary file
|
133 |
+
with tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) as tmp_file:
|
134 |
+
output_path = tmp_file.name
|
135 |
+
processed_video.write_videofile(output_path, codec="libx264",
|
136 |
+
preset='ultrafast', threads=NUM_WORKERS)
|
137 |
+
|
138 |
yield gr.update(visible=False), gr.update(visible=True)
|
139 |
+
yield processed_frames[-1], output_path
|
140 |
+
|
141 |
except Exception as e:
|
142 |
print(f"Error: {e}")
|
143 |
yield gr.update(visible=False), gr.update(visible=True)
|
144 |
yield None, f"Error processing video: {e}"
|
145 |
|
146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
with gr.Blocks(theme=gr.themes.Ocean()) as demo:
|
148 |
with gr.Row():
|
149 |
in_video = gr.Video(label="Input Video", interactive=True)
|