Spaces:
Running
on
Zero
Running
on
Zero
JingShiang Yang
commited on
Commit
·
ed7e522
1
Parent(s):
a4bb623
Add grayscale depth map output
Browse files
app.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
| 3 |
import torch
|
|
|
|
|
|
|
| 4 |
from depth_anything_3.api import DepthAnything3
|
| 5 |
|
| 6 |
# Load model
|
|
@@ -11,34 +13,33 @@ model = model.to(device=device)
|
|
| 11 |
@spaces.GPU
|
| 12 |
def analyze_depth(image):
|
| 13 |
"""
|
| 14 |
-
|
| 15 |
"""
|
| 16 |
if image is None:
|
| 17 |
-
return
|
| 18 |
|
| 19 |
# Run inference
|
| 20 |
prediction = model.inference([image])
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
result += f"[{intrinsics_np[0, 0]:.4f}, {intrinsics_np[0, 1]:.4f}, {intrinsics_np[0, 2]:.4f}]\n"
|
| 30 |
-
result += f"[{intrinsics_np[1, 0]:.4f}, {intrinsics_np[1, 1]:.4f}, {intrinsics_np[1, 2]:.4f}]\n"
|
| 31 |
-
result += f"[{intrinsics_np[2, 0]:.4f}, {intrinsics_np[2, 1]:.4f}, {intrinsics_np[2, 2]:.4f}]"
|
| 32 |
-
|
| 33 |
-
return result
|
| 34 |
|
| 35 |
# 建立 Gradio 介面
|
| 36 |
demo = gr.Interface(
|
| 37 |
fn=analyze_depth,
|
| 38 |
inputs=gr.Image(type="pil", label="上傳圖片"),
|
| 39 |
-
outputs=gr.
|
| 40 |
-
title="Depth Anything V3 -
|
| 41 |
-
description="
|
| 42 |
)
|
| 43 |
|
| 44 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
| 3 |
import torch
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
from depth_anything_3.api import DepthAnything3
|
| 7 |
|
| 8 |
# Load model
|
|
|
|
| 13 |
@spaces.GPU
|
| 14 |
def analyze_depth(image):
|
| 15 |
"""
|
| 16 |
+
分析圖片深度並輸出灰階深度圖
|
| 17 |
"""
|
| 18 |
if image is None:
|
| 19 |
+
return None
|
| 20 |
|
| 21 |
# Run inference
|
| 22 |
prediction = model.inference([image])
|
| 23 |
|
| 24 |
+
# 取得深度圖 [H, W]
|
| 25 |
+
depth = prediction.depth[0]
|
| 26 |
+
depth_np = depth.cpu().numpy() if torch.is_tensor(depth) else depth
|
| 27 |
|
| 28 |
+
# 正規化:depthMin → 255 (近/亮), depthMax → 0 (遠/暗)
|
| 29 |
+
depth_min = depth_np.min()
|
| 30 |
+
depth_max = depth_np.max()
|
| 31 |
+
depth_normalized = (depth_max - depth_np) / (depth_max - depth_min)
|
| 32 |
+
depth_gray = (depth_normalized * 255).astype(np.uint8)
|
| 33 |
|
| 34 |
+
return Image.fromarray(depth_gray, mode='L')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
# 建立 Gradio 介面
|
| 37 |
demo = gr.Interface(
|
| 38 |
fn=analyze_depth,
|
| 39 |
inputs=gr.Image(type="pil", label="上傳圖片"),
|
| 40 |
+
outputs=gr.Image(type="pil", label="灰階深度圖"),
|
| 41 |
+
title="Depth Anything V3 - 深度分析",
|
| 42 |
+
description="上傳圖片,輸出灰階深度圖(白色=近,黑色=遠)"
|
| 43 |
)
|
| 44 |
|
| 45 |
demo.launch()
|