liamsch
commited on
Commit
·
e34628e
1
Parent(s):
8b1eedf
move ffmpeg command out of GPU decorator
Browse files- gradio_demo.py +59 -47
gradio_demo.py
CHANGED
|
@@ -144,60 +144,72 @@ def process_image(image: np.ndarray) -> Image.Image:
|
|
| 144 |
|
| 145 |
|
| 146 |
@spaces.GPU
|
| 147 |
-
def
|
| 148 |
"""
|
| 149 |
-
Process
|
|
|
|
| 150 |
"""
|
| 151 |
# Initialize models on first use (lazy loading for @spaces.GPU)
|
| 152 |
initialize_models()
|
| 153 |
|
| 154 |
-
temp_dir = Path(tempfile.mkdtemp())
|
| 155 |
render_size = 512
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
)
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
progress(0.95, desc="Encoding video...")
|
| 202 |
output_path = temp_dir / "output.mp4"
|
| 203 |
ffmpeg_cmd = [
|
|
|
|
| 144 |
|
| 145 |
|
| 146 |
@spaces.GPU
|
| 147 |
+
def process_video_frames(video_path: str, temp_dir: Path, progress=gr.Progress()):
|
| 148 |
"""
|
| 149 |
+
Process video frames with GPU (inference and rendering).
|
| 150 |
+
Returns fps and number of frames processed.
|
| 151 |
"""
|
| 152 |
# Initialize models on first use (lazy loading for @spaces.GPU)
|
| 153 |
initialize_models()
|
| 154 |
|
|
|
|
| 155 |
render_size = 512
|
| 156 |
+
# Prepare dataset and dataloader
|
| 157 |
+
dataset = VideoFrameDataset(video_path, fa_model)
|
| 158 |
+
dataloader = DataLoader(dataset, batch_size=1, num_workers=0)
|
| 159 |
+
fps = dataset.fps
|
| 160 |
+
num_frames = len(dataset)
|
| 161 |
+
# Prepare rendering thread and queue
|
| 162 |
+
render_queue = Queue(maxsize=32)
|
| 163 |
+
num_render_workers = 1
|
| 164 |
+
rendering_threads = []
|
| 165 |
+
for _ in range(num_render_workers):
|
| 166 |
+
thread = RenderingThread(render_queue, temp_dir, flame.faces, c2w, render_size)
|
| 167 |
+
thread.start()
|
| 168 |
+
rendering_threads.append(thread)
|
| 169 |
+
progress(0, desc="Processing video frames...")
|
| 170 |
+
frame_idx = 0
|
| 171 |
+
with torch.no_grad():
|
| 172 |
+
for batch in dataloader:
|
| 173 |
+
images = batch["image"].to(device)
|
| 174 |
+
cropped_frames = batch["cropped_frame"]
|
| 175 |
+
# Run inference
|
| 176 |
+
predictions = sheap_model(images)
|
| 177 |
+
verts = flame(
|
| 178 |
+
shape=predictions["shape_from_facenet"],
|
| 179 |
+
expression=predictions["expr"],
|
| 180 |
+
pose=pose_components_to_rotmats(predictions),
|
| 181 |
+
eyelids=predictions["eyelids"],
|
| 182 |
+
translation=predictions["cam_trans"],
|
| 183 |
+
)
|
| 184 |
+
verts = verts.cpu()
|
| 185 |
+
for i in range(images.shape[0]):
|
| 186 |
+
cropped_frame = _tensor_to_numpy_image(cropped_frames[i])
|
| 187 |
+
render_queue.put((frame_idx, cropped_frame, verts[i]))
|
| 188 |
+
frame_idx += 1
|
| 189 |
+
progress(
|
| 190 |
+
frame_idx / num_frames, desc=f"Processing frame {frame_idx}/{num_frames}"
|
| 191 |
)
|
| 192 |
+
# Stop rendering threads
|
| 193 |
+
for _ in range(num_render_workers):
|
| 194 |
+
render_queue.put(None)
|
| 195 |
+
for thread in rendering_threads:
|
| 196 |
+
thread.join()
|
| 197 |
+
if frame_idx == 0:
|
| 198 |
+
raise ValueError("No frames were successfully processed!")
|
| 199 |
+
|
| 200 |
+
return fps, frame_idx
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
def process_video(video_path: str, progress=gr.Progress()) -> str:
|
| 204 |
+
"""
|
| 205 |
+
Process a video and return path to the rendered output video.
|
| 206 |
+
"""
|
| 207 |
+
temp_dir = Path(tempfile.mkdtemp())
|
| 208 |
+
try:
|
| 209 |
+
# Process frames with GPU
|
| 210 |
+
fps, num_frames = process_video_frames(video_path, temp_dir, progress)
|
| 211 |
+
|
| 212 |
+
# Create output video using ffmpeg (CPU-only, outside GPU context)
|
| 213 |
progress(0.95, desc="Encoding video...")
|
| 214 |
output_path = temp_dir / "output.mp4"
|
| 215 |
ffmpeg_cmd = [
|