JingShiang Yang commited on
Commit
ed7e522
·
1 Parent(s): a4bb623

Add grayscale depth map output

Browse files
Files changed (1) hide show
  1. app.py +16 -15
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
- 分析圖片深度並回傳 intrinsics
15
  """
16
  if image is None:
17
- return "請上傳圖片"
18
 
19
  # Run inference
20
  prediction = model.inference([image])
21
 
22
- # 取得 intrinsics
23
- intrinsics = prediction.intrinsics[0] # [3, 3] 矩陣
 
24
 
25
- # 轉換為 numpy 並格式化輸出
26
- intrinsics_np = intrinsics.cpu().numpy() if torch.is_tensor(intrinsics) else intrinsics
 
 
 
27
 
28
- result = "Camera Intrinsics (3x3):\n"
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.Textbox(label="Intrinsics 結果", lines=10),
40
- title="Depth Anything V3 - Intrinsics 測試",
41
- description="上傳一張圖片,系統將回傳相機內參 (intrinsics)"
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()