File size: 8,583 Bytes
44189a1 09e9f28 48a87fd 09e9f28 17eeb1a 48a87fd 09e9f28 17eeb1a 7bb1989 dc78df8 17eeb1a dc78df8 17eeb1a 48a87fd 17eeb1a 284af32 17eeb1a 284af32 17eeb1a 284af32 17eeb1a dc78df8 17eeb1a 09e9f28 17eeb1a 09e9f28 17eeb1a 09e9f28 48a87fd 17eeb1a 09e9f28 17eeb1a 48a87fd 17eeb1a dc78df8 09e9f28 17eeb1a 09e9f28 17eeb1a 09e9f28 17eeb1a 44189a1 17eeb1a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
import gradio as gr
import torch
import os
import tempfile
import shutil
import time
import ffmpeg
import numpy as np
from PIL import Image
from concurrent.futures import ThreadPoolExecutor
import moviepy.editor as mp
from infer import lotus # Import the depth model inference function
import spaces
# Set device to use the L40s GPU
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Add the preprocess_video function to limit video resolution and frame rate
def preprocess_video(video_path, target_fps=24, max_resolution=(1920, 1080)):
"""Preprocess the video to resize and reduce its frame rate."""
video = mp.VideoFileClip(video_path)
# Resize video if it's larger than the target resolution
if video.size[0] > max_resolution[0] or video.size[1] > max_resolution[1]:
video = video.resize(newsize=max_resolution)
# Limit FPS
video = video.set_fps(target_fps)
return video
def process_frame(frame, seed=0):
"""Process a single frame through the depth model and return depth map."""
try:
# Convert frame to PIL Image
image = Image.fromarray(frame)
# Save temporary image (lotus requires a file path)
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp:
image.save(tmp.name)
# Process through the depth model (lotus)
_, output_d = lotus(tmp.name, 'depth', seed, device)
# Clean up temp file
os.unlink(tmp.name)
# Convert depth output to numpy array
depth_array = np.array(output_d)
return depth_array
except Exception as e:
print(f"Error processing frame: {e}")
return None
@spaces.GPU
def process_video(video_path, fps=0, seed=0, max_workers=32):
"""Process video, batch frames, and use L40s GPU to generate depth maps."""
temp_dir = None
try:
start_time = time.time()
# Preprocess the video
video = preprocess_video(video_path)
# Use original video FPS if not specified
if fps == 0:
fps = video.fps
frames = list(video.iter_frames(fps=fps))
total_frames = len(frames)
print(f"Processing {total_frames} frames at {fps} FPS...")
# Create temporary directory for frame sequence
temp_dir = tempfile.mkdtemp()
frames_dir = os.path.join(temp_dir, "frames")
os.makedirs(frames_dir, exist_ok=True)
# Process frames in larger batches (based on GPU VRAM)
batch_size = 50 # Increased batch size to fully utilize the GPU's capabilities
processed_frames = []
with ThreadPoolExecutor(max_workers=max_workers) as executor:
for i in range(0, total_frames, batch_size):
futures = [executor.submit(process_frame, frames[j], seed) for j in range(i, min(i + batch_size, total_frames))]
for j, future in enumerate(futures):
try:
result = future.result()
if result is not None:
# Save frame
frame_path = os.path.join(frames_dir, f"frame_{i+j:06d}.png")
Image.fromarray(result).save(frame_path)
# Collect processed frame for preview
processed_frames.append(result)
# Update preview (only showing every 10th frame to avoid clutter)
if (i + j + 1) % 10 == 0:
elapsed_time = time.time() - start_time
yield processed_frames[-1], None, None, f"Processed {i+j+1}/{total_frames} frames... Elapsed: {elapsed_time:.2f}s"
except Exception as e:
print(f"Error processing frame {i + j + 1}: {e}")
print("Creating output files...")
# Create output directory
output_dir = os.path.join(os.path.dirname(video_path), "output")
os.makedirs(output_dir, exist_ok=True)
# Create ZIP of frame sequence
zip_filename = f"depth_frames_{int(time.time())}.zip"
zip_path = os.path.join(output_dir, zip_filename)
shutil.make_archive(zip_path[:-4], 'zip', frames_dir)
# Create MP4 video
video_filename = f"depth_video_{int(time.time())}.mp4"
video_path = os.path.join(output_dir, video_filename)
try:
# FFmpeg settings for high-quality MP4
stream = ffmpeg.input(
os.path.join(frames_dir, 'frame_%06d.png'),
pattern_type='sequence',
framerate=fps
)
stream = ffmpeg.output(
stream,
video_path,
vcodec='libx264',
pix_fmt='yuv420p',
crf=17, # High quality
threads=max_workers
)
ffmpeg.run(stream, overwrite_output=True, capture_stdout=True, capture_stderr=True)
print("MP4 video created successfully!")
except ffmpeg.Error as e:
print(f"Error creating video: {e.stderr.decode() if e.stderr else str(e)}")
video_path = None
print("Processing complete!")
yield None, zip_path, video_path, f"Processing complete! Total time: {time.time() - start_time:.2f} seconds"
except Exception as e:
print(f"Error: {e}")
yield None, None, None, f"Error processing video: {e}"
finally:
if temp_dir and os.path.exists(temp_dir):
try:
shutil.rmtree(temp_dir)
except Exception as e:
print(f"Error cleaning up temp directory: {e}")
def process_wrapper(video, fps=0, seed=0, max_workers=32):
if video is None:
raise gr.Error("Please upload a video.")
try:
outputs = []
for output in process_video(video, fps, seed, max_workers):
outputs.append(output)
yield output
return outputs[-1]
except Exception as e:
raise gr.Error(f"Error processing video: {str(e)}")
# Custom CSS for styling
custom_css = """
.title-container {
text-align: center;
padding: 10px 0;
}
#title {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
font-size: 36px;
font-weight: bold;
color: #000000;
padding: 10px;
border-radius: 10px;
display: inline-block;
background: linear-gradient(
135deg,
#e0f7fa, #e8f5e9, #fff9c4, #ffebee,
#f3e5f5, #e1f5fe, #fff3e0, #e8eaf6
);
background-size: 400% 400%;
animation: gradient-animation 15s ease infinite;
}
@keyframes gradient-animation {
0% { background-position: 0% 50%; }
50% { background-position: 100% 50%; }
100% { background-position: 0% 50%; }
}
"""
# Gradio Interface
with gr.Blocks(css=custom_css) as demo:
gr.HTML('''
<div class="title-container">
<div id="title">Video Depth Estimation</div>
</div>
''')
with gr.Row():
with gr.Column():
video_input = gr.Video(label="Upload Video", interactive=True, show_label=True)
fps_slider = gr.Slider(minimum=0, maximum=60, step=1, value=0, label="Output FPS")
seed_slider = gr.Slider(minimum=0, maximum=999999999, step=1, value=0, label="Seed")
max_workers_slider = gr.Slider(minimum=1, maximum=32, step=1, value=32, label="Max Workers")
btn = gr.Button("Process Video", elem_id="submit-button")
with gr.Column():
preview_image = gr.Image(label="Live Preview", show_label=True)
output_frames_zip = gr.File(label="Download Frame Sequence (ZIP)")
output_video = gr.File(label="Download Video (MP4)")
time_textbox = gr.Textbox(label="Status", interactive=False)
btn.click(fn=process_wrapper
, inputs=[video_input, fps_slider, seed_slider, max_workers_slider],
outputs=[preview_image, output_frames_zip, output_video, time_textbox])
demo.queue()
if __name__ == "__main__":
demo.launch(debug=True) |