Spaces:
Running on Zero
Running on Zero
File size: 5,563 Bytes
7c27268 924193b 7c27268 02d2686 de5bd2a 02d2686 de5bd2a 48d0430 7c27268 1afd902 7c27268 c36a864 59967b8 ee764cf c36a864 1bbfe5b 59967b8 7c27268 ad22cdb 7c27268 187c6c8 7c27268 924193b 59967b8 7c27268 c36a864 7c27268 187c6c8 7c27268 187c6c8 7c27268 59967b8 ee764cf 7c27268 c36a864 | 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 | 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 = """<div style="max-width:760px;margin:0 auto;text-align:center;padding:20px 16px 2px;font-family:system-ui,-apple-system,'Segoe UI',sans-serif">
<h1 style="font-size:1.7rem;font-weight:700;margin:0 0 6px;letter-spacing:-.02em">Depth Anything V2</h1>
<p style="font-size:1rem;line-height:1.5;opacity:.6;margin:0">Turn any photo into a detailed depth map with Depth Anything V2.</p>
</div>"""
footer = """<div style="max-width:640px;margin:2rem auto .4rem;text-align:center;font-family:system-ui,-apple-system,'Segoe UI',sans-serif">
<p style="font-size:.85rem;line-height:1.6;opacity:.5;margin:0 0 10px">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.</p>
<p style="font-size:.85rem;line-height:1.6;opacity:.65;margin:0">Maintained by <a href="https://upsampler.com" target="_blank" rel="noopener" style="color:#8b7cf6;font-weight:600;text-decoration:none">Upsampler</a>. Check out the <a href="https://upsampler.com/free-depth-map-generator-no-signup" target="_blank" rel="noopener" style="color:#8b7cf6;font-weight:600;text-decoration:none">free depth map generator</a>, no sign-up required.</p>
</div>"""
# Single forward pass of the depth model: ~1-3s on ZeroGPU. Request a tight
# duration so anonymous visitors with small quotas aren't rejected up front
# (ZeroGPU checks the requested duration against remaining quota) and the
# task gets higher queue priority.
@spaces.GPU(duration=12)
def predict_depth(image):
return model.infer_image(image)
with gr.Blocks(theme=UPSAMPLER_THEME, css=UPSAMPLER_CSS + "\n" + css) as demo:
gr.HTML(header)
with gr.Row():
with gr.Column():
input_image = gr.Image(label="Input Image", type='numpy', elem_id='img-display-input')
submit = gr.Button(value="Compute Depth", variant="primary")
with gr.Column():
depth_image_slider = ImageSlider(label="Depth Map with Slider View", elem_id='img-display-output', position=0.5)
gray_depth_file = gr.File(label="Grayscale depth map", elem_id="download",)
raw_file = gr.File(label="16-bit raw output (can be considered as disparity)", elem_id="download",)
cmap = matplotlib.colormaps.get_cmap('Spectral_r')
def on_submit(image):
original_image = image.copy()
h, w = image.shape[:2]
depth = predict_depth(image[:, :, ::-1])
raw_depth = Image.fromarray(depth.astype('uint16'))
tmp_raw_depth = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
raw_depth.save(tmp_raw_depth.name)
depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255.0
depth = depth.astype(np.uint8)
colored_depth = (cmap(depth)[:, :, :3] * 255).astype(np.uint8)
gray_depth = Image.fromarray(depth)
tmp_gray_depth = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
gray_depth.save(tmp_gray_depth.name)
return [(original_image, colored_depth), tmp_gray_depth.name, tmp_raw_depth.name]
submit.click(on_submit, inputs=[input_image], outputs=[depth_image_slider, gray_depth_file, raw_file])
gr.HTML(footer)
if __name__ == '__main__':
demo.queue().launch(ssr_mode=False, show_error=True)
|