import gradio as gr UPSAMPLER_THEME = gr.themes.Soft( primary_hue=gr.themes.colors.indigo, secondary_hue=gr.themes.colors.purple, neutral_hue=gr.themes.colors.slate, ).set( button_primary_background_fill="linear-gradient(135deg, #6366f1, #a855f7)", button_primary_background_fill_hover="linear-gradient(135deg, #5457e5, #9333ea)", button_primary_text_color="#ffffff", button_primary_border_color="*primary_500", ) UPSAMPLER_CSS = """ footer{display:none !important} .gradio-container{max-width:1000px !important; margin:0 auto !important} h1,h2,h3{font-family:system-ui,-apple-system,'Segoe UI',sans-serif} """ import cv2 import matplotlib import numpy as np import os from PIL import Image import spaces import torch import tempfile from gradio_imageslider import ImageSlider from huggingface_hub import hf_hub_download from depth_anything_v2.dpt import DepthAnythingV2 css = """ #img-display-container { max-height: 100vh; } #img-display-input { max-height: 80vh; } #img-display-output { max-height: 80vh; } #download { height: 62px; } """ DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu' model_configs = { 'vits': {'encoder': 'vits', 'features': 64, 'out_channels': [48, 96, 192, 384]}, 'vitb': {'encoder': 'vitb', 'features': 128, 'out_channels': [96, 192, 384, 768]}, 'vitl': {'encoder': 'vitl', 'features': 256, 'out_channels': [256, 512, 1024, 1024]}, 'vitg': {'encoder': 'vitg', 'features': 384, 'out_channels': [1536, 1536, 1536, 1536]} } encoder2name = { 'vits': 'Small', 'vitb': 'Base', 'vitl': 'Large', 'vitg': 'Giant', # we are undergoing company review procedures to release our giant model checkpoint } encoder = 'vitl' model_name = encoder2name[encoder] model = DepthAnythingV2(**model_configs[encoder]) filepath = hf_hub_download(repo_id=f"depth-anything/Depth-Anything-V2-{model_name}", filename=f"depth_anything_v2_{encoder}.pth", repo_type="model") state_dict = torch.load(filepath, map_location="cpu") model.load_state_dict(state_dict) model = model.to(DEVICE).eval() header = """
Turn any photo into a detailed depth map with Depth Anything V2.
Depth Anything V2 is a state-of-the-art monocular depth estimation model that turns a single photo into a detailed depth map online, no stereo pair or LiDAR required. The grayscale depth maps it produces are used for 3D parallax effects, depth-of-field and bokeh simulation, relighting, ControlNet conditioning, and robotics prototyping.
Maintained by Upsampler. Check out the free depth map generator, no sign-up required.