Spaces:
Running on Zero
Running on Zero
Rawal Khirodkar commited on
Commit Β·
2940904
1
Parent(s): 5dd5fbb
Pointmap: 3-pane layout (input | depth | point cloud), drop raw .glb accordion
Browse files
app.py
CHANGED
|
@@ -145,6 +145,25 @@ def _foreground_mask(image_pil: Image.Image, target_h: int, target_w: int) -> np
|
|
| 145 |
return (out.argmax(dim=1)[0] > 0).cpu().numpy()
|
| 146 |
|
| 147 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
# -----------------------------------------------------------------------------
|
| 149 |
# Point cloud export β trimesh β .glb (much faster than Open3D .ply for Three.js)
|
| 150 |
|
|
@@ -220,12 +239,20 @@ def predict(image: Image.Image, size: str):
|
|
| 220 |
mask = _foreground_mask(image_pil, h_n, w_n)
|
| 221 |
print(f"[time] fg mask {(_t.perf_counter()-t)*1000:.0f} ms")
|
| 222 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
t = _t.perf_counter()
|
| 224 |
glb_path = _make_glb(image_pil, pointmap, mask)
|
| 225 |
print(f"[time] glb export {(_t.perf_counter()-t)*1000:.0f} ms")
|
| 226 |
|
| 227 |
print(f"[time] TOTAL {(_t.perf_counter()-t0)*1000:.0f} ms")
|
| 228 |
-
return
|
| 229 |
|
| 230 |
|
| 231 |
# -----------------------------------------------------------------------------
|
|
@@ -293,8 +320,9 @@ with gr.Blocks(title="Sapiens2 Pointmap", theme=gr.themes.Soft(), css=CUSTOM_CSS
|
|
| 293 |
|
| 294 |
with gr.Row(equal_height=True):
|
| 295 |
inp = gr.Image(label="Input", type="pil", height=640)
|
|
|
|
| 296 |
out_glb = gr.Model3D(
|
| 297 |
-
label="Point cloud
|
| 298 |
height=640,
|
| 299 |
clear_color=[0.07, 0.09, 0.13, 1.0],
|
| 300 |
display_mode="point_cloud",
|
|
@@ -313,10 +341,7 @@ with gr.Blocks(title="Sapiens2 Pointmap", theme=gr.themes.Soft(), css=CUSTOM_CSS
|
|
| 313 |
|
| 314 |
gr.Examples(examples=EXAMPLES, inputs=inp, examples_per_page=14)
|
| 315 |
|
| 316 |
-
|
| 317 |
-
out_glb_file = gr.File(label="Point cloud (.glb β open in Blender/MeshLab/web viewers)")
|
| 318 |
-
|
| 319 |
-
run.click(predict, inputs=[inp, size], outputs=[out_glb, out_glb_file])
|
| 320 |
|
| 321 |
|
| 322 |
if __name__ == "__main__":
|
|
|
|
| 145 |
return (out.argmax(dim=1)[0] > 0).cpu().numpy()
|
| 146 |
|
| 147 |
|
| 148 |
+
def _depth_to_rgb(depth: np.ndarray, mask: np.ndarray) -> np.ndarray:
|
| 149 |
+
"""Inverse-depth turbo colormap. Background pixels are left at 0 β caller overlays them."""
|
| 150 |
+
valid = np.isfinite(depth) & (depth > 1e-3) & mask
|
| 151 |
+
rgb = np.zeros((*depth.shape, 3), dtype=np.uint8)
|
| 152 |
+
if not valid.any():
|
| 153 |
+
return rgb
|
| 154 |
+
inv = np.zeros_like(depth, dtype=np.float32)
|
| 155 |
+
inv[valid] = 1.0 / depth[valid]
|
| 156 |
+
p1, p99 = np.percentile(inv[valid], [1, 99])
|
| 157 |
+
lo, hi = float(p1), float(p99)
|
| 158 |
+
if hi <= lo:
|
| 159 |
+
hi = lo + 1e-3
|
| 160 |
+
norm = ((inv - lo) / (hi - lo)).clip(0, 1)
|
| 161 |
+
grey = (norm * 255.0).astype(np.uint8)
|
| 162 |
+
color = cv2.applyColorMap(grey, cv2.COLORMAP_TURBO)[:, :, ::-1]
|
| 163 |
+
rgb[valid] = color[valid]
|
| 164 |
+
return rgb
|
| 165 |
+
|
| 166 |
+
|
| 167 |
# -----------------------------------------------------------------------------
|
| 168 |
# Point cloud export β trimesh β .glb (much faster than Open3D .ply for Three.js)
|
| 169 |
|
|
|
|
| 239 |
mask = _foreground_mask(image_pil, h_n, w_n)
|
| 240 |
print(f"[time] fg mask {(_t.perf_counter()-t)*1000:.0f} ms")
|
| 241 |
|
| 242 |
+
t = _t.perf_counter()
|
| 243 |
+
depth = pointmap[:, :, 2]
|
| 244 |
+
depth_rgb = _depth_to_rgb(depth, mask)
|
| 245 |
+
depth_rgb[~mask] = 200 # solid grey background
|
| 246 |
+
w0, h0 = image_pil.size
|
| 247 |
+
depth_pil = Image.fromarray(depth_rgb).resize((w0, h0), Image.LANCZOS)
|
| 248 |
+
print(f"[time] depth+resize {(_t.perf_counter()-t)*1000:.0f} ms")
|
| 249 |
+
|
| 250 |
t = _t.perf_counter()
|
| 251 |
glb_path = _make_glb(image_pil, pointmap, mask)
|
| 252 |
print(f"[time] glb export {(_t.perf_counter()-t)*1000:.0f} ms")
|
| 253 |
|
| 254 |
print(f"[time] TOTAL {(_t.perf_counter()-t0)*1000:.0f} ms")
|
| 255 |
+
return depth_pil, glb_path
|
| 256 |
|
| 257 |
|
| 258 |
# -----------------------------------------------------------------------------
|
|
|
|
| 320 |
|
| 321 |
with gr.Row(equal_height=True):
|
| 322 |
inp = gr.Image(label="Input", type="pil", height=640)
|
| 323 |
+
out_depth = gr.Image(label="Depth (Z)", type="pil", height=640)
|
| 324 |
out_glb = gr.Model3D(
|
| 325 |
+
label="Point cloud",
|
| 326 |
height=640,
|
| 327 |
clear_color=[0.07, 0.09, 0.13, 1.0],
|
| 328 |
display_mode="point_cloud",
|
|
|
|
| 341 |
|
| 342 |
gr.Examples(examples=EXAMPLES, inputs=inp, examples_per_page=14)
|
| 343 |
|
| 344 |
+
run.click(predict, inputs=[inp, size], outputs=[out_depth, out_glb])
|
|
|
|
|
|
|
|
|
|
| 345 |
|
| 346 |
|
| 347 |
if __name__ == "__main__":
|