Vanyadoing commited on
Commit
2f05688
·
verified ·
1 Parent(s): 307213f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -6
app.py CHANGED
@@ -1,9 +1,13 @@
1
  import gradio as gr
2
  import torch
3
  import numpy as np
 
4
  from huggingface_hub import hf_hub_download
 
 
5
  from depth_anything_v2.dpt import DepthAnythingV2
6
 
 
7
  DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
8
  model_configs = {
9
  'vitl': {'encoder': 'vitl', 'features': 256, 'out_channels': [256, 512, 1024, 1024]},
@@ -19,12 +23,31 @@ state_dict = torch.load(model_path, map_location="cpu")
19
  model.load_state_dict(state_dict)
20
  model = model.to(DEVICE).eval()
21
 
22
- def infer(img):
 
 
 
 
23
  with torch.no_grad():
24
- depth = model.infer_image(img[:, :, ::-1]) # BGR to RGB if needed
25
- # Normalize to 0-255 and convert to uint8
26
- depth_norm = (depth - depth.min()) / (depth.max() - depth.min()) * 255.0
27
- return depth_norm.astype(np.uint8)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
- iface = gr.Interface(fn=infer, inputs=gr.Image(type="numpy"), outputs=gr.Image())
30
  iface.launch()
 
1
  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
+ # Load model 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
+ # Use a matplotlib colormap
27
+ CMAP = plt.get_cmap('Spectral_r')
28
+
29
+ def infer(image: np.ndarray):
30
+ # 1. Run the model (BGR to RGB if needed)
31
  with torch.no_grad():
32
+ depth = model.infer_image(image[:, :, ::-1])
33
+ # 2. 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
+ # 3. Color map
38
+ colored = (CMAP(depth_norm)[:, :, :3] * 255).astype(np.uint8)
39
+ color = Image.fromarray(colored)
40
+ return gray, color
41
+
42
+ iface = gr.Interface(
43
+ fn=infer,
44
+ inputs=gr.Image(type="numpy", label="Input Image"),
45
+ outputs=[
46
+ gr.Image(label="Grayscale Depth"),
47
+ gr.Image(label="Colored Depth"),
48
+ ],
49
+ title="Depth Anything V2 (Minimal, with Colored Output)",
50
+ description="Upload an image, get depth as grayscale and colored."
51
+ )
52
 
 
53
  iface.launch()