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 # Import the depth model inference function import logging import io from multiprocessing import Pool, cpu_count # Set up logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) # Set device to use the L40s GPU explicitly device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # Preprocess the video to adjust resolution and frame rate def preprocess_video(video_path, target_fps=24, max_resolution=(1920, 1080)): """Preprocess the video to resize and adjust 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(height=max_resolution[1]) # Adjust FPS if target_fps is specified if target_fps > 0: video = video.set_fps(target_fps) return video # Process a single frame through the depth model def process_frame(args): """Process a single frame through the depth model and return depth map.""" frame_index, frame_data, seed = args try: # Set seeds for reproducibility torch.manual_seed(seed) np.random.seed(seed) # Convert frame data to PIL Image image = Image.fromarray(frame_data).convert('RGB') # Save image to an in-memory file img_bytes = io.BytesIO() image.save(img_bytes, format='PNG') img_bytes.seek(0) # Reset file pointer to the beginning # Process through the depth model _, output_d = lotus(img_bytes, 'depth', seed, device) # Convert depth output to numpy array depth_array = np.array(output_d) return (frame_index, depth_array) except Exception as e: logger.error(f"Error processing frame {frame_index}: {e}") return (frame_index, None) # Process video frames and generate depth maps def process_video(video_path, fps=0, seed=0, num_workers=4): """Process video frames in parallel 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(fps=video.fps)) 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) # Prepare arguments for multiprocessing args_list = [(i, frames[i], seed) for i in range(total_frames)] # Use multiprocessing Pool to process frames in parallel with Pool(processes=num_workers) as pool: results = [] for result in pool.imap_unordered(process_frame, args_list): frame_index, depth_map = result if depth_map is not None: # Save frame frame_path = os.path.join(frames_dir, f"frame_{frame_index:06d}.png") Image.fromarray(depth_map.squeeze()).save(frame_path) # Update preview every 10% progress if (frame_index + 1) % max(1, total_frames // 10) == 0: elapsed_time = time.time() - start_time progress = ((frame_index + 1) / total_frames) * 100 yield depth_map, None, None, f"Processed {frame_index + 1}/{total_frames} frames... ({progress:.2f}%) Elapsed: {elapsed_time:.2f}s" else: logger.error(f"Frame {frame_index} failed to process.") 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 ( ffmpeg .input(os.path.join(frames_dir, 'frame_%06d.png'), pattern_type='sequence', framerate=fps) .output(output_video_path, vcodec='libx264', pix_fmt='yuv420p', crf=17) .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}" # Do not delete temp_dir here; we need the files to persist # Cleanup can be handled elsewhere if necessary # Wrapper function with error handling def process_wrapper(video, fps=0, seed=0, num_workers=4): 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, num_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 (unchanged) custom_css = """ /* Your existing custom CSS */ """ # Gradio Interface with gr.Blocks(css=custom_css) as demo: gr.HTML('''
Video Depth Estimation
''') 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") num_workers_slider = gr.Slider(minimum=1, maximum=cpu_count(), step=1, value=4, label="Number of Workers") 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, num_workers_slider], outputs=[preview_image, output_frames_zip, output_video, time_textbox] ) demo.queue() if __name__ == "__main__": demo.launch(debug=True)