JingShiang Yang commited on
Commit
3aaa09c
·
1 Parent(s): 72e1ca6

Add 16bit PNG depth map with download

Browse files
Files changed (1) hide show
  1. app.py +16 -6
app.py CHANGED
@@ -19,27 +19,37 @@ def analyze_depth(image):
19
  return None
20
 
21
  # Run inference
22
- prediction = model.inference([image], process_res=1024)
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()
 
19
  return None
20
 
21
  # Run inference
22
+ prediction = model.inference([image], process_res=1600)
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 → 最大值 (近/亮), 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
 
33
+ # 16bit PNG (0-65535)
34
+ depth_16bit = (depth_normalized * 65535).astype(np.uint16)
35
+ depth_16bit_img = Image.fromarray(depth_16bit, mode='I;16')
36
+
37
+ # 8bit 預覽圖 (0-255)
38
+ depth_8bit = (depth_normalized * 255).astype(np.uint8)
39
+ depth_8bit_img = Image.fromarray(depth_8bit, mode='L')
40
+
41
+ return depth_8bit_img, depth_16bit_img
42
 
43
  # 建立 Gradio 介面
44
  demo = gr.Interface(
45
  fn=analyze_depth,
46
  inputs=gr.Image(type="pil", label="上傳圖片"),
47
+ outputs=[
48
+ gr.Image(type="pil", label="8bit 預覽圖"),
49
+ gr.Image(type="pil", label="16bit 深度圖(可下載)")
50
+ ],
51
  title="Depth Anything V3 - 深度分析",
52
+ description="上傳圖片,輸出深度圖(白色=近,黑色=遠)。16bit PNG 保留更精準的深度值。"
53
  )
54
 
55
  demo.launch()