Spaces:
Sleeping
Sleeping
File size: 1,968 Bytes
d0a4adf b0778a4 b565be2 2f05688 e9f1a2b b0778a4 e9f1a2b b0778a4 2f05688 e9f1a2b b565be2 2f05688 e9f1a2b 2f05688 e9f1a2b 2f05688 e9f1a2b 2f05688 e9f1a2b 2f05688 e9f1a2b 2f05688 b565be2 b0778a4 |
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 |
import gradio as gr
import torch
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import cv2
from huggingface_hub import hf_hub_download
from depth_anything_v2.dpt import DepthAnythingV2
# Model loading (as before)
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
model_configs = {
'vitl': {'encoder': 'vitl', 'features': 256, 'out_channels': [256, 512, 1024, 1024]},
}
encoder = 'vitl'
model = DepthAnythingV2(**model_configs[encoder])
model_path = hf_hub_download(
repo_id="depth-anything/Depth-Anything-V2-Large",
filename=f"depth_anything_v2_{encoder}.pth",
repo_type="model"
)
state_dict = torch.load(model_path, map_location="cpu")
model.load_state_dict(state_dict)
model = model.to(DEVICE).eval()
CMAP = plt.get_cmap('Spectral_r')
def infer(image: np.ndarray):
# Run depth model
with torch.no_grad():
depth = model.infer_image(image[:, :, ::-1])
# Grayscale map (normalize to 0..255)
depth_norm = (depth - depth.min()) / (depth.max() - depth.min()) * 255.0
depth_norm = depth_norm.astype(np.uint8)
gray = Image.fromarray(depth_norm)
# Colored map
colored = (CMAP(depth_norm)[:, :, :3] * 255).astype(np.uint8)
color = Image.fromarray(colored)
# Edge map using Canny on the original image (convert to grayscale first)
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) if image.shape[2] == 3 else image
edges = cv2.Canny(image_gray, 100, 200) # threshold1/2 can be tuned
edge_img = Image.fromarray(edges)
return gray, color, edge_img
iface = gr.Interface(
fn=infer,
inputs=gr.Image(type="numpy", label="Input Image"),
outputs=[
gr.Image(label="Grayscale Depth"),
gr.Image(label="Colored Depth"),
gr.Image(label="Canny Edge Map"),
],
title="Depth Anything V2 (with Colored Output + Canny Edges)",
description="Upload an image to get depth (gray, color), plus Canny edge map."
)
iface.launch()
|