Spaces:
Build error
Build error
Update app.py
Browse files1234563 unmodified lines10111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970715 unmodified lines777879808182838485868788899091929394959697989910033 unmodified lines1341351361371381391401 unmodified line1421431441451461471485 unmodified lines1541551561571581591601611621634 unmodified lines16816917017117217317417517617717810 unmodified lines189190191192193194195196197198199200201202203204205206207import osimport gcimport torchimport cv2import gradio as grimport numpy as np3 unmodified linesimport sysimport spacesfrom video_depth_anything.video_depth import VideoDepthAnythingfrom utils.dc_utils import read_video_frames, save_videofrom huggingface_hub import hf_hub_download# Examples for the Gradio Demo.# Each example now contains 8 parameters:# [video_path, max_len, target_fps, max_res, stitch, grayscale, convert_from_color, blur]examples = [['assets/example_videos/octopus_01.mp4', -1, -1, 1280, True, True, True, 0.3],['assets/example_videos/chicken_01.mp4', -1, -1, 1280, True, True, True, 0.3],['assets/example_videos/gorilla_01.mp4', -1, -1, 1280, True, True, True, 0.3],['assets/example_videos/davis_rollercoaster.mp4', -1, -1, 1280, True, True, True, 0.3],['assets/example_videos/Tokyo-Walk_rgb.mp4', -1, -1, 1280, True, True, True, 0.3],['assets/example_videos/4158877-uhd_3840_2160_30fps_rgb.mp4', -1, -1, 1280, True, True, True, 0.3],['assets/example_videos/4511004-uhd_3840_2160_24fps_rgb.mp4', -1, -1, 1280, True, True, True, 0.3],['assets/example_videos/1753029-hd_1920_1080_30fps.mp4', -1, -1, 1280, True, True, True, 0.3],['assets/example_videos/davis_burnout.mp4', -1, -1, 1280, True, True, True, 0.3],['assets/example_videos/example_5473765-l.mp4', -1, -1, 1280, True, True, True, 0.3],['assets/example_videos/Istanbul-26920.mp4', -1, -1, 1280, True, True, True, 0.3],['assets/example_videos/obj_1.mp4', -1, -1, 1280, True, True, True, 0.3],['assets/example_videos/sheep_cut1.mp4', -1, -1, 1280, True, True, True, 0.3],]# Use GPU if available; otherwise, use CPU.DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'# Model configuration for different encoder variants.model_configs = {'vits': {'encoder': 'vits', 'features': 64, 'out_channels': [48, 96, 192, 384]},'vitl': {'encoder': 'vitl', 'features': 256, 'out_channels': [256, 512, 1024, 1024]},}encoder2name = {'vits': 'Small','vitl': 'Large',}encoder = 'vitl'model_name = encoder2name[encoder]# Initialize the model.video_depth_anything = VideoDepthAnything(**model_configs[encoder])filepath = hf_hub_download(repo_id=f"depth-anything/Video-Depth-Anything-{model_name}",filename=f"video_depth_anything_{encoder}.pth",repo_type="model")video_depth_anything.load_state_dict(torch.load(filepath, map_location='cpu'))video_depth_anything = video_depth_anything.to(DEVICE).eval()title = "# Video Depth Anything + RGBD sbs output"description = """**Video Depth Anything** + RGBD sbs output for viewing with Looking Glass Factory displays.Please refer to our [paper](https://arxiv.org/abs/2501.12375), [project page](https://videodepthanything.github.io/), and [github](https://github.com/DepthAnything/Video-Depth-Anything) for more details."""@spaces.GPU(enable_queue=True)def infer_video_depth(input_video: str,max_len: int = -1,target_fps: int = -1,max_res: int = 1280,5 unmodified linesinput_size: int = 518,):# 1. Read input video frames for inference (downscaled to max_res).frames, target_fps = read_video_frames(input_video, max_len, target_fps, max_res)# 2. Perform depth inference using the model.depths, fps = video_depth_anything.infer_video_depth(frames, target_fps, input_size=input_size, device=DEVICE)video_name = os.path.basename(input_video)if not os.path.exists(output_dir):os.makedirs(output_dir)# Save the preprocessed (RGB) video and the generated depth visualization.processed_video_path = os.path.join(output_dir, os.path.splitext(video_name)[0] + '_src.mp4')depth_vis_path = os.path.join(output_dir, os.path.splitext(video_name)[0] + '_vis.mp4')save_video(frames, processed_video_path, fps=fps)save_video(depths, depth_vis_path, fps=fps, is_depths=True)stitched_video_path = Noneif stitch:# For stitching: read the original video in full resolution (without downscaling).full_frames, _ = read_video_frames(input_video, max_len, target_fps, max_res=-1)# For each frame, create a visual depth image from the inferenced depths.d_min, d_max = depths.min(), depths.max()stitched_frames = []33 unmodified linesbase_name = os.path.splitext(video_name)[0]short_name = base_name[:20]stitched_video_path = os.path.join(output_dir, short_name + '_RGBD.mp4')save_video(stitched_frames, stitched_video_path, fps=fps)# Merge audio from the input video into the stitched video using ffmpeg.temp_audio_path = stitched_video_path.replace('_RGBD.mp4', '_RGBD_audio.mp4')1 unmodified line"ffmpeg","-y","-i", stitched_video_path,"-i", input_video,"-c:v", "copy","-c:a", "aac","-map", "0:v:0",5 unmodified linesos.replace(temp_audio_path, stitched_video_path)gc.collect()torch.cuda.empty_cache()# Return the preprocessed RGB video, depth visualization, and (if created) the stitched video.return [processed_video_path, depth_vis_path, stitched_video_path]def construct_demo():with gr.Blocks(analytics_enabled=False) as demo:4 unmodified lineswith gr.Row(equal_height=True):with gr.Column(scale=1):# Video input component for file upload.input_video = gr.Video(label="Input Video")with gr.Column(scale=2):with gr.Row(equal_height=True):processed_video = gr.Video(label="Preprocessed Video", interactive=False, autoplay=True, loop=True, show_share_button=True, scale=5)depth_vis_video = gr.Video(label="Generated Depth Video", interactive=False, autoplay=True, loop=True, show_share_button=True, scale=5)stitched_video = gr.Video(label="Stitched RGBD Video", interactive=False, autoplay=True, loop=True, show_share_button=True, scale=5)with gr.Row(equal_height=True):10 unmodified lineswith gr.Column(scale=2):passgr.Examples(examples=examples,inputs=[input_video, max_len, target_fps, max_res, stitch_option, grayscale_option, convert_from_color_option, blur_slider],outputs=[processed_video, depth_vis_video, stitched_video],fn=infer_video_depth,cache_examples=False,cache_mode="lazy",)generate_btn.click(fn=infer_video_depth,inputs=[input_video, max_len, target_fps, max_res, stitch_option, grayscale_option, convert_from_color_option, blur_slider],outputs=[processed_video, depth_vis_video, stitched_video],)return demo123453 unmodified lines9101112131415161718192021222324255 unmodified lines31323334353637383940414243444546474833 unmodified lines828384858687881 unmodified line909192939495965 unmodified lines1021031041051061071081091104 unmodified lines11511611711811912012112212312410 unmodified lines135136137138139140141142143144import osimport gcimport cv2import gradio as grimport numpy as np3 unmodified linesimport sysimport spacesfrom utils.dc_utils import read_video_frames, save_videotitle = "#RGBD sbs output"description = """**Video Depth Anything** + RGBD sbs output for viewing with Looking Glass Factory displays.Please refer to our [paper](https://arxiv.org/abs/2501.12375), [project page](https://videodepthanything.github.io/), and [github](https://github.com/DepthAnything/Video-Depth-Anything) for more details."""@spaces.GPU(enable_queue=True)def stitch_rgbd_videos(processed_video: str,depth_vis_video: str,max_len: int = -1,target_fps: int = -1,max_res: int = 1280,5 unmodified linesinput_size: int = 518,):# 1. Read input video frames for inference (downscaled to max_res).frames, target_fps = read_video_frames(processed_video, max_len, target_fps, max_res)video_name = os.path.basename(processed_video)if not os.path.exists(output_dir):os.makedirs(output_dir)stitched_video_path = Noneif stitch:# For stitching: read the original video in full resolution (without downscaling).full_frames, _ = read_video_frames(processed_video, max_len, target_fps, max_res=-1)depths, _ = read_video_frames(depth_vis_video, max_len, target_fps, max_res=-1)# For each frame, create a visual depth image from the inferenced depths.d_min, d_max = depths.min(), depths.max()stitched_frames = []33 unmodified linesbase_name = os.path.splitext(video_name)[0]short_name = base_name[:20]stitched_video_path = os.path.join(output_dir, short_name + '_RGBD.mp4')save_video(stitched_frames, stitched_video_path, fps=target_fps)# Merge audio from the input video into the stitched video using ffmpeg.temp_audio_path = stitched_video_path.replace('_RGBD.mp4', '_RGBD_audio.mp4')1 unmodified line"ffmpeg","-y","-i", stitched_video_path,"-i", processed_video,"-c:v", "copy","-c:a", "aac","-map", "0:v:0",5 unmodified linesos.replace(temp_audio_path, stitched_video_path)gc.collect()# Return stitched video.return [stitched_video_path]def construct_demo():with gr.Blocks(analytics_enabled=False) as demo:4 unmodified lineswith gr.Row(equal_height=True):with gr.Column(scale=1):# Video input component for file upload.processed_video = gr.Video(label="Input Video")depth_vis_video = gr.Video(label="Generated Depth Video")with gr.Column(scale=2):with gr.Row(equal_height=True):stitched_video = gr.Video(label="Stitched RGBD Video", interactive=False, autoplay=True, loop=True, show_share_button=True, scale=5)with gr.Row(equal_height=True):10 unmodified lineswith gr.Column(scale=2):passgenerate_btn.click(fn=stitch_rgbd_videos,inputs=[processed_video, depth_vis_video, max_len, target_fps, max_res, stitch_option, grayscale_option, convert_from_color_option, blur_slider],outputs=[stitched_video],)return demo
app.py+20-83