|
|
import gradio as gr |
|
|
import matplotlib |
|
|
import numpy as np |
|
|
from PIL import Image |
|
|
import spaces |
|
|
import torch |
|
|
import tempfile |
|
|
from gradio_imageslider import ImageSlider |
|
|
from huggingface_hub import hf_hub_download |
|
|
import yaml |
|
|
import os |
|
|
|
|
|
from networks.models import * |
|
|
from depth_anything_utils import Resize, NormalizeImage, PrepareForNet |
|
|
|
|
|
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' |
|
|
config = 'panda_large.yaml' |
|
|
with open(config, 'r') as f: |
|
|
config = yaml.load(f, Loader=yaml.FullLoader) |
|
|
hf_weight = hf_hub_download(repo_id=f"ZidongC/PanDA", filename=f"panda_large.pth", repo_type="model") |
|
|
state_dict = torch.load(hf_weight, map_location="cpu") |
|
|
new_state_dict = {} |
|
|
for key, value in state_dict.items(): |
|
|
new_key = key[7:] if key.startswith('module.') else key |
|
|
new_state_dict[new_key] = value |
|
|
model = make(config['model']) |
|
|
|
|
|
|
|
|
model_state_dict = model.state_dict() |
|
|
model.load_state_dict({k: v for k, v in new_state_dict.items() if k in model_state_dict}) |
|
|
model = model.to(DEVICE).eval() |
|
|
|
|
|
title = "# PanDA" |
|
|
description = """Official demo for **PanDA**. |
|
|
Please refer to our [github](https://github.com/caozidong/PanDA) for more details.""" |
|
|
|
|
|
@spaces.GPU |
|
|
def predict_depth(image): |
|
|
return model.infer_image(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='numpy', elem_id='img-display-input') |
|
|
depth_image_slider = ImageSlider(label="Depth Map with Slider View", elem_id='img-display-output', position=0.5) |
|
|
submit = gr.Button(value="Compute Depth") |
|
|
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]) |
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
demo.queue().launch(share=True) |