Insta360-Research commited on
Commit
2ba6c81
·
verified ·
1 Parent(s): 77df1b9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -10
app.py CHANGED
@@ -12,7 +12,6 @@ try:
12
  import spaces
13
  gpu_decorator = spaces.GPU
14
  except Exception:
15
- # 本地环境没有 spaces 时兜底
16
  gpu_decorator = lambda f: f
17
 
18
  # ================== 工程路径 ==================
@@ -35,10 +34,10 @@ import matplotlib
35
  def colorize_depth_fixed(depth_u8: np.ndarray, cmap: str = "Spectral") -> np.ndarray:
36
  """
37
  depth_u8: uint8, 0~255
38
- return: RGB uint8, 颜色全局一致
39
  """
40
- disp = depth_u8.astype(np.float32) / 255.0 # 固定映射
41
- colored = matplotlib.colormaps[cmap](disp)[..., :3] # RGB float
42
  colored = (colored * 255).astype(np.uint8)
43
  return np.ascontiguousarray(colored)
44
 
@@ -79,15 +78,15 @@ def load_model(config_path: str):
79
  # ================== 启动时加载一次模型 ==================
80
  model = load_model(CONFIG_PATH)
81
 
82
- # ================== 推理函数(ZeroGPU 必须) ==================
83
  @gpu_decorator
84
  def predict(img_rgb: np.ndarray):
85
  if img_rgb is None:
86
- return None, None
87
 
88
  import torch
89
 
90
- # 输入处理
91
  img = img_rgb.astype(np.float32) / 255.0
92
  tensor = torch.from_numpy(
93
  img.transpose(2, 0, 1)
@@ -105,14 +104,18 @@ def predict(img_rgb: np.ndarray):
105
  else:
106
  pred = outputs[0].cpu().squeeze().numpy()
107
 
108
- # ================== 固定尺度(假设模型输出 0~1) ==================
109
  pred = pred.astype(np.float32)
110
  pred_clip = np.clip(pred, 0.0, 1.0)
111
 
112
  depth_gray = (pred_clip * 255).astype(np.uint8)
113
  depth_color_rgb = colorize_depth_fixed(depth_gray, cmap="Spectral")
114
 
115
- return depth_color_rgb, depth_gray
 
 
 
 
116
 
117
  # ================== Gradio UI ==================
118
  demo = gr.Interface(
@@ -121,9 +124,10 @@ demo = gr.Interface(
121
  outputs=[
122
  gr.Image(type="numpy", label="Depth (Color)"),
123
  gr.Image(type="numpy", label="Depth (Gray)"),
 
124
  ],
125
  title="DAP Depth Prediction Demo",
126
- description="Upload an image and get depth prediction.",
127
  )
128
 
129
  demo.launch(
 
12
  import spaces
13
  gpu_decorator = spaces.GPU
14
  except Exception:
 
15
  gpu_decorator = lambda f: f
16
 
17
  # ================== 工程路径 ==================
 
34
  def colorize_depth_fixed(depth_u8: np.ndarray, cmap: str = "Spectral") -> np.ndarray:
35
  """
36
  depth_u8: uint8, 0~255
37
+ return: RGB uint8
38
  """
39
+ disp = depth_u8.astype(np.float32) / 255.0
40
+ colored = matplotlib.colormaps[cmap](disp)[..., :3]
41
  colored = (colored * 255).astype(np.uint8)
42
  return np.ascontiguousarray(colored)
43
 
 
78
  # ================== 启动时加载一次模型 ==================
79
  model = load_model(CONFIG_PATH)
80
 
81
+ # ================== 推理函数 ==================
82
  @gpu_decorator
83
  def predict(img_rgb: np.ndarray):
84
  if img_rgb is None:
85
+ return None, None, None
86
 
87
  import torch
88
 
89
+ # -------- 输入处理 --------
90
  img = img_rgb.astype(np.float32) / 255.0
91
  tensor = torch.from_numpy(
92
  img.transpose(2, 0, 1)
 
104
  else:
105
  pred = outputs[0].cpu().squeeze().numpy()
106
 
107
+ # -------- 深度后处理 --------
108
  pred = pred.astype(np.float32)
109
  pred_clip = np.clip(pred, 0.0, 1.0)
110
 
111
  depth_gray = (pred_clip * 255).astype(np.uint8)
112
  depth_color_rgb = colorize_depth_fixed(depth_gray, cmap="Spectral")
113
 
114
+ # -------- 保存 npy --------
115
+ npy_path = "/tmp/depth.npy"
116
+ np.save(npy_path, pred) # 保存原始 float32 深度
117
+
118
+ return depth_color_rgb, depth_gray, npy_path
119
 
120
  # ================== Gradio UI ==================
121
  demo = gr.Interface(
 
124
  outputs=[
125
  gr.Image(type="numpy", label="Depth (Color)"),
126
  gr.Image(type="numpy", label="Depth (Gray)"),
127
+ gr.File(label="Depth (.npy)"),
128
  ],
129
  title="DAP Depth Prediction Demo",
130
+ description="Upload an image and get depth prediction (color / gray / npy).",
131
  )
132
 
133
  demo.launch(