File size: 7,915 Bytes
44189a1 0567627 c12e34c b7ae821 c12e34c b7ae821 17eeb1a b7ae821 a9377e4 b7ae821 ec60737 a85f402 ec60737 dc78df8 a85f402 dc78df8 a9377e4 a85f402 ec60737 afe7cc3 ec60737 afe7cc3 0a17f2b 3e8f9ec 17eeb1a afe7cc3 a85f402 afe7cc3 17eeb1a 284af32 0567627 a9377e4 0567627 7d32b39 17eeb1a 3e8f9ec 0567627 afe7cc3 0567627 ec60737 0567627 afe7cc3 17eeb1a 7d32b39 dc78df8 7d32b39 8df521f 17eeb1a afe7cc3 17eeb1a 3e8f9ec afe7cc3 17eeb1a afe7cc3 0567627 afe7cc3 a85f402 afe7cc3 8df521f afe7cc3 7d32b39 0567627 a9377e4 7d32b39 0567627 7d32b39 3e8f9ec 7d32b39 0567627 7d32b39 0567627 7d32b39 afe7cc3 8df521f afe7cc3 8df521f afe7cc3 0567627 8df521f afe7cc3 8df521f 0567627 3e8f9ec 8df521f 3e8f9ec 0567627 8e07e41 8df521f 0567627 afe7cc3 8df521f afe7cc3 8df521f afe7cc3 8df521f afe7cc3 17eeb1a a85f402 17eeb1a a85f402 173c7e2 6a4fc34 0567627 7d32b39 17eeb1a 8324267 7d32b39 17eeb1a 48a87fd 8df521f 17eeb1a afe7cc3 17eeb1a dc78df8 09e9f28 b7ae821 17eeb1a afe7cc3 09e9f28 a85f402 afe7cc3 09e9f28 a85f402 17eeb1a afe7cc3 a85f402 7d32b39 a85f402 17eeb1a 44189a1 17eeb1a 0567627 |
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 230 231 232 233 234 235 236 237 238 |
import gradio as gr
import torch
import os
import tempfile
import shutil
import time
import ffmpeg
import numpy as np
from PIL import Image
import moviepy.editor as mp
from infer import lotus, load_models, pipe_g, pipe_d # Import the global models
import logging
import spaces # Import the spaces module for ZeroGPU
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Device will be set inside GPU-decorated functions
device = 'cuda' # Use 'cuda' as placeholder
# Load models once inside a GPU context
task_name = 'depth'
@spaces.GPU
def initialize_models():
load_models(task_name, device)
# Call the function to load models
initialize_models()
# Preprocess the video to adjust frame rate
def preprocess_video(video_path, target_fps=24):
"""Preprocess the video to adjust its frame rate."""
video = mp.VideoFileClip(video_path)
# Adjust FPS if target_fps is specified
if target_fps > 0:
video = video.set_fps(target_fps)
return video
# Resize image while preserving aspect ratio and adding padding
def resize_and_pad(image, target_size):
"""Resize and pad an image to the target size while preserving aspect ratio."""
# Calculate the new size preserving aspect ratio
image.thumbnail(target_size, Image.LANCZOS)
# Create a new image with the target size and black background
new_image = Image.new("RGB", target_size)
new_image.paste(
image, ((target_size[0] - image.width) // 2, (target_size[1] - image.height) // 2)
)
return new_image
@spaces.GPU
def process_frame(frame, seed=0, target_size=(512, 512)):
"""Process a single frame and return depth map."""
try:
# Convert frame to PIL Image
image = Image.fromarray(frame).convert('RGB')
# Resize and pad image
input_image = resize_and_pad(image, target_size)
# Run inference
depth_map = lotus(input_image, 'depth', seed, device)
# Crop the output depth map back to original image size
width, height = image.size
left = (target_size[0] - width) // 2
top = (target_size[1] - height) // 2
right = left + width
bottom = top + height
depth_map_cropped = depth_map.crop((left, top, right, bottom))
return depth_map_cropped
except Exception as e:
logger.error(f"Error processing frame: {e}")
return None
def process_video(video_path, fps=0, seed=0):
"""Process video frames individually and generate depth maps."""
# Create a persistent temporary directory
temp_dir = tempfile.mkdtemp()
try:
start_time = time.time()
# Preprocess the video
video = preprocess_video(video_path, target_fps=fps)
# Use original video FPS if not specified
if fps == 0:
fps = video.fps
frames = list(video.iter_frames())
total_frames = len(frames)
logger.info(f"Processing {total_frames} frames at {fps} FPS...")
# Create directory for frame sequence and outputs
frames_dir = os.path.join(temp_dir, "frames")
os.makedirs(frames_dir, exist_ok=True)
# Process frames individually
for i, frame in enumerate(frames):
# Process each frame with GPU allocation
depth_map = process_frame(frame, seed)
if depth_map is not None:
# Save frame
frame_path = os.path.join(frames_dir, f"frame_{i:06d}.png")
depth_map.save(frame_path, format='PNG', compress_level=0)
# Update live preview every 10% progress
if i % max(1, total_frames // 10) == 0:
elapsed_time = time.time() - start_time
progress = (i / total_frames) * 100
yield depth_map, None, None, f"Processed {i}/{total_frames} frames... ({progress:.2f}%) Elapsed: {elapsed_time:.2f}s"
else:
logger.error(f"Error processing frame {i}")
logger.info("Creating output files...")
# Create ZIP of frame sequence
zip_filename = f"depth_frames_{int(time.time())}.zip"
zip_path = os.path.join(temp_dir, zip_filename)
shutil.make_archive(zip_path[:-4], 'zip', frames_dir)
# Create MP4 video
video_filename = f"depth_video_{int(time.time())}.mp4"
output_video_path = os.path.join(temp_dir, video_filename)
try:
# FFmpeg settings for high-quality MP4
input_pattern = os.path.join(frames_dir, 'frame_%06d.png')
(
ffmpeg
.input(input_pattern, pattern_type='sequence', framerate=fps)
.output(output_video_path, vcodec='libx264', pix_fmt='yuv420p', crf=17, preset='slow')
.run(overwrite_output=True, quiet=True)
)
logger.info("MP4 video created successfully!")
except ffmpeg.Error as e:
logger.error(f"Error creating video: {e.stderr.decode() if e.stderr else str(e)}")
output_video_path = None
total_time = time.time() - start_time
logger.info("Processing complete!")
# Yield the file paths
yield None, zip_path, output_video_path, f"Processing complete! Total time: {total_time:.2f} seconds"
except Exception as e:
logger.error(f"Error: {e}")
yield None, None, None, f"Error processing video: {e}"
finally:
# Clean up temporary directory if necessary
pass
def process_wrapper(video, fps=0, seed=0):
if video is None:
raise gr.Error("Please upload a video.")
try:
outputs = []
# Use video directly, since it's the file path
for output in process_video(video, fps, seed):
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 (unchanged)
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)
fps_slider = gr.Slider(minimum=0, maximum=60, step=1, value=0, label="Output FPS (0 for original)")
seed_slider = gr.Number(value=0, label="Seed")
btn = gr.Button("Process Video")
with gr.Column():
preview_image = gr.Image(label="Live Preview")
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],
outputs=[preview_image, output_frames_zip, output_video, time_textbox]
)
demo.queue()
if __name__ == "__main__":
demo.launch(debug=True) |