Spaces:
Running
on
Zero
Running
on
Zero
File size: 3,175 Bytes
7c27268 7a6bafa 7c27268 2b8834a 050c6f5 7c27268 02d2686 de5bd2a 02d2686 de5bd2a 48d0430 7c27268 c5f38fa 7a6bafa 82fa24a 7a6bafa 050c6f5 7a6bafa 050c6f5 f6e95ff 7c27268 6ceac94 7c27268 055cd1b 7c27268 bc1d278 7c27268 055cd1b 7c27268 6ceac94 7c27268 2c36086 eabf5af 7c27268 ad367d9 055cd1b 5e51d3c f165604 7c27268 d66adea 7c27268 055cd1b 45a5139 7798364 7c27268 055cd1b 7c27268 055cd1b |
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 |
import gradio as gr
import cv2
import matplotlib
import numpy as np
import os
from PIL import Image
import mmap
import json
import spaces
import torch
import tempfile
from gradio_imageslider import ImageSlider
from huggingface_hub import hf_hub_download
from safetensors.torch import load_file
from transformers import pipeline
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="depth-anything/Depth-Anything-V2-Metric-Indoor-Large-hf", filename="model.safetensors", repo_type="model")
# Convert to PyTorch tensor
#state_dict = torch.load(filepath, map_location="cpu", weights_only=True)
#state_dict = load_file(filepath)
#model.load_state_dict(state_dict)
#model = model.to(DEVICE).eval()
pipe = pipeline(task="depth-estimation", model="depth-anything/Depth-Anything-V2-Metric-Outdoor-Large-hf", device=DEVICE)
#pipe = pipeline(task="depth-estimation", model="LiheYoung/Depth-Anything", device=DEVICE)
title = "# Depth Anything V2"
description = """Official demo for **Depth Anything V2**.
Please refer to our [paper](https://arxiv.org/abs/2406.09414), [project page](https://depth-anything-v2.github.io), and [github](https://github.com/DepthAnything/Depth-Anything-V2) for more details."""
@spaces.GPU
def predict_depth(image):
return pipe(image)
with gr.Blocks(css=css) as demo:
gr.Markdown(title)
gr.Markdown(description)
gr.Markdown("### Depth Prediction demo")
with gr.Row():
input_image = gr.Image(label="Input Image", type="pil" ,elem_id='img-display-input')
depth_image_slider = gr.Image(label="Depth Map with Slider View", type="pil", elem_id='img-display-output')
submit = gr.Button(value="Compute Depth")
def on_submit(image):
original_image = image.copy()
result = predict_depth(image)
return result["depth"]
submit.click(on_submit, inputs=[input_image], outputs=[depth_image_slider])
example_files = os.listdir('assets/examples')
example_files.sort()
example_files = [os.path.join('assets/examples', filename) for filename in example_files]
examples = gr.Examples(examples=example_files, inputs=[input_image], outputs=[depth_image_slider], fn=on_submit)
if __name__ == '__main__':
demo.queue().launch(share=True)
|