Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,12 +2,12 @@ import gradio as gr
|
|
2 |
import torch
|
3 |
import numpy as np
|
4 |
from PIL import Image
|
5 |
-
from huggingface_hub import hf_hub_download
|
6 |
import matplotlib.pyplot as plt
|
7 |
-
|
|
|
8 |
from depth_anything_v2.dpt import DepthAnythingV2
|
9 |
|
10 |
-
#
|
11 |
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
|
12 |
model_configs = {
|
13 |
'vitl': {'encoder': 'vitl', 'features': 256, 'out_channels': [256, 512, 1024, 1024]},
|
@@ -23,21 +23,28 @@ state_dict = torch.load(model_path, map_location="cpu")
|
|
23 |
model.load_state_dict(state_dict)
|
24 |
model = model.to(DEVICE).eval()
|
25 |
|
26 |
-
# Use a matplotlib colormap
|
27 |
CMAP = plt.get_cmap('Spectral_r')
|
28 |
|
29 |
def infer(image: np.ndarray):
|
30 |
-
#
|
31 |
with torch.no_grad():
|
32 |
depth = model.infer_image(image[:, :, ::-1])
|
33 |
-
|
|
|
34 |
depth_norm = (depth - depth.min()) / (depth.max() - depth.min()) * 255.0
|
35 |
depth_norm = depth_norm.astype(np.uint8)
|
36 |
gray = Image.fromarray(depth_norm)
|
37 |
-
|
|
|
38 |
colored = (CMAP(depth_norm)[:, :, :3] * 255).astype(np.uint8)
|
39 |
color = Image.fromarray(colored)
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
iface = gr.Interface(
|
43 |
fn=infer,
|
@@ -45,9 +52,10 @@ iface = gr.Interface(
|
|
45 |
outputs=[
|
46 |
gr.Image(label="Grayscale Depth"),
|
47 |
gr.Image(label="Colored Depth"),
|
|
|
48 |
],
|
49 |
-
title="Depth Anything V2 (
|
50 |
-
description="Upload an image
|
51 |
)
|
52 |
|
53 |
iface.launch()
|
|
|
2 |
import torch
|
3 |
import numpy as np
|
4 |
from PIL import Image
|
|
|
5 |
import matplotlib.pyplot as plt
|
6 |
+
import cv2
|
7 |
+
from huggingface_hub import hf_hub_download
|
8 |
from depth_anything_v2.dpt import DepthAnythingV2
|
9 |
|
10 |
+
# Model loading (as before)
|
11 |
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
|
12 |
model_configs = {
|
13 |
'vitl': {'encoder': 'vitl', 'features': 256, 'out_channels': [256, 512, 1024, 1024]},
|
|
|
23 |
model.load_state_dict(state_dict)
|
24 |
model = model.to(DEVICE).eval()
|
25 |
|
|
|
26 |
CMAP = plt.get_cmap('Spectral_r')
|
27 |
|
28 |
def infer(image: np.ndarray):
|
29 |
+
# Run depth model
|
30 |
with torch.no_grad():
|
31 |
depth = model.infer_image(image[:, :, ::-1])
|
32 |
+
|
33 |
+
# Grayscale map (normalize to 0..255)
|
34 |
depth_norm = (depth - depth.min()) / (depth.max() - depth.min()) * 255.0
|
35 |
depth_norm = depth_norm.astype(np.uint8)
|
36 |
gray = Image.fromarray(depth_norm)
|
37 |
+
|
38 |
+
# Colored map
|
39 |
colored = (CMAP(depth_norm)[:, :, :3] * 255).astype(np.uint8)
|
40 |
color = Image.fromarray(colored)
|
41 |
+
|
42 |
+
# Edge map using Canny on the original image (convert to grayscale first)
|
43 |
+
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) if image.shape[2] == 3 else image
|
44 |
+
edges = cv2.Canny(image_gray, 100, 200) # threshold1/2 can be tuned
|
45 |
+
edge_img = Image.fromarray(edges)
|
46 |
+
|
47 |
+
return gray, color, edge_img
|
48 |
|
49 |
iface = gr.Interface(
|
50 |
fn=infer,
|
|
|
52 |
outputs=[
|
53 |
gr.Image(label="Grayscale Depth"),
|
54 |
gr.Image(label="Colored Depth"),
|
55 |
+
gr.Image(label="Canny Edge Map"),
|
56 |
],
|
57 |
+
title="Depth Anything V2 (with Colored Output + Canny Edges)",
|
58 |
+
description="Upload an image to get depth (gray, color), plus Canny edge map."
|
59 |
)
|
60 |
|
61 |
iface.launch()
|