Spaces:
Running
Running
Focal-free Depth Pro 2 ONNX in the browser
Browse files- README.md +10 -5
- index.html +213 -18
README.md
CHANGED
|
@@ -1,10 +1,15 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: static
|
|
|
|
| 7 |
pinned: false
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Depth Pro 2 ONNX (unofficial)
|
| 3 |
+
emoji: 📏
|
| 4 |
+
colorFrom: indigo
|
| 5 |
+
colorTo: red
|
| 6 |
sdk: static
|
| 7 |
+
app_file: index.html
|
| 8 |
pinned: false
|
| 9 |
+
license: apple-amlr
|
| 10 |
+
short_description: Metric depth in the browser from a photo and a focal length
|
| 11 |
+
models:
|
| 12 |
+
- christianengel/DepthPro2-S-ONNX
|
| 13 |
---
|
| 14 |
|
| 15 |
+
Focal-free Depth Pro 2 S (FP16 ONNX) in the browser with ONNX Runtime Web: WebGPU, else WASM. Give a photo and its field of view or `fx`; hover the depth map for meters. Unofficial. Weights are for non-commercial research use only ([license](https://huggingface.co/christianengel/DepthPro2-S-ONNX/blob/main/LICENSE)). Code: [github.com/protortyp/depthpro2-onnx](https://github.com/protortyp/depthpro2-onnx).
|
index.html
CHANGED
|
@@ -1,19 +1,214 @@
|
|
| 1 |
-
<!
|
| 2 |
-
<html>
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
</html>
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="utf-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
| 6 |
+
<title>Depth Pro 2 ONNX, focal-free (unofficial)</title>
|
| 7 |
+
<style>
|
| 8 |
+
:root { color-scheme: dark; --bg: #1a1a19; --panel: #232322; --text: #f0efec; --muted: #c3c2b7;
|
| 9 |
+
--line: #3a3a37; --accent: #3987e5; }
|
| 10 |
+
* { box-sizing: border-box; }
|
| 11 |
+
body { margin: 0; background: var(--bg); color: var(--text);
|
| 12 |
+
font: 15px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif; }
|
| 13 |
+
main { max-width: 1120px; margin: 0 auto; padding: 28px 20px 48px; }
|
| 14 |
+
h1 { font-size: 24px; margin: 0 0 6px; }
|
| 15 |
+
p.lead { color: var(--muted); margin: 0 0 4px; }
|
| 16 |
+
a { color: var(--accent); }
|
| 17 |
+
.controls { display: flex; flex-wrap: wrap; gap: 12px 20px; align-items: end; margin: 22px 0;
|
| 18 |
+
padding: 16px; background: var(--panel); border-radius: 8px; }
|
| 19 |
+
label { display: flex; flex-direction: column; gap: 4px; color: var(--muted); font-size: 13px; }
|
| 20 |
+
input, select, button { font: inherit; color: var(--text); background: var(--bg);
|
| 21 |
+
border: 1px solid var(--line); border-radius: 6px; padding: 7px 10px; }
|
| 22 |
+
input[type=number] { width: 110px; }
|
| 23 |
+
button { background: var(--accent); border-color: var(--accent); color: #fff; cursor: pointer; }
|
| 24 |
+
button:disabled { opacity: .5; cursor: default; }
|
| 25 |
+
.views { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
|
| 26 |
+
figure { margin: 0; }
|
| 27 |
+
canvas { width: 100%; height: auto; background: var(--panel); border-radius: 8px; display: block; }
|
| 28 |
+
figcaption { color: var(--muted); font-size: 13px; margin-top: 6px; }
|
| 29 |
+
#status { min-height: 1.5em; margin: 14px 0 0; color: var(--muted); }
|
| 30 |
+
#readout { font-variant-numeric: tabular-nums; }
|
| 31 |
+
.note { color: var(--muted); font-size: 13px; margin-top: 28px; }
|
| 32 |
+
@media (max-width: 760px) { .views { grid-template-columns: 1fr; } }
|
| 33 |
+
</style>
|
| 34 |
+
</head>
|
| 35 |
+
<body>
|
| 36 |
+
<main>
|
| 37 |
+
<h1>Depth Pro 2 ONNX, focal-free (unofficial)</h1>
|
| 38 |
+
<p class="lead">Metric depth from one photo and a focal length you already know, running in your
|
| 39 |
+
browser with ONNX Runtime Web. Nothing is uploaded.</p>
|
| 40 |
+
<p class="lead"><a href="https://huggingface.co/christianengel/DepthPro2-S-ONNX">Model</a> ·
|
| 41 |
+
<a href="https://github.com/protortyp/depthpro2-onnx">Code, ROS 2 node, TensorRT</a></p>
|
| 42 |
+
|
| 43 |
+
<div class="controls">
|
| 44 |
+
<label>Photo <input type="file" id="file" accept="image/*"></label>
|
| 45 |
+
<label>Focal length as
|
| 46 |
+
<select id="focalMode">
|
| 47 |
+
<option value="fov">horizontal FOV (degrees)</option>
|
| 48 |
+
<option value="fx">fx in pixels of the photo</option>
|
| 49 |
+
</select>
|
| 50 |
+
</label>
|
| 51 |
+
<label>Value <input type="number" id="focal" value="65.6" step="0.1" min="0.1"></label>
|
| 52 |
+
<label>Size
|
| 53 |
+
<select id="size">
|
| 54 |
+
<option value="480x640">480x640 (4:3)</option>
|
| 55 |
+
<option value="384x672">384x672 (16:9)</option>
|
| 56 |
+
</select>
|
| 57 |
+
</label>
|
| 58 |
+
<button id="run" disabled>Estimate depth</button>
|
| 59 |
+
</div>
|
| 60 |
+
<div id="status">Loading ONNX Runtime…</div>
|
| 61 |
+
|
| 62 |
+
<div class="views">
|
| 63 |
+
<figure><canvas id="input" width="640" height="480"></canvas>
|
| 64 |
+
<figcaption>Model input: scaled to cover, center-cropped</figcaption></figure>
|
| 65 |
+
<figure><canvas id="depth" width="640" height="480"></canvas>
|
| 66 |
+
<figcaption>Depth, red near, blue far. Hover for meters. <span id="readout"></span></figcaption></figure>
|
| 67 |
+
</div>
|
| 68 |
+
|
| 69 |
+
<p class="note">DepthPro2-S (24M parameters), FP16 graph, focal-length network removed.
|
| 70 |
+
Weights: Apple Machine Learning Research Model License, <strong>non-commercial research use
|
| 71 |
+
only</strong>. Not affiliated with or endorsed by Apple. Example photo: Apple's
|
| 72 |
+
<a href="https://github.com/apple-aiml-research/ml-depth-pro-2">ml-depth-pro-2</a> sample, loaded from GitHub.</p>
|
| 73 |
+
</main>
|
| 74 |
+
|
| 75 |
+
<script src="https://cdn.jsdelivr.net/npm/onnxruntime-web@1.23.2/dist/ort.webgpu.min.js"></script>
|
| 76 |
+
<script>
|
| 77 |
+
const REPO = "https://huggingface.co/christianengel/DepthPro2-S-ONNX/resolve/main/";
|
| 78 |
+
const EXAMPLE = "https://raw.githubusercontent.com/apple-aiml-research/ml-depth-pro-2/aa389cb/assets/example.jpg";
|
| 79 |
+
const $ = (id) => document.getElementById(id);
|
| 80 |
+
const sessions = {};
|
| 81 |
+
let image = null, depth = null, backend = null;
|
| 82 |
+
|
| 83 |
+
ort.env.wasm.wasmPaths = "https://cdn.jsdelivr.net/npm/onnxruntime-web@1.23.2/dist/";
|
| 84 |
+
|
| 85 |
+
function status(text) { $("status").textContent = text; }
|
| 86 |
+
|
| 87 |
+
async function session(size) {
|
| 88 |
+
if (sessions[size]) return sessions[size];
|
| 89 |
+
// config.json first: the Hub counts a model download by this file.
|
| 90 |
+
await fetch(REPO + "config.json");
|
| 91 |
+
const file = size === "480x640" ? "onnx/model_fp16.onnx" : `onnx/${size}/model_fp16.onnx`;
|
| 92 |
+
status(`Downloading ${file} (51 MB, cached by the browser afterwards)…`);
|
| 93 |
+
const bytes = new Uint8Array(await (await fetch(REPO + file)).arrayBuffer());
|
| 94 |
+
const providers = "gpu" in navigator ? ["webgpu", "wasm"] : ["wasm"];
|
| 95 |
+
for (const ep of providers) {
|
| 96 |
+
try {
|
| 97 |
+
sessions[size] = await ort.InferenceSession.create(bytes, { executionProviders: [ep] });
|
| 98 |
+
backend = ep;
|
| 99 |
+
return sessions[size];
|
| 100 |
+
} catch (err) { console.warn(`${ep} failed`, err); }
|
| 101 |
+
}
|
| 102 |
+
throw new Error("No execution provider could load the model.");
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
function loadImage(src) {
|
| 106 |
+
return new Promise((resolve, reject) => {
|
| 107 |
+
const img = new Image();
|
| 108 |
+
img.crossOrigin = "anonymous";
|
| 109 |
+
img.onload = () => resolve(img);
|
| 110 |
+
img.onerror = reject;
|
| 111 |
+
img.src = src;
|
| 112 |
+
});
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
function coverCrop(img, h, w) {
|
| 116 |
+
const scale = Math.max(w / img.naturalWidth, h / img.naturalHeight);
|
| 117 |
+
const sw = Math.max(w, Math.round(img.naturalWidth * scale));
|
| 118 |
+
const sh = Math.max(h, Math.round(img.naturalHeight * scale));
|
| 119 |
+
const canvas = $("input");
|
| 120 |
+
canvas.width = w; canvas.height = h;
|
| 121 |
+
const ctx = canvas.getContext("2d", { willReadFrequently: true });
|
| 122 |
+
ctx.imageSmoothingQuality = "high";
|
| 123 |
+
ctx.drawImage(img, -Math.floor((sw - w) / 2), -Math.floor((sh - h) / 2), sw, sh);
|
| 124 |
+
return { pixels: ctx.getImageData(0, 0, w, h).data, scale };
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
// Turbo colormap (Google, Apache-2.0), polynomial approximation.
|
| 128 |
+
function turbo(t) {
|
| 129 |
+
t = Math.min(1, Math.max(0, t));
|
| 130 |
+
const r = 0.13572138 + t * (4.6153926 + t * (-42.66032258 + t * (132.13108234 + t * (-152.94239396 + t * 59.28637943))));
|
| 131 |
+
const g = 0.09140261 + t * (2.19418839 + t * (4.84296658 + t * (-14.18503333 + t * (4.27729857 + t * 2.82956604))));
|
| 132 |
+
const b = 0.1066733 + t * (12.64194608 + t * (-60.58204836 + t * (110.36276771 + t * (-89.90310912 + t * 27.34824973))));
|
| 133 |
+
return [r, g, b].map((v) => Math.round(255 * Math.min(1, Math.max(0, v))));
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
function percentile(sorted, p) { return sorted[Math.min(sorted.length - 1, Math.floor(p * sorted.length))]; }
|
| 137 |
+
|
| 138 |
+
function drawDepth(values, h, w) {
|
| 139 |
+
const finite = Float32Array.from(values.filter(Number.isFinite)).sort();
|
| 140 |
+
const near = percentile(finite, 0.02), far = percentile(finite, 0.98);
|
| 141 |
+
const canvas = $("depth");
|
| 142 |
+
canvas.width = w; canvas.height = h;
|
| 143 |
+
const ctx = canvas.getContext("2d");
|
| 144 |
+
const out = ctx.createImageData(w, h);
|
| 145 |
+
for (let i = 0; i < values.length; i++) {
|
| 146 |
+
const d = Math.min(far, Math.max(near, Number.isFinite(values[i]) ? values[i] : far));
|
| 147 |
+
const [r, g, b] = turbo((1 / d - 1 / far) / (1 / near - 1 / far));
|
| 148 |
+
out.data.set([r, g, b, 255], 4 * i);
|
| 149 |
+
}
|
| 150 |
+
ctx.putImageData(out, 0, 0);
|
| 151 |
+
return { near, far, median: percentile(finite, 0.5) };
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
async function run() {
|
| 155 |
+
if (!image) return;
|
| 156 |
+
$("run").disabled = true;
|
| 157 |
+
try {
|
| 158 |
+
const [h, w] = $("size").value.split("x").map(Number);
|
| 159 |
+
const sess = await session($("size").value);
|
| 160 |
+
const { pixels, scale } = coverCrop(image, h, w);
|
| 161 |
+
const value = Number($("focal").value);
|
| 162 |
+
const fPx = $("focalMode").value === "fx" ? value * scale : 0.5 * w / Math.tan(value * Math.PI / 360);
|
| 163 |
+
const chw = new Float32Array(3 * h * w);
|
| 164 |
+
for (let i = 0; i < h * w; i++) for (let c = 0; c < 3; c++) chw[c * h * w + i] = pixels[4 * i + c] / 255;
|
| 165 |
+
status(`Running on ${backend}…`);
|
| 166 |
+
const start = performance.now();
|
| 167 |
+
const result = await sess.run({
|
| 168 |
+
pixel_values: new ort.Tensor("float32", chw, [1, 3, h, w]),
|
| 169 |
+
f_px: new ort.Tensor("float32", Float32Array.of(fPx), [1]),
|
| 170 |
+
});
|
| 171 |
+
const ms = performance.now() - start;
|
| 172 |
+
depth = { values: result.depth_meters.data, h, w };
|
| 173 |
+
const s = drawDepth(depth.values, h, w);
|
| 174 |
+
status(`${backend}, ${ms.toFixed(0)} ms. f_px ${fPx.toFixed(1)}. Depth 2nd-98th percentile ` +
|
| 175 |
+
`${s.near.toFixed(2)}-${s.far.toFixed(2)} m, median ${s.median.toFixed(2)} m.`);
|
| 176 |
+
} catch (err) {
|
| 177 |
+
status(`Error: ${err.message}`);
|
| 178 |
+
console.error(err);
|
| 179 |
+
} finally {
|
| 180 |
+
$("run").disabled = false;
|
| 181 |
+
}
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
$("depth").addEventListener("mousemove", (e) => {
|
| 185 |
+
if (!depth) return;
|
| 186 |
+
const rect = e.target.getBoundingClientRect();
|
| 187 |
+
const x = Math.floor((e.clientX - rect.left) / rect.width * depth.w);
|
| 188 |
+
const y = Math.floor((e.clientY - rect.top) / rect.height * depth.h);
|
| 189 |
+
const d = depth.values[y * depth.w + x];
|
| 190 |
+
$("readout").textContent = Number.isFinite(d) ? `${d.toFixed(2)} m at (${x}, ${y})` : "infinitely far";
|
| 191 |
+
});
|
| 192 |
+
$("depth").addEventListener("mouseleave", () => { $("readout").textContent = ""; });
|
| 193 |
+
$("file").addEventListener("change", async (e) => {
|
| 194 |
+
const file = e.target.files[0];
|
| 195 |
+
if (!file) return;
|
| 196 |
+
image = await loadImage(URL.createObjectURL(file));
|
| 197 |
+
coverCrop(image, ...$("size").value.split("x").map(Number));
|
| 198 |
+
});
|
| 199 |
+
$("size").addEventListener("change", () => { if (image) coverCrop(image, ...$("size").value.split("x").map(Number)); });
|
| 200 |
+
$("run").addEventListener("click", run);
|
| 201 |
+
|
| 202 |
+
(async () => {
|
| 203 |
+
try {
|
| 204 |
+
image = await loadImage(EXAMPLE);
|
| 205 |
+
coverCrop(image, 480, 640);
|
| 206 |
+
status("Example loaded (horizontal FOV 65.6°). Press Estimate depth, or pick your own photo.");
|
| 207 |
+
} catch {
|
| 208 |
+
status("Pick a photo to start.");
|
| 209 |
+
}
|
| 210 |
+
$("run").disabled = false;
|
| 211 |
+
})();
|
| 212 |
+
</script>
|
| 213 |
+
</body>
|
| 214 |
</html>
|