Spaces:
Running
on
Zero
Running
on
Zero
JingShiang Yang
Claude
commited on
Commit
·
9922e21
1
Parent(s):
35e8a5e
Add camera parameters and depth range to output
Browse filesExtend the Gradio interface to output depth min/max values, camera extrinsics (OpenCV w2c/COLMAP format), and camera intrinsics alongside the existing 8-bit and 16-bit depth maps. This provides complete depth and camera information for downstream 3D reconstruction tasks.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
app.py
CHANGED
|
@@ -16,10 +16,10 @@ model = model.to(device=device)
|
|
| 16 |
@spaces.GPU
|
| 17 |
def analyze_depth(image):
|
| 18 |
"""
|
| 19 |
-
|
| 20 |
"""
|
| 21 |
if image is None:
|
| 22 |
-
return None
|
| 23 |
|
| 24 |
# Run inference
|
| 25 |
prediction = model.inference([image], process_res=1600)
|
|
@@ -29,8 +29,8 @@ def analyze_depth(image):
|
|
| 29 |
depth_np = depth.cpu().numpy() if torch.is_tensor(depth) else depth
|
| 30 |
|
| 31 |
# 正規化:depthMin → 最大值 (近/亮), depthMax → 0 (遠/暗)
|
| 32 |
-
depth_min = depth_np.min()
|
| 33 |
-
depth_max = depth_np.max()
|
| 34 |
depth_normalized = (depth_max - depth_np) / (depth_max - depth_min)
|
| 35 |
|
| 36 |
# 8bit 預覽圖 (0-255)
|
|
@@ -48,7 +48,32 @@ def analyze_depth(image):
|
|
| 48 |
# 使用 imageio 儲存 16bit PNG
|
| 49 |
imageio.imwrite(temp_path, depth_16bit)
|
| 50 |
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
# 建立 Gradio 介面
|
| 54 |
demo = gr.Interface(
|
|
@@ -56,10 +81,13 @@ demo = gr.Interface(
|
|
| 56 |
inputs=gr.Image(type="pil", label="上傳圖片"),
|
| 57 |
outputs=[
|
| 58 |
gr.Image(type="pil", label="8bit 預覽圖"),
|
| 59 |
-
gr.File(label="16bit 深度圖 (PNG)")
|
|
|
|
|
|
|
|
|
|
| 60 |
],
|
| 61 |
title="Depth Anything V3 - 深度分析",
|
| 62 |
-
description="
|
| 63 |
)
|
| 64 |
|
| 65 |
demo.launch()
|
|
|
|
| 16 |
@spaces.GPU
|
| 17 |
def analyze_depth(image):
|
| 18 |
"""
|
| 19 |
+
分析圖片深度並輸出灰階深度圖及相機參數
|
| 20 |
"""
|
| 21 |
if image is None:
|
| 22 |
+
return None, None, None, None, None
|
| 23 |
|
| 24 |
# Run inference
|
| 25 |
prediction = model.inference([image], process_res=1600)
|
|
|
|
| 29 |
depth_np = depth.cpu().numpy() if torch.is_tensor(depth) else depth
|
| 30 |
|
| 31 |
# 正規化:depthMin → 最大值 (近/亮), depthMax → 0 (遠/暗)
|
| 32 |
+
depth_min = float(depth_np.min())
|
| 33 |
+
depth_max = float(depth_np.max())
|
| 34 |
depth_normalized = (depth_max - depth_np) / (depth_max - depth_min)
|
| 35 |
|
| 36 |
# 8bit 預覽圖 (0-255)
|
|
|
|
| 48 |
# 使用 imageio 儲存 16bit PNG
|
| 49 |
imageio.imwrite(temp_path, depth_16bit)
|
| 50 |
|
| 51 |
+
# 取得相機參數
|
| 52 |
+
extrinsics = prediction.extrinsics[0] # [3, 4]
|
| 53 |
+
intrinsics = prediction.intrinsics[0] # [3, 3]
|
| 54 |
+
|
| 55 |
+
# 轉換為 numpy array 並格式化為可讀的 JSON
|
| 56 |
+
extrinsics_np = extrinsics.cpu().numpy() if torch.is_tensor(extrinsics) else extrinsics
|
| 57 |
+
intrinsics_np = intrinsics.cpu().numpy() if torch.is_tensor(intrinsics) else intrinsics
|
| 58 |
+
|
| 59 |
+
# 準備輸出資料
|
| 60 |
+
depth_info = {
|
| 61 |
+
"depth_min": depth_min,
|
| 62 |
+
"depth_max": depth_max
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
extrinsics_info = {
|
| 66 |
+
"format": "OpenCV w2c / COLMAP",
|
| 67 |
+
"shape": "[3, 4]",
|
| 68 |
+
"matrix": extrinsics_np.tolist()
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
intrinsics_info = {
|
| 72 |
+
"shape": "[3, 3]",
|
| 73 |
+
"matrix": intrinsics_np.tolist()
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
return depth_8bit_img, temp_path, depth_info, extrinsics_info, intrinsics_info
|
| 77 |
|
| 78 |
# 建立 Gradio 介面
|
| 79 |
demo = gr.Interface(
|
|
|
|
| 81 |
inputs=gr.Image(type="pil", label="上傳圖片"),
|
| 82 |
outputs=[
|
| 83 |
gr.Image(type="pil", label="8bit 預覽圖"),
|
| 84 |
+
gr.File(label="16bit 深度圖 (PNG)"),
|
| 85 |
+
gr.JSON(label="深度範圍 (Depth Min/Max)"),
|
| 86 |
+
gr.JSON(label="相機外參 (Extrinsics)"),
|
| 87 |
+
gr.JSON(label="相機內參 (Intrinsics)")
|
| 88 |
],
|
| 89 |
title="Depth Anything V3 - 深度分析",
|
| 90 |
+
description="上傳圖片,輸出深度圖(白色=近,黑色=遠)及相機參數。16bit PNG 保留更精準的深度值 (0-65535)。"
|
| 91 |
)
|
| 92 |
|
| 93 |
demo.launch()
|