Spaces:
Running
Running
Depth Pro 2 in the browser on WebGPU
Browse files- .gitattributes +4 -0
- README.md +31 -5
- index.html +1194 -17
- samples/forest-thumb.jpg +0 -0
- samples/forest.jpg +3 -0
- samples/kitchen-thumb.jpg +0 -0
- samples/kitchen.jpg +3 -0
- samples/macaques-thumb.jpg +0 -0
- samples/macaques.jpg +3 -0
- samples/mountains-thumb.jpg +0 -0
- samples/mountains.jpg +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,7 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
samples/forest.jpg filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
samples/kitchen.jpg filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
samples/macaques.jpg filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
samples/mountains.jpg filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -1,10 +1,36 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: static
|
| 7 |
pinned: false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Depth Pro 2 WebGPU
|
| 3 |
+
emoji: 📷
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: red
|
| 6 |
sdk: static
|
| 7 |
pinned: false
|
| 8 |
+
license: other
|
| 9 |
+
license_name: apple-amlr
|
| 10 |
+
license_link: https://github.com/apple-aiml-research/ml-depth-pro-2/blob/main/LICENSE_MODEL
|
| 11 |
+
short_description: Depth from one photo, in your browser on WebGPU
|
| 12 |
+
models:
|
| 13 |
+
- byoussef/DepthPro2-litert
|
| 14 |
+
tags:
|
| 15 |
+
- depth-estimation
|
| 16 |
+
- webgpu
|
| 17 |
+
- litert
|
| 18 |
---
|
| 19 |
|
| 20 |
+
# Depth Pro 2 on WebGPU
|
| 21 |
+
|
| 22 |
+
[Depth Pro 2](https://huggingface.co/apple/DepthPro2) running in the browser: the [LiteRT files](https://huggingface.co/byoussef/DepthPro2-litert) on WebGPU, with [LiteRT.js](https://ai.google.dev/edge/litert/web). Photos and camera frames stay on the device.
|
| 23 |
+
|
| 24 |
+
- Pick a sample, upload a photo or start the camera, then drag the divider between the photo and its depth.
|
| 25 |
+
- Model S (98 MB) or B (387 MB), in float16 or float32. float16 needs the GPU's `shader-f16` feature; without it the page stays in float32.
|
| 26 |
+
- "Depth in meters" adds the field-of-view model (95 MB), which estimates the focal length. Point at the picture to read a distance.
|
| 27 |
+
|
| 28 |
+
It needs WebGPU: Chrome, Edge or Firefox on a computer. Safari 27.2 runs it and returns wrong depth.
|
| 29 |
+
|
| 30 |
+
The whole demo is [`index.html`](index.html).
|
| 31 |
+
|
| 32 |
+
## License
|
| 33 |
+
|
| 34 |
+
Apple Machine Learning Research Model is licensed under the Apple Machine Learning Research Model License Agreement, which allows research use only. The demo and the converted files are unofficial, and Apple does not endorse them.
|
| 35 |
+
|
| 36 |
+
The sample photos are CC0, from Wikimedia Commons: [macaques](https://commons.wikimedia.org/wiki/File:Bonnet_Macaque_David_raju-.jpg) by Davidvraju, [kitchen](https://commons.wikimedia.org/wiki/File:Blue_white_kitchen_interior_(Unsplash).jpg) by NeONBRAND, [forest path](https://commons.wikimedia.org/wiki/File:Forest_path_surrounded_by_green_trees.jpg) by Zed Can77 and [mountain reservoir](https://commons.wikimedia.org/wiki/File:Mountain_lake_dam.jpg) by Paul Gilmore.
|
index.html
CHANGED
|
@@ -1,19 +1,1196 @@
|
|
| 1 |
<!doctype html>
|
| 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 on WebGPU</title>
|
| 7 |
+
<meta name="description" content="Depth from a single photo, computed in your browser on WebGPU with LiteRT.js.">
|
| 8 |
+
<link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Crect width='8' height='16' fill='%2358626b'/%3E%3Crect x='8' width='8' height='16' fill='%23f66b19'/%3E%3Crect x='7.25' width='1.5' height='16' fill='%23fff'/%3E%3C/svg%3E">
|
| 9 |
+
<style>
|
| 10 |
+
:root {
|
| 11 |
+
color-scheme: light dark;
|
| 12 |
+
--ground: #f2f4f6;
|
| 13 |
+
--surface: #ffffff;
|
| 14 |
+
--hover: #e6e9ed;
|
| 15 |
+
--ink: #15181b;
|
| 16 |
+
--ink-2: #4b535a;
|
| 17 |
+
--line: #d3d8dd;
|
| 18 |
+
--line-2: #aab1b9;
|
| 19 |
+
--accent: #c2410c;
|
| 20 |
+
--caution: #8a4b00;
|
| 21 |
+
--radius: 6px;
|
| 22 |
+
}
|
| 23 |
+
@media (prefers-color-scheme: dark) {
|
| 24 |
+
:root {
|
| 25 |
+
--ground: #111315;
|
| 26 |
+
--surface: #1b1e21;
|
| 27 |
+
--hover: #262a2e;
|
| 28 |
+
--ink: #eceef0;
|
| 29 |
+
--ink-2: #aab1b8;
|
| 30 |
+
--line: #2b3035;
|
| 31 |
+
--line-2: #4a525a;
|
| 32 |
+
--accent: #ff8f3d;
|
| 33 |
+
--caution: #ffb86b;
|
| 34 |
+
}
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
* { box-sizing: border-box; }
|
| 38 |
+
[hidden] { display: none !important; }
|
| 39 |
+
::selection { background: color-mix(in srgb, var(--accent) 28%, transparent); }
|
| 40 |
+
html { background: var(--ground); }
|
| 41 |
+
body {
|
| 42 |
+
margin: 0;
|
| 43 |
+
background: var(--ground);
|
| 44 |
+
color: var(--ink);
|
| 45 |
+
font: 0.875rem/1.45 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
|
| 46 |
+
-webkit-font-smoothing: antialiased;
|
| 47 |
+
}
|
| 48 |
+
a { color: inherit; text-decoration-color: var(--line-2); text-decoration-thickness: 1px; text-underline-offset: 3px; }
|
| 49 |
+
a:hover { text-decoration-color: currentColor; }
|
| 50 |
+
:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }
|
| 51 |
+
|
| 52 |
+
/* The page is no wider than what keeps the picture inside the window. */
|
| 53 |
+
.page {
|
| 54 |
+
max-width: min(1240px, max(780px, calc((100dvh - 190px) * 854 / 480 + 368px)));
|
| 55 |
+
margin: 0 auto;
|
| 56 |
+
padding: 18px 24px 24px;
|
| 57 |
+
}
|
| 58 |
+
header { display: flex; flex-wrap: wrap; align-items: baseline; gap: 2px 16px; margin-bottom: 14px; }
|
| 59 |
+
h1 { margin: 0; font-size: 1.125rem; font-weight: 650; letter-spacing: -0.01em; }
|
| 60 |
+
.lede { flex: 1 1 30ch; margin: 0; color: var(--ink-2); }
|
| 61 |
+
header nav { display: flex; gap: 16px; }
|
| 62 |
+
main { display: grid; grid-template-columns: minmax(0, 1fr) 296px; gap: 24px; align-items: start; }
|
| 63 |
+
|
| 64 |
+
/* The picture: depth underneath, the photo on top of it, clipped at the divider. */
|
| 65 |
+
.viewer {
|
| 66 |
+
--split: 100;
|
| 67 |
+
position: relative;
|
| 68 |
+
aspect-ratio: 854 / 480;
|
| 69 |
+
overflow: hidden;
|
| 70 |
+
border-radius: var(--radius);
|
| 71 |
+
background: #0c0e10;
|
| 72 |
+
touch-action: pan-y;
|
| 73 |
+
user-select: none;
|
| 74 |
+
-webkit-user-select: none;
|
| 75 |
+
}
|
| 76 |
+
.viewer canvas { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }
|
| 77 |
+
#photo { clip-path: inset(0 calc(100% - var(--split) * 1%) 0 0); }
|
| 78 |
+
.viewer.dropping { outline: 2px solid var(--accent); outline-offset: 2px; }
|
| 79 |
+
.viewer[data-probing] { cursor: crosshair; }
|
| 80 |
+
.divider {
|
| 81 |
+
position: absolute;
|
| 82 |
+
top: 0;
|
| 83 |
+
bottom: 0;
|
| 84 |
+
left: calc(var(--split) * 1%);
|
| 85 |
+
display: grid;
|
| 86 |
+
place-items: center;
|
| 87 |
+
width: 44px;
|
| 88 |
+
margin-left: -22px;
|
| 89 |
+
cursor: ew-resize;
|
| 90 |
+
touch-action: none;
|
| 91 |
+
outline: none;
|
| 92 |
+
}
|
| 93 |
+
.divider::before {
|
| 94 |
+
content: "";
|
| 95 |
+
position: absolute;
|
| 96 |
+
top: 0;
|
| 97 |
+
bottom: 0;
|
| 98 |
+
left: 21px;
|
| 99 |
+
width: 2px;
|
| 100 |
+
background: #fff;
|
| 101 |
+
box-shadow: 0 0 0 1px rgb(0 0 0 / 0.28);
|
| 102 |
+
}
|
| 103 |
+
.grip {
|
| 104 |
+
position: relative;
|
| 105 |
+
display: grid;
|
| 106 |
+
place-items: center;
|
| 107 |
+
width: 34px;
|
| 108 |
+
height: 34px;
|
| 109 |
+
border-radius: 50%;
|
| 110 |
+
background: #fff;
|
| 111 |
+
color: #15181b;
|
| 112 |
+
box-shadow: 0 1px 2px rgb(0 0 0 / 0.3), 0 4px 12px rgb(0 0 0 / 0.28);
|
| 113 |
+
}
|
| 114 |
+
.divider:focus-visible .grip { box-shadow: 0 0 0 2px #15181b, 0 0 0 4px #fff; }
|
| 115 |
+
.viewer:not([data-depth]) .divider, .viewer:not([data-depth]) #tag-depth { display: none; }
|
| 116 |
+
.tag {
|
| 117 |
+
position: absolute;
|
| 118 |
+
bottom: 10px;
|
| 119 |
+
padding: 3px 8px;
|
| 120 |
+
border-radius: 4px;
|
| 121 |
+
background: rgb(12 14 16 / 0.74);
|
| 122 |
+
color: #fff;
|
| 123 |
+
font-size: 0.75rem;
|
| 124 |
+
font-weight: 600;
|
| 125 |
+
pointer-events: none;
|
| 126 |
+
}
|
| 127 |
+
#tag-photo { left: 10px; }
|
| 128 |
+
#tag-depth { right: 10px; }
|
| 129 |
+
.probe, .spot { position: absolute; top: 0; left: 0; pointer-events: none; }
|
| 130 |
+
.probe {
|
| 131 |
+
padding: 3px 7px;
|
| 132 |
+
border-radius: 4px;
|
| 133 |
+
background: #fff;
|
| 134 |
+
color: #15181b;
|
| 135 |
+
font-size: 0.8125rem;
|
| 136 |
+
font-weight: 600;
|
| 137 |
+
font-variant-numeric: tabular-nums;
|
| 138 |
+
white-space: nowrap;
|
| 139 |
+
box-shadow: 0 1px 2px rgb(0 0 0 / 0.3), 0 3px 10px rgb(0 0 0 / 0.25);
|
| 140 |
+
}
|
| 141 |
+
.spot {
|
| 142 |
+
width: 9px;
|
| 143 |
+
height: 9px;
|
| 144 |
+
margin: -4.5px 0 0 -4.5px;
|
| 145 |
+
border: 1.5px solid #fff;
|
| 146 |
+
border-radius: 50%;
|
| 147 |
+
box-shadow: 0 0 0 1px rgb(0 0 0 / 0.5);
|
| 148 |
+
}
|
| 149 |
+
.progress {
|
| 150 |
+
position: absolute;
|
| 151 |
+
bottom: 14px;
|
| 152 |
+
left: 50%;
|
| 153 |
+
width: max-content;
|
| 154 |
+
min-width: 200px;
|
| 155 |
+
max-width: calc(100% - 28px);
|
| 156 |
+
padding: 9px 12px 10px;
|
| 157 |
+
border-radius: var(--radius);
|
| 158 |
+
background: rgb(12 14 16 / 0.88);
|
| 159 |
+
color: #fff;
|
| 160 |
+
font-size: 0.8125rem;
|
| 161 |
+
font-variant-numeric: tabular-nums;
|
| 162 |
+
transform: translateX(-50%);
|
| 163 |
+
}
|
| 164 |
+
.bar { height: 3px; margin-top: 7px; overflow: hidden; border-radius: 2px; background: rgb(255 255 255 / 0.22); }
|
| 165 |
+
.bar i { display: block; width: var(--done, 0%); height: 100%; border-radius: 2px; background: #fff; transition: width 0.15s linear; }
|
| 166 |
+
.progress.waiting .bar i { width: 35%; transition: none; animation: slide 1.15s cubic-bezier(0.4, 0, 0.2, 1) infinite; }
|
| 167 |
+
@keyframes slide { from { transform: translateX(-100%); } to { transform: translateX(290%); } }
|
| 168 |
+
.notice {
|
| 169 |
+
position: absolute;
|
| 170 |
+
inset: 0;
|
| 171 |
+
display: grid;
|
| 172 |
+
place-content: center;
|
| 173 |
+
justify-items: center;
|
| 174 |
+
gap: 14px;
|
| 175 |
+
padding: 24px;
|
| 176 |
+
background: rgb(12 14 16 / 0.8);
|
| 177 |
+
color: #fff;
|
| 178 |
+
text-align: center;
|
| 179 |
+
}
|
| 180 |
+
.notice p { max-width: 44ch; margin: 0; font-size: 0.9375rem; text-wrap: balance; }
|
| 181 |
+
.notice .button { border-color: #fff; background: #fff; color: #15181b; }
|
| 182 |
+
|
| 183 |
+
/* The colors as distances: laid out like the distance scale of a lens, which is also linear in
|
| 184 |
+
inverse depth and ends at infinity. */
|
| 185 |
+
.scale { position: relative; margin-top: 12px; }
|
| 186 |
+
#ramp { display: block; width: 100%; height: 10px; border-radius: 2px; }
|
| 187 |
+
#index {
|
| 188 |
+
position: absolute;
|
| 189 |
+
top: -3px;
|
| 190 |
+
width: 3px;
|
| 191 |
+
height: 16px;
|
| 192 |
+
margin-left: -1.5px;
|
| 193 |
+
border-radius: 1.5px;
|
| 194 |
+
background: var(--ink);
|
| 195 |
+
box-shadow: 0 0 0 1.5px var(--ground);
|
| 196 |
+
}
|
| 197 |
+
.ticks { position: relative; height: 26px; color: var(--ink-2); font-size: 0.75rem; font-variant-numeric: tabular-nums; }
|
| 198 |
+
.ticks span { position: absolute; top: 0; padding-top: 8px; white-space: nowrap; transform: translateX(-50%); }
|
| 199 |
+
.ticks span::before { content: ""; position: absolute; top: 2px; left: 50%; width: 1px; height: 5px; background: var(--line-2); }
|
| 200 |
+
.ticks .first { transform: none; }
|
| 201 |
+
.ticks .first::before { left: 0; }
|
| 202 |
+
.ticks .last { transform: translateX(-100%); }
|
| 203 |
+
.ticks .infinity { font-size: 1rem; line-height: 1; }
|
| 204 |
+
.ticks .last::before { right: 0; left: auto; }
|
| 205 |
+
.status, .caution { min-height: 1.45em; margin: 6px 0 0; font-variant-numeric: tabular-nums; }
|
| 206 |
+
.status { color: var(--ink-2); }
|
| 207 |
+
.caution { color: var(--caution); }
|
| 208 |
+
|
| 209 |
+
/* The controls. */
|
| 210 |
+
.panel { display: grid; gap: 22px; }
|
| 211 |
+
.group { min-width: 0; margin: 0; padding: 0; border: 0; }
|
| 212 |
+
.group h2, .group legend, .switch { margin: 0 0 8px; padding: 0; font-size: 0.8125rem; font-weight: 600; }
|
| 213 |
+
.hint { margin: 7px 0 0; color: var(--ink-2); font-size: 0.8125rem; }
|
| 214 |
+
.samples { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
|
| 215 |
+
.samples button {
|
| 216 |
+
aspect-ratio: 16 / 9;
|
| 217 |
+
padding: 0;
|
| 218 |
+
overflow: hidden;
|
| 219 |
+
border: 0;
|
| 220 |
+
border-radius: 4px;
|
| 221 |
+
background: var(--line);
|
| 222 |
+
cursor: pointer;
|
| 223 |
+
}
|
| 224 |
+
.samples img { display: block; width: 100%; height: 100%; object-fit: cover; }
|
| 225 |
+
.samples button:hover { outline: 2px solid var(--line-2); outline-offset: 2px; }
|
| 226 |
+
.samples button[aria-pressed="true"] { outline: 2px solid var(--accent); outline-offset: 2px; }
|
| 227 |
+
.samples button:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }
|
| 228 |
+
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 10px; }
|
| 229 |
+
.button {
|
| 230 |
+
position: relative;
|
| 231 |
+
display: inline-flex;
|
| 232 |
+
align-items: center;
|
| 233 |
+
justify-content: center;
|
| 234 |
+
gap: 7px;
|
| 235 |
+
padding: 7px 12px;
|
| 236 |
+
border: 1px solid var(--line-2);
|
| 237 |
+
border-radius: var(--radius);
|
| 238 |
+
background: var(--surface);
|
| 239 |
+
color: var(--ink);
|
| 240 |
+
font: inherit;
|
| 241 |
+
font-weight: 600;
|
| 242 |
+
cursor: pointer;
|
| 243 |
+
transition: background-color 0.15s, border-color 0.15s;
|
| 244 |
+
}
|
| 245 |
+
.button:hover { border-color: var(--ink-2); }
|
| 246 |
+
.button:active { background: var(--hover); }
|
| 247 |
+
.button[aria-pressed="true"] { border-color: var(--ink); background: var(--ink); color: var(--ground); }
|
| 248 |
+
.button:has(:focus-visible) { outline: 2px solid var(--ink); outline-offset: 2px; }
|
| 249 |
+
.button svg, .grip svg { fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
|
| 250 |
+
.button input { position: absolute; width: 1px; height: 1px; opacity: 0; }
|
| 251 |
+
.segmented {
|
| 252 |
+
display: grid;
|
| 253 |
+
grid-auto-columns: 1fr;
|
| 254 |
+
grid-auto-flow: column;
|
| 255 |
+
gap: 2px;
|
| 256 |
+
padding: 2px;
|
| 257 |
+
border: 1px solid var(--line-2);
|
| 258 |
+
border-radius: var(--radius);
|
| 259 |
+
background: var(--surface);
|
| 260 |
+
}
|
| 261 |
+
.segmented label { position: relative; }
|
| 262 |
+
.segmented input { position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; opacity: 0; cursor: pointer; }
|
| 263 |
+
.segmented span {
|
| 264 |
+
display: flex;
|
| 265 |
+
align-items: baseline;
|
| 266 |
+
justify-content: center;
|
| 267 |
+
gap: 6px;
|
| 268 |
+
padding: 5px 8px;
|
| 269 |
+
border-radius: 4px;
|
| 270 |
+
font-weight: 600;
|
| 271 |
+
transition: background-color 0.15s, color 0.15s;
|
| 272 |
+
}
|
| 273 |
+
.segmented small { color: var(--ink-2); font-size: 0.75rem; font-weight: 400; }
|
| 274 |
+
.segmented label:hover input:not(:checked, :disabled) + span { background: var(--hover); }
|
| 275 |
+
.segmented input:checked + span { background: var(--ink); color: var(--ground); }
|
| 276 |
+
.segmented input:checked + span small { color: inherit; opacity: 0.75; }
|
| 277 |
+
.segmented input:focus-visible + span { outline: 2px solid var(--ink); outline-offset: 3px; }
|
| 278 |
+
.segmented input:disabled { cursor: not-allowed; }
|
| 279 |
+
.segmented input:disabled + span { opacity: 0.45; }
|
| 280 |
+
.switch { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 0; cursor: pointer; }
|
| 281 |
+
.switch input {
|
| 282 |
+
position: relative;
|
| 283 |
+
flex: none;
|
| 284 |
+
width: 36px;
|
| 285 |
+
height: 20px;
|
| 286 |
+
margin: 0;
|
| 287 |
+
border-radius: 10px;
|
| 288 |
+
background: var(--line-2);
|
| 289 |
+
cursor: pointer;
|
| 290 |
+
transition: background-color 0.15s;
|
| 291 |
+
appearance: none;
|
| 292 |
+
-webkit-appearance: none;
|
| 293 |
+
}
|
| 294 |
+
.switch input::after {
|
| 295 |
+
content: "";
|
| 296 |
+
position: absolute;
|
| 297 |
+
top: 2px;
|
| 298 |
+
left: 2px;
|
| 299 |
+
width: 16px;
|
| 300 |
+
height: 16px;
|
| 301 |
+
border-radius: 50%;
|
| 302 |
+
background: #fff;
|
| 303 |
+
box-shadow: 0 1px 2px rgb(0 0 0 / 0.3);
|
| 304 |
+
transition: transform 0.18s cubic-bezier(0.16, 1, 0.3, 1);
|
| 305 |
+
}
|
| 306 |
+
.switch input:checked { background: var(--accent); }
|
| 307 |
+
.switch input:checked::after { transform: translateX(16px); }
|
| 308 |
+
.panel[inert] { opacity: 0.5; }
|
| 309 |
+
footer { max-width: 86ch; margin-top: 26px; color: var(--ink-2); font-size: 0.8125rem; }
|
| 310 |
+
footer p { margin: 0; }
|
| 311 |
+
|
| 312 |
+
@media (max-width: 860px) {
|
| 313 |
+
.page { max-width: none; padding: 14px 16px 20px; }
|
| 314 |
+
main { grid-template-columns: minmax(0, 1fr); gap: 18px; }
|
| 315 |
+
.samples { grid-template-columns: repeat(4, 1fr); }
|
| 316 |
+
}
|
| 317 |
+
@media (prefers-reduced-motion: reduce) {
|
| 318 |
+
*, *::before, *::after { transition: none !important; }
|
| 319 |
+
.progress.waiting .bar i { width: 100%; opacity: 0.5; animation: none; }
|
| 320 |
+
}
|
| 321 |
+
</style>
|
| 322 |
+
</head>
|
| 323 |
+
<body>
|
| 324 |
+
<div class="page">
|
| 325 |
+
<header>
|
| 326 |
+
<h1>Depth Pro 2 on WebGPU</h1>
|
| 327 |
+
<p class="lede">Depth from a single photo, computed in this browser on your GPU. Photos stay on your device.</p>
|
| 328 |
+
<nav>
|
| 329 |
+
<a href="https://huggingface.co/byoussef/DepthPro2-litert" target="_blank" rel="noopener">Model files</a>
|
| 330 |
+
<a href="https://huggingface.co/apple/DepthPro2" target="_blank" rel="noopener">Original model</a>
|
| 331 |
+
<a href="https://arxiv.org/abs/2611.26603" target="_blank" rel="noopener">Paper</a>
|
| 332 |
+
</nav>
|
| 333 |
+
</header>
|
| 334 |
+
|
| 335 |
+
<main>
|
| 336 |
+
<section aria-label="Result">
|
| 337 |
+
<div class="viewer" id="viewer">
|
| 338 |
+
<canvas id="depth" width="854" height="480" role="img" aria-label="Depth map: red is near, blue is far"></canvas>
|
| 339 |
+
<canvas id="photo" width="854" height="480" role="img" aria-label="The photo"></canvas>
|
| 340 |
+
<div class="divider" id="divider" role="slider" tabindex="0" aria-label="Photo on the left, depth on the right"
|
| 341 |
+
aria-orientation="horizontal" aria-valuemin="0" aria-valuemax="100" aria-valuenow="100">
|
| 342 |
+
<span class="grip"><svg width="16" height="16" viewBox="0 0 16 16" aria-hidden="true"><path d="M6 4.5 2.5 8 6 11.5M10 4.5 13.5 8 10 11.5"/></svg></span>
|
| 343 |
+
</div>
|
| 344 |
+
<span class="tag" id="tag-photo">Photo</span>
|
| 345 |
+
<span class="tag" id="tag-depth">Depth</span>
|
| 346 |
+
<span class="spot" id="spot" hidden></span>
|
| 347 |
+
<output class="probe" id="probe" hidden></output>
|
| 348 |
+
<div class="progress" id="progress" hidden>
|
| 349 |
+
<span id="progress-text"></span>
|
| 350 |
+
<div class="bar"><i></i></div>
|
| 351 |
+
</div>
|
| 352 |
+
<div class="notice" id="notice" role="alert" hidden>
|
| 353 |
+
<p id="notice-text"></p>
|
| 354 |
+
<button class="button" id="notice-action" type="button"></button>
|
| 355 |
+
</div>
|
| 356 |
+
</div>
|
| 357 |
+
<div class="scale">
|
| 358 |
+
<canvas id="ramp" width="213" height="1" aria-hidden="true"></canvas>
|
| 359 |
+
<span id="index" hidden></span>
|
| 360 |
+
<div class="ticks" id="ticks"></div>
|
| 361 |
+
</div>
|
| 362 |
+
<p class="status" id="status" role="status"></p>
|
| 363 |
+
<p class="caution" id="caution" hidden></p>
|
| 364 |
+
</section>
|
| 365 |
+
|
| 366 |
+
<aside class="panel" id="panel" aria-label="Settings">
|
| 367 |
+
<section class="group">
|
| 368 |
+
<h2>Photo</h2>
|
| 369 |
+
<div class="samples" id="samples"></div>
|
| 370 |
+
<div class="row">
|
| 371 |
+
<label class="button">
|
| 372 |
+
<svg width="16" height="16" viewBox="0 0 16 16" aria-hidden="true"><path d="M8 10.5v-8M4.75 5.5 8 2.25l3.25 3.25M2.5 10.5v2a1 1 0 0 0 1 1h9a1 1 0 0 0 1-1v-2"/></svg>
|
| 373 |
+
Upload
|
| 374 |
+
<input id="file" type="file" accept="image/*">
|
| 375 |
+
</label>
|
| 376 |
+
<button class="button" id="camera" type="button" aria-pressed="false">
|
| 377 |
+
<svg width="16" height="16" viewBox="0 0 16 16" aria-hidden="true"><rect x="1.75" y="4" width="9" height="8" rx="1.5"/><path d="m10.75 7 3.5-2v6l-3.5-2"/></svg>
|
| 378 |
+
<span id="camera-label">Camera</span>
|
| 379 |
+
</button>
|
| 380 |
+
</div>
|
| 381 |
+
<p class="hint">Or drop a photo anywhere on the page. It is cropped to 16:9, the shape the models take.</p>
|
| 382 |
+
</section>
|
| 383 |
+
|
| 384 |
+
<fieldset class="group">
|
| 385 |
+
<legend>Model</legend>
|
| 386 |
+
<div class="segmented">
|
| 387 |
+
<label><input type="radio" name="model" value="s" checked><span>S <small>98 MB</small></span></label>
|
| 388 |
+
<label><input type="radio" name="model" value="b"><span>B <small>387 MB</small></span></label>
|
| 389 |
+
</div>
|
| 390 |
+
<p class="hint">B is sharper and takes about 2.5 times as long.</p>
|
| 391 |
+
</fieldset>
|
| 392 |
+
|
| 393 |
+
<fieldset class="group">
|
| 394 |
+
<legend>Precision</legend>
|
| 395 |
+
<div class="segmented">
|
| 396 |
+
<label><input type="radio" name="precision" value="fp16" checked><span>float16</span></label>
|
| 397 |
+
<label><input type="radio" name="precision" value="fp32"><span>float32</span></label>
|
| 398 |
+
</div>
|
| 399 |
+
<p class="hint" id="precision-hint">float16 is about twice as fast and within a percent or so of float32.</p>
|
| 400 |
+
</fieldset>
|
| 401 |
+
|
| 402 |
+
<div class="group">
|
| 403 |
+
<label class="switch">Depth in meters <input id="metric" type="checkbox" role="switch"></label>
|
| 404 |
+
<p class="hint">Adds a second model (95 MB) that estimates the camera's field of view. Without it the depth is relative.</p>
|
| 405 |
+
</div>
|
| 406 |
+
</aside>
|
| 407 |
+
</main>
|
| 408 |
+
|
| 409 |
+
<footer>
|
| 410 |
+
<p>
|
| 411 |
+
Depth Pro 2 is Apple's model. The files here are unofficial
|
| 412 |
+
<a href="https://huggingface.co/byoussef/DepthPro2-litert" target="_blank" rel="noopener">LiteRT conversions</a>,
|
| 413 |
+
not endorsed by Apple, under the
|
| 414 |
+
<a href="https://huggingface.co/byoussef/DepthPro2-litert/blob/main/LICENSE" target="_blank" rel="noopener">Apple Machine Learning Research Model License</a>:
|
| 415 |
+
research use only. The sample photos are CC0, from Wikimedia Commons.
|
| 416 |
+
</p>
|
| 417 |
+
</footer>
|
| 418 |
+
</div>
|
| 419 |
+
<video id="video" playsinline muted hidden></video>
|
| 420 |
+
|
| 421 |
+
<script type="module">
|
| 422 |
+
const LITERT = 'https://cdn.jsdelivr.net/npm/@litertjs/core@2.6.0';
|
| 423 |
+
// A fixed revision: what the browser has stored of it stays right.
|
| 424 |
+
const FILES = 'https://huggingface.co/byoussef/DepthPro2-litert/resolve/85da0e06b4f3d2a733c84d4527ab45e235648654/';
|
| 425 |
+
const MODELS = {
|
| 426 |
+
s: {name: 'S', label: 'model S', file: 'depthpro2-s_480x854.tflite', bytes: 98344368},
|
| 427 |
+
b: {name: 'B', label: 'model B', file: 'depthpro2-b_480x854.tflite', bytes: 387119408},
|
| 428 |
+
fov: {label: 'the field-of-view model', file: 'depthpro2-fov_480x832.tflite', bytes: 95280716},
|
| 429 |
+
};
|
| 430 |
+
const SAMPLES = [
|
| 431 |
+
['macaques', 'Two macaques'], ['kitchen', 'A kitchen'], ['forest', 'A forest path'], ['mountains', 'A mountain reservoir'],
|
| 432 |
+
];
|
| 433 |
+
const [WIDTH, HEIGHT] = [854, 480]; // the frame the depth models take
|
| 434 |
+
// The field-of-view model takes the frame resized to 864x480 and cropped to its middle 832 columns.
|
| 435 |
+
const [FOV_WIDTH, FOV_CROP] = [864, 832];
|
| 436 |
+
// As the original code colors it: Turbo from inverse depth 0, infinitely far, to 1.2 times the nearest.
|
| 437 |
+
const HEADROOM = 1.2;
|
| 438 |
+
const TURBO = Uint8Array.from(atob(
|
| 439 |
+
'MBI7MRVCMhhKNBtRNR5YNiFfNyNlOCZsOSlyOix5Oy9/PDKFPDWLPTeRPjqWPz2cQEChQEOmQUWrQUiwQku1Q066Q1C+Q1PCRFbHRFjLRVvORV7SRWDW' +
|
| 440 |
+
'RWPZRmbdRmjgRmvjRm3mRnDoRnPrRnXtRnjwRnryRn30Rn/2RoL4RYT5RYf7RYn8RIz9Q479QpH+QZP+QJb+P5j+Ppv+PJ39O6D8OaL8OKX7Nqj5NKr4' +
|
| 441 |
+
'M6z2Ma/1L7HzLbTxK7bvKrntKLvrJr3pJcDmI8LkIcThIMbfHsncHcvaHM3XG8/UGtHSGdPPGNXMGNfKF9nHF9rEF9zCF96/GOC9GOG6GeO4GuS2G+W0' +
|
| 442 |
+
'HeexHuivIOmsIuupJOymJ+2jKe6gLO+dL/CaMvGXNfOUOPSRO/SNP/WKQvaHRveDSviATfl8Ufl5Vfp2WftyXftvYfxsZfxoaf1lbf1icf1fdP5ceP5Z' +
|
| 443 |
+
'fP5WgP5ThP5Qh/5Ni/5Ljv5Ikv5Glf5EmP5Cm/1Anv0+ofw9pPw7pvs6qfs5rPo3rvk3sfg2s/g1tvc1ufU0u/Q0vvM0wPIzw/Ezxe8zyO4zyu0zzes0' +
|
| 444 |
+
'z+o00eg01Oc11uU12OM12uI23eA239424dw349o35dg459c46NU46tM57NE57c8578058Ms68sg688Y69MQ69sI698A5+L45+bw5+bo4+rc3+7U3+7M2' +
|
| 445 |
+
'/LA1/K40/asz/aky/aYx/aMw/qEv/p4u/pst/pgs/ZUr/ZIp/Y8o/Ywn/Ikm/IYk+4Mj+4Ai+n0g+nof+Xce+HQc93Eb924a9msY9WgX9GUW82MV8mAU' +
|
| 446 |
+
'8V0T71oR7lgQ7VUP7FIO6lAN6U0N6EsM5kkL5UYK40QK4kIJ4EAI3j4I3TwH2zoH2TgG1zYG1jQF1DIF0jAF0C8Ezi0EyysDySkDxygDxSYCwyQCwCMC' +
|
| 447 |
+
'viECux8BuR4BthwBtBsBsRkBrhgBrBYBqRUBphQBoxIBoBEBnRABmg4Blw0BlAwBkQsBjgoBiwkBhwgBhAcBgQYCfQUCegQC'),
|
| 448 |
+
(character) => character.charCodeAt(0));
|
| 449 |
+
|
| 450 |
+
const $ = (id) => document.getElementById(id);
|
| 451 |
+
const viewer = $('viewer');
|
| 452 |
+
const video = $('video');
|
| 453 |
+
const photo = $('photo').getContext('2d');
|
| 454 |
+
const depthContext = $('depth').getContext('2d');
|
| 455 |
+
const depthPixels = new ImageData(WIDTH, HEIGHT);
|
| 456 |
+
// What the photo side shows, and the pixels the models take: made together, so the two sides
|
| 457 |
+
// of the divider are always of the same moment.
|
| 458 |
+
const capture = document.createElement('canvas').getContext('2d');
|
| 459 |
+
const crop = document.createElement('canvas').getContext('2d', {willReadFrequently: true});
|
| 460 |
+
let frame = null; // RGB of the frame, row by row, 0 to 255
|
| 461 |
+
const calm = matchMedia('(prefers-reduced-motion: reduce)');
|
| 462 |
+
|
| 463 |
+
const want = {model: 's', precision: 'fp16', metric: false}; // what the controls ask for
|
| 464 |
+
const loaded = {depth: null, fov: null}; // the compiled models
|
| 465 |
+
const held = new Map(); // files downloaded and not compiled yet
|
| 466 |
+
let litert;
|
| 467 |
+
let gpu = '';
|
| 468 |
+
let transfer = null; // the download in progress
|
| 469 |
+
let source = null; // what is looked at: a photo or the camera
|
| 470 |
+
let depth = null; // the depth on screen
|
| 471 |
+
let stopped = false; // nothing more can run before a reload
|
| 472 |
+
let resting = 50; // where the divider settles
|
| 473 |
+
let pointer = null; // the place being probed, as fractions of the picture
|
| 474 |
+
let standing = ''; // a caution that holds for the whole visit
|
| 475 |
+
let asked = 0; // the photos asked for so far
|
| 476 |
+
let ready = false; // LiteRT.js is up, and the visitor agreed to the download where asked
|
| 477 |
+
|
| 478 |
+
// ---- Messages -----------------------------------------------------------------------------
|
| 479 |
+
|
| 480 |
+
function progress(text, fraction) {
|
| 481 |
+
$('progress').hidden = false;
|
| 482 |
+
$('progress').classList.toggle('waiting', fraction === undefined);
|
| 483 |
+
$('progress').style.setProperty('--done', `${100 * (fraction ?? 0)}%`);
|
| 484 |
+
$('progress-text').textContent = text;
|
| 485 |
+
document.documentElement.dataset.state = 'busy';
|
| 486 |
+
}
|
| 487 |
+
|
| 488 |
+
function settle() {
|
| 489 |
+
$('progress').hidden = true;
|
| 490 |
+
document.documentElement.dataset.state = $('notice').hidden ? 'ready' : 'blocked';
|
| 491 |
+
}
|
| 492 |
+
|
| 493 |
+
// A message over the picture, with what to do about it.
|
| 494 |
+
function notify(text, action, act) {
|
| 495 |
+
$('notice').hidden = false;
|
| 496 |
+
$('notice-text').textContent = text;
|
| 497 |
+
$('notice-action').hidden = !action;
|
| 498 |
+
$('notice-action').textContent = action ?? '';
|
| 499 |
+
$('notice-action').onclick = act ?? null;
|
| 500 |
+
settle();
|
| 501 |
+
}
|
| 502 |
+
|
| 503 |
+
function stop(text) {
|
| 504 |
+
if (stopped) return;
|
| 505 |
+
stopped = true;
|
| 506 |
+
endCamera();
|
| 507 |
+
$('panel').inert = true;
|
| 508 |
+
notify(text, 'Reload the page', () => location.reload());
|
| 509 |
+
}
|
| 510 |
+
|
| 511 |
+
// A line under the picture; with no text, back to what holds for the whole visit.
|
| 512 |
+
function warn(text) {
|
| 513 |
+
$('caution').textContent = text || standing;
|
| 514 |
+
$('caution').hidden = !$('caution').textContent;
|
| 515 |
+
}
|
| 516 |
+
|
| 517 |
+
// ---- The models ---------------------------------------------------------------------------
|
| 518 |
+
|
| 519 |
+
async function start() {
|
| 520 |
+
if (!navigator.gpu) {
|
| 521 |
+
throw new Error('This browser has no WebGPU, which the models run on. Chrome, Edge and Firefox on a computer have it.');
|
| 522 |
+
}
|
| 523 |
+
let adapter = null;
|
| 524 |
+
for (let attempt = 0; attempt < 8 && !adapter; attempt++) {
|
| 525 |
+
adapter = await navigator.gpu.requestAdapter({powerPreference: 'high-performance'});
|
| 526 |
+
if (!adapter) await new Promise((resolve) => setTimeout(resolve, 250));
|
| 527 |
+
}
|
| 528 |
+
if (!adapter) throw new Error('WebGPU is on in this browser, but it found no GPU to use.');
|
| 529 |
+
try {
|
| 530 |
+
litert = await import(`${LITERT}/+esm`);
|
| 531 |
+
await litert.loadLiteRt(`${LITERT}/wasm/`);
|
| 532 |
+
} catch (error) {
|
| 533 |
+
console.error(error);
|
| 534 |
+
throw new Error('LiteRT.js did not load. Check the connection and reload the page.');
|
| 535 |
+
}
|
| 536 |
+
const device = litert.getWebGpuDevice();
|
| 537 |
+
device.lost.then((loss) => {
|
| 538 |
+
if (loss.reason !== 'destroyed') stop('The GPU stopped responding. Reload the page to start again.');
|
| 539 |
+
});
|
| 540 |
+
const info = device.adapterInfo ?? adapter.info ?? {};
|
| 541 |
+
gpu = info.description || [info.vendor, info.architecture].filter(Boolean).join(' ');
|
| 542 |
+
return device.features.has('shader-f16');
|
| 543 |
+
}
|
| 544 |
+
|
| 545 |
+
const keyOf = ({model, precision}) => `${model} ${precision}`;
|
| 546 |
+
const needed = (file) => file === MODELS[want.model].file || (want.metric && file === MODELS.fov.file);
|
| 547 |
+
|
| 548 |
+
class DownloadError extends Error {}
|
| 549 |
+
|
| 550 |
+
// The bytes of a model file: from what this browser stored on an earlier visit, or downloaded
|
| 551 |
+
// and stored for the next one.
|
| 552 |
+
async function download(model) {
|
| 553 |
+
if (held.has(model.file)) return held.get(model.file);
|
| 554 |
+
const url = FILES + model.file;
|
| 555 |
+
const megabytes = (bytes) => Math.round(bytes / 1e6);
|
| 556 |
+
const store = await globalThis.caches?.open('depthpro2-litert').catch(() => null);
|
| 557 |
+
const stored = await store?.match(url).catch(() => null);
|
| 558 |
+
if (stored) {
|
| 559 |
+
progress(`Loading ${model.label}`);
|
| 560 |
+
const bytes = await stored.arrayBuffer().catch(() => null);
|
| 561 |
+
if (bytes?.byteLength === model.bytes) {
|
| 562 |
+
held.set(model.file, new Uint8Array(bytes));
|
| 563 |
+
return held.get(model.file);
|
| 564 |
+
}
|
| 565 |
+
await store.delete(url).catch(() => {});
|
| 566 |
+
}
|
| 567 |
+
transfer = {file: model.file, controller: new AbortController()};
|
| 568 |
+
try {
|
| 569 |
+
progress(`Downloading ${model.label}: 0 of ${megabytes(model.bytes)} MB`, 0);
|
| 570 |
+
const response = await fetch(url, {signal: transfer.controller.signal});
|
| 571 |
+
if (!response.ok) throw new Error(`${url}: ${response.status}`);
|
| 572 |
+
const [mine, theirs] = response.body.tee();
|
| 573 |
+
if (store) store.put(url, new Response(theirs)).catch(() => {});
|
| 574 |
+
else theirs.cancel();
|
| 575 |
+
const bytes = new Uint8Array(model.bytes);
|
| 576 |
+
const reader = mine.getReader();
|
| 577 |
+
let received = 0;
|
| 578 |
+
for (;;) {
|
| 579 |
+
const {done, value} = await reader.read();
|
| 580 |
+
if (done) break;
|
| 581 |
+
if (received + value.length > bytes.length) throw new Error(`${url}: longer than expected`);
|
| 582 |
+
bytes.set(value, received);
|
| 583 |
+
received += value.length;
|
| 584 |
+
progress(`Downloading ${model.label}: ${megabytes(received)} of ${megabytes(model.bytes)} MB`, received / model.bytes);
|
| 585 |
+
}
|
| 586 |
+
if (received !== bytes.length) throw new Error(`${url}: shorter than expected`);
|
| 587 |
+
held.set(model.file, bytes);
|
| 588 |
+
return bytes;
|
| 589 |
+
} catch (error) {
|
| 590 |
+
if (error?.name === 'AbortError') throw error;
|
| 591 |
+
console.error(error);
|
| 592 |
+
throw new DownloadError(model.label);
|
| 593 |
+
} finally {
|
| 594 |
+
transfer = null;
|
| 595 |
+
}
|
| 596 |
+
}
|
| 597 |
+
|
| 598 |
+
async function compile(model, bytes, precision) {
|
| 599 |
+
const compiled = await litert.loadAndCompile(bytes, {accelerator: 'webgpu', gpuOptions: {precision}});
|
| 600 |
+
held.delete(model.file);
|
| 601 |
+
return compiled;
|
| 602 |
+
}
|
| 603 |
+
|
| 604 |
+
// Runs a model of one input and one output; the milliseconds include reading the output back.
|
| 605 |
+
async function infer(model, values, shape) {
|
| 606 |
+
const tensor = new litert.Tensor(values, shape);
|
| 607 |
+
const begun = performance.now();
|
| 608 |
+
const [onGpu] = await model.run(tensor);
|
| 609 |
+
const onHost = await onGpu.moveTo('wasm');
|
| 610 |
+
const output = onHost.toTypedArray().slice();
|
| 611 |
+
const milliseconds = performance.now() - begun;
|
| 612 |
+
tensor.delete();
|
| 613 |
+
onHost.delete();
|
| 614 |
+
return {output, milliseconds};
|
| 615 |
+
}
|
| 616 |
+
|
| 617 |
+
// ---- Pictures -----------------------------------------------------------------------------
|
| 618 |
+
|
| 619 |
+
// Draws an image or a video frame scaled to cover width x height, cropped to its center.
|
| 620 |
+
function cover(context, width, height, image, mirrored) {
|
| 621 |
+
const canvas = context.canvas;
|
| 622 |
+
if (canvas.width !== width || canvas.height !== height) Object.assign(canvas, {width, height});
|
| 623 |
+
const [w, h] = [image.videoWidth ?? image.width, image.videoHeight ?? image.height];
|
| 624 |
+
const scale = Math.max(width / w, height / h);
|
| 625 |
+
context.imageSmoothingQuality = 'high';
|
| 626 |
+
context.setTransform(mirrored ? -1 : 1, 0, 0, 1, mirrored ? width : 0, 0);
|
| 627 |
+
context.drawImage(image, (width - w * scale) / 2, (height - h * scale) / 2, w * scale, h * scale);
|
| 628 |
+
context.setTransform(1, 0, 0, 1, 0, 0);
|
| 629 |
+
}
|
| 630 |
+
|
| 631 |
+
// For each of `to` pixels along a line of `from`, the first pixel it averages and their weights:
|
| 632 |
+
// a triangle as wide as the step between pixels, as in Pillow's and PyTorch's antialiased bilinear.
|
| 633 |
+
function weights(from, to) {
|
| 634 |
+
const step = from / to;
|
| 635 |
+
const reach = Math.max(step, 1);
|
| 636 |
+
return Array.from({length: to}, (unused, index) => {
|
| 637 |
+
const center = (index + 0.5) * step;
|
| 638 |
+
const first = Math.max(0, Math.floor(center - reach));
|
| 639 |
+
const row = new Float32Array(Math.min(from, Math.ceil(center + reach)) - first);
|
| 640 |
+
let sum = 0;
|
| 641 |
+
for (let k = 0; k < row.length; k++) sum += row[k] = Math.max(0, 1 - Math.abs(first + k + 0.5 - center) / reach);
|
| 642 |
+
return {first, row: row.map((weight) => weight / sum)};
|
| 643 |
+
});
|
| 644 |
+
}
|
| 645 |
+
|
| 646 |
+
// Resizes pixels of `channels` values each, of which the first three are RGB, to RGB of
|
| 647 |
+
// toWidth x toHeight. Written out because the original code resizes this way and the field of
|
| 648 |
+
// view it estimates moves by several percent with a browser's own filter.
|
| 649 |
+
function resized(pixels, width, height, channels, toWidth, toHeight) {
|
| 650 |
+
const across = weights(width, toWidth);
|
| 651 |
+
const narrowed = new Float32Array(3 * toWidth * height);
|
| 652 |
+
for (let y = 0; y < height; y++) {
|
| 653 |
+
for (let x = 0; x < toWidth; x++) {
|
| 654 |
+
const {first, row} = across[x];
|
| 655 |
+
let r = 0;
|
| 656 |
+
let g = 0;
|
| 657 |
+
let b = 0;
|
| 658 |
+
for (let k = 0, at = (y * width + first) * channels; k < row.length; k++, at += channels) {
|
| 659 |
+
r += row[k] * pixels[at];
|
| 660 |
+
g += row[k] * pixels[at + 1];
|
| 661 |
+
b += row[k] * pixels[at + 2];
|
| 662 |
+
}
|
| 663 |
+
const to = 3 * (y * toWidth + x);
|
| 664 |
+
narrowed[to] = r;
|
| 665 |
+
narrowed[to + 1] = g;
|
| 666 |
+
narrowed[to + 2] = b;
|
| 667 |
+
}
|
| 668 |
+
}
|
| 669 |
+
const down = weights(height, toHeight);
|
| 670 |
+
const result = new Float32Array(3 * toWidth * toHeight);
|
| 671 |
+
for (let y = 0; y < toHeight; y++) {
|
| 672 |
+
const {first, row} = down[y];
|
| 673 |
+
for (let x = 0; x < toWidth; x++) {
|
| 674 |
+
let r = 0;
|
| 675 |
+
let g = 0;
|
| 676 |
+
let b = 0;
|
| 677 |
+
for (let k = 0, at = 3 * (first * toWidth + x); k < row.length; k++, at += 3 * toWidth) {
|
| 678 |
+
r += row[k] * narrowed[at];
|
| 679 |
+
g += row[k] * narrowed[at + 1];
|
| 680 |
+
b += row[k] * narrowed[at + 2];
|
| 681 |
+
}
|
| 682 |
+
const to = 3 * (y * toWidth + x);
|
| 683 |
+
result[to] = r;
|
| 684 |
+
result[to + 1] = g;
|
| 685 |
+
result[to + 2] = b;
|
| 686 |
+
}
|
| 687 |
+
}
|
| 688 |
+
return result;
|
| 689 |
+
}
|
| 690 |
+
|
| 691 |
+
// Takes a still or the camera's current frame: for the photo side at up to twice the frame's
|
| 692 |
+
// size, and as the frame itself, resized from the middle of the picture in its own pixels.
|
| 693 |
+
function grab(image, mirrored = false) {
|
| 694 |
+
const [w, h] = [image.videoWidth ?? image.width, image.videoHeight ?? image.height];
|
| 695 |
+
const sharper = Math.min(2, Math.max(1, Math.min(w / WIDTH, h / HEIGHT)));
|
| 696 |
+
cover(capture, Math.round(WIDTH * sharper), Math.round(HEIGHT * sharper), image, mirrored);
|
| 697 |
+
const [cropWidth, cropHeight] = [Math.min(w, Math.round(h * WIDTH / HEIGHT)), Math.min(h, Math.round(w * HEIGHT / WIDTH))];
|
| 698 |
+
// Beyond 4 times the frame the browser shrinks the picture first, to keep the resize quick.
|
| 699 |
+
const shrink = Math.min(1, 4 * WIDTH / cropWidth);
|
| 700 |
+
const [width, height] = [Math.round(cropWidth * shrink), Math.round(cropHeight * shrink)];
|
| 701 |
+
if (crop.canvas.width !== width || crop.canvas.height !== height) Object.assign(crop.canvas, {width, height});
|
| 702 |
+
crop.imageSmoothingQuality = 'high';
|
| 703 |
+
crop.setTransform(mirrored ? -1 : 1, 0, 0, 1, mirrored ? width : 0, 0);
|
| 704 |
+
crop.drawImage(image, Math.floor((w - cropWidth) / 2), Math.floor((h - cropHeight) / 2), cropWidth, cropHeight, 0, 0, width, height);
|
| 705 |
+
crop.setTransform(1, 0, 0, 1, 0, 0);
|
| 706 |
+
frame = resized(crop.getImageData(0, 0, width, height).data, width, height, 4, WIDTH, HEIGHT);
|
| 707 |
+
}
|
| 708 |
+
|
| 709 |
+
function present() {
|
| 710 |
+
const {width, height} = capture.canvas;
|
| 711 |
+
if (photo.canvas.width !== width || photo.canvas.height !== height) Object.assign(photo.canvas, {width, height});
|
| 712 |
+
photo.drawImage(capture.canvas, 0, 0);
|
| 713 |
+
}
|
| 714 |
+
|
| 715 |
+
// RGB pixels as the models take them: planes (1, 3, height, columns) in [0, 1], from the
|
| 716 |
+
// column `left` on.
|
| 717 |
+
function planes(pixels, width, height, left = 0, columns = width) {
|
| 718 |
+
const size = columns * height;
|
| 719 |
+
const values = new Float32Array(3 * size);
|
| 720 |
+
for (let y = 0; y < height; y++) {
|
| 721 |
+
for (let x = 0; x < columns; x++) {
|
| 722 |
+
const from = 3 * (y * width + left + x);
|
| 723 |
+
const to = y * columns + x;
|
| 724 |
+
values[to] = pixels[from] / 255;
|
| 725 |
+
values[size + to] = pixels[from + 1] / 255;
|
| 726 |
+
values[2 * size + to] = pixels[from + 2] / 255;
|
| 727 |
+
}
|
| 728 |
+
}
|
| 729 |
+
return values;
|
| 730 |
+
}
|
| 731 |
+
|
| 732 |
+
// What the field-of-view model takes: the frame resized to 864x480, then its middle 832 columns.
|
| 733 |
+
function fieldOfViewInput() {
|
| 734 |
+
const widened = resized(frame, WIDTH, HEIGHT, 3, FOV_WIDTH, HEIGHT);
|
| 735 |
+
return planes(widened, FOV_WIDTH, HEIGHT, (FOV_WIDTH - FOV_CROP) / 2, FOV_CROP);
|
| 736 |
+
}
|
| 737 |
+
|
| 738 |
+
function paint(inverse, scale) {
|
| 739 |
+
const pixels = depthPixels.data;
|
| 740 |
+
const step = 256 / scale;
|
| 741 |
+
for (let i = 0; i < inverse.length; i++) {
|
| 742 |
+
const color = 3 * Math.min(255, Math.max(0, (inverse[i] * step) | 0));
|
| 743 |
+
pixels[4 * i] = TURBO[color];
|
| 744 |
+
pixels[4 * i + 1] = TURBO[color + 1];
|
| 745 |
+
pixels[4 * i + 2] = TURBO[color + 2];
|
| 746 |
+
pixels[4 * i + 3] = 255;
|
| 747 |
+
}
|
| 748 |
+
depthContext.putImageData(depthPixels, 0, 0);
|
| 749 |
+
}
|
| 750 |
+
|
| 751 |
+
// ---- The work -----------------------------------------------------------------------------
|
| 752 |
+
|
| 753 |
+
// Does whatever the controls and the photo now ask for that is not done yet. A change that
|
| 754 |
+
// arrives meanwhile is taken up when this one finishes, or cuts short a download it makes useless.
|
| 755 |
+
let running = false;
|
| 756 |
+
let again = false;
|
| 757 |
+
|
| 758 |
+
async function refresh() {
|
| 759 |
+
again = true;
|
| 760 |
+
if (running || stopped || !ready) return;
|
| 761 |
+
running = true;
|
| 762 |
+
while (again && !stopped) {
|
| 763 |
+
again = false;
|
| 764 |
+
try {
|
| 765 |
+
await step();
|
| 766 |
+
settle();
|
| 767 |
+
} catch (error) {
|
| 768 |
+
if (error?.name === 'AbortError') continue;
|
| 769 |
+
endCamera();
|
| 770 |
+
if (error instanceof DownloadError) {
|
| 771 |
+
notify(`Downloading ${error.message} failed. Check the connection, then try again.`, 'Try again', () => {
|
| 772 |
+
$('notice').hidden = true;
|
| 773 |
+
refresh();
|
| 774 |
+
});
|
| 775 |
+
} else {
|
| 776 |
+
console.error(error);
|
| 777 |
+
stop('The GPU runtime stopped, most likely out of memory. Reload the page to start again.');
|
| 778 |
+
}
|
| 779 |
+
break;
|
| 780 |
+
}
|
| 781 |
+
if (source?.live) {
|
| 782 |
+
again = true;
|
| 783 |
+
await new Promise(requestAnimationFrame);
|
| 784 |
+
}
|
| 785 |
+
}
|
| 786 |
+
running = false;
|
| 787 |
+
}
|
| 788 |
+
|
| 789 |
+
async function step() {
|
| 790 |
+
const key = keyOf(want);
|
| 791 |
+
if (loaded.depth?.key !== key) {
|
| 792 |
+
const model = MODELS[want.model];
|
| 793 |
+
const bytes = await download(model);
|
| 794 |
+
if (keyOf(want) !== key) return;
|
| 795 |
+
progress(`Preparing ${model.label} for your GPU`);
|
| 796 |
+
// One depth model at a time: LiteRT.js aborts when it holds too many.
|
| 797 |
+
loaded.depth?.compiled.delete();
|
| 798 |
+
loaded.depth = null;
|
| 799 |
+
loaded.depth = {key, compiled: await compile(model, bytes, want.precision), warm: false};
|
| 800 |
+
if (keyOf(want) !== key) return;
|
| 801 |
+
}
|
| 802 |
+
if (want.metric && !loaded.fov) {
|
| 803 |
+
const bytes = await download(MODELS.fov);
|
| 804 |
+
if (!want.metric) return;
|
| 805 |
+
progress(`Preparing ${MODELS.fov.label} for your GPU`);
|
| 806 |
+
// In float32 always: float16 moves its angles, and with them every distance, by up to 2%.
|
| 807 |
+
loaded.fov = await compile(MODELS.fov, bytes, 'fp32');
|
| 808 |
+
}
|
| 809 |
+
|
| 810 |
+
const shot = source;
|
| 811 |
+
if (!shot) return;
|
| 812 |
+
// Whether this step is of a camera frame, even if the camera stops while it runs.
|
| 813 |
+
const live = shot.live;
|
| 814 |
+
if (live) grab(video, shot.mirrored);
|
| 815 |
+
if (want.metric && !shot.focal) {
|
| 816 |
+
if (!live) progress('Estimating the field of view');
|
| 817 |
+
const {output} = await infer(loaded.fov, fieldOfViewInput(), [1, 3, HEIGHT, FOV_CROP]);
|
| 818 |
+
if (shot !== source) return;
|
| 819 |
+
// Its second output is the vertical angle, in degrees.
|
| 820 |
+
shot.focal = 0.5 * HEIGHT / Math.tan(output[1] * Math.PI / 360);
|
| 821 |
+
}
|
| 822 |
+
if (live || depth?.shot !== shot || depth.key !== key) {
|
| 823 |
+
if (!live) progress('Estimating depth');
|
| 824 |
+
const input = planes(frame, WIDTH, HEIGHT);
|
| 825 |
+
let run = await infer(loaded.depth.compiled, input, [1, 3, HEIGHT, WIDTH]);
|
| 826 |
+
if (shot !== source) return;
|
| 827 |
+
show(shot, key, run, live);
|
| 828 |
+
if (!loaded.depth.warm && !live) {
|
| 829 |
+
// The first run of a model also sets up the GPU: the depth is shown, and a second run times it.
|
| 830 |
+
depth.milliseconds = null;
|
| 831 |
+
$('progress').hidden = true;
|
| 832 |
+
render();
|
| 833 |
+
run = await infer(loaded.depth.compiled, input, [1, 3, HEIGHT, WIDTH]);
|
| 834 |
+
if (depth.shot === shot && depth.key === key) depth.milliseconds = run.milliseconds;
|
| 835 |
+
}
|
| 836 |
+
loaded.depth.warm = true;
|
| 837 |
+
}
|
| 838 |
+
render();
|
| 839 |
+
}
|
| 840 |
+
|
| 841 |
+
function show(shot, key, run, live) {
|
| 842 |
+
const inverse = run.output;
|
| 843 |
+
let nearest = 0;
|
| 844 |
+
for (let i = 0; i < inverse.length; i++) if (inverse[i] > nearest) nearest = inverse[i];
|
| 845 |
+
let scale = nearest > 0 ? nearest * HEADROOM : 1;
|
| 846 |
+
// A camera's colors follow the scene slowly, not frame by frame.
|
| 847 |
+
if (live && shot.scale) scale = shot.scale + 0.15 * (scale - shot.scale);
|
| 848 |
+
const now = performance.now();
|
| 849 |
+
if (live && shot.shownAt) {
|
| 850 |
+
const rate = 1000 / (now - shot.shownAt);
|
| 851 |
+
shot.rate = shot.rate ? shot.rate + 0.3 * (rate - shot.rate) : rate;
|
| 852 |
+
}
|
| 853 |
+
Object.assign(shot, {scale, shownAt: now});
|
| 854 |
+
depth = {shot, key, inverse, scale, milliseconds: run.milliseconds};
|
| 855 |
+
paint(inverse, scale);
|
| 856 |
+
if (live) present();
|
| 857 |
+
if (!shot.revealed) {
|
| 858 |
+
shot.revealed = true;
|
| 859 |
+
reveal();
|
| 860 |
+
}
|
| 861 |
+
}
|
| 862 |
+
|
| 863 |
+
// ---- The divider --------------------------------------------------------------------------
|
| 864 |
+
|
| 865 |
+
let sweep = 0;
|
| 866 |
+
|
| 867 |
+
function split(percent) {
|
| 868 |
+
const value = Math.min(100, Math.max(0, percent));
|
| 869 |
+
viewer.style.setProperty('--split', value);
|
| 870 |
+
$('divider').setAttribute('aria-valuenow', Math.round(value));
|
| 871 |
+
$('tag-photo').hidden = value < 14;
|
| 872 |
+
$('tag-depth').hidden = value > 86;
|
| 873 |
+
}
|
| 874 |
+
|
| 875 |
+
// The photo gives way to its depth, from the right edge to where the divider rests.
|
| 876 |
+
function reveal() {
|
| 877 |
+
viewer.dataset.depth = '';
|
| 878 |
+
cancelAnimationFrame(sweep);
|
| 879 |
+
if (calm.matches) return split(resting);
|
| 880 |
+
const begun = performance.now();
|
| 881 |
+
const move = (now) => {
|
| 882 |
+
const part = Math.min(1, Math.max(0, (now - begun) / 900));
|
| 883 |
+
split(part === 1 ? resting : 100 + (resting - 100) * (1 - 2 ** (-10 * part)));
|
| 884 |
+
if (part < 1) sweep = requestAnimationFrame(move);
|
| 885 |
+
};
|
| 886 |
+
sweep = requestAnimationFrame(move);
|
| 887 |
+
}
|
| 888 |
+
|
| 889 |
+
function drag(percent) {
|
| 890 |
+
cancelAnimationFrame(sweep);
|
| 891 |
+
resting = Math.min(100, Math.max(0, percent));
|
| 892 |
+
split(resting);
|
| 893 |
+
}
|
| 894 |
+
|
| 895 |
+
// ---- Reading the depth --------------------------------------------------------------------
|
| 896 |
+
|
| 897 |
+
const distance = (meters) => meters >= 1000 ? `${(meters / 1000).toFixed(1)} km`
|
| 898 |
+
: `${meters.toFixed(meters < 10 ? 2 : meters < 100 ? 1 : 0)} m`;
|
| 899 |
+
|
| 900 |
+
// Meters of an inverse depth, for the photo on screen, once its field of view is estimated.
|
| 901 |
+
function metersOf(inverse) {
|
| 902 |
+
return want.metric && depth?.shot.focal ? depth.shot.focal / (WIDTH * inverse) : null;
|
| 903 |
+
}
|
| 904 |
+
|
| 905 |
+
function render() {
|
| 906 |
+
const current = depth && depth.shot === source ? depth : null;
|
| 907 |
+
const ticks = $('ticks');
|
| 908 |
+
const tick = (at, text, edge) => {
|
| 909 |
+
const mark = Object.assign(document.createElement('span'), {textContent: text, className: edge ?? ''});
|
| 910 |
+
mark.style.left = `${100 * at}%`;
|
| 911 |
+
ticks.append(mark);
|
| 912 |
+
};
|
| 913 |
+
ticks.replaceChildren();
|
| 914 |
+
const nearest = current && metersOf(current.scale / HEADROOM);
|
| 915 |
+
if (nearest) {
|
| 916 |
+
// Like a lens's distance scale: even steps of inverse depth, so the far marks crowd toward infinity.
|
| 917 |
+
const width = ticks.clientWidth || 600;
|
| 918 |
+
tick(0, distance(nearest), 'first');
|
| 919 |
+
let free = 70 / width;
|
| 920 |
+
for (const meters of [0.1, 0.2, 0.3, 0.5, 1, 2, 3, 5, 10, 20, 30, 50, 100, 200, 500]) {
|
| 921 |
+
const at = 1 - nearest / meters;
|
| 922 |
+
if (at < free || at > 1 - 28 / width) continue;
|
| 923 |
+
tick(at, String(meters));
|
| 924 |
+
free = at + 34 / width;
|
| 925 |
+
}
|
| 926 |
+
tick(1, '∞', 'last infinity');
|
| 927 |
+
} else {
|
| 928 |
+
tick(0, 'Near', 'first');
|
| 929 |
+
tick(1, 'Far', 'last');
|
| 930 |
+
}
|
| 931 |
+
|
| 932 |
+
let status = '';
|
| 933 |
+
if (current) {
|
| 934 |
+
const [model, precision] = current.key.split(' ');
|
| 935 |
+
status = `Model ${MODELS[model].name} in ${precision === 'fp16' ? 'float16' : 'float32'}`;
|
| 936 |
+
if (current.shot.live && current.shot.rate) status += `: ${current.shot.rate.toFixed(1)} frames a second`;
|
| 937 |
+
if (!current.shot.live && current.milliseconds) status += `: ${Math.round(current.milliseconds)} ms`;
|
| 938 |
+
status += gpu ? ` on ${gpu}.` : '.';
|
| 939 |
+
if (want.metric && current.shot.focal) {
|
| 940 |
+
const angle = (pixels) => {
|
| 941 |
+
const degrees = 2 * Math.atan(pixels / (2 * current.shot.focal)) * 180 / Math.PI;
|
| 942 |
+
return degrees.toFixed(degrees < 10 ? 1 : 0);
|
| 943 |
+
};
|
| 944 |
+
status += ` Estimated field of view: ${angle(WIDTH)}° by ${angle(HEIGHT)}°.`;
|
| 945 |
+
}
|
| 946 |
+
}
|
| 947 |
+
$('status').textContent = status;
|
| 948 |
+
probe();
|
| 949 |
+
}
|
| 950 |
+
|
| 951 |
+
// The depth under the pointer: a mark on the scale, and the distance when it is known.
|
| 952 |
+
function probe() {
|
| 953 |
+
const current = depth && depth.shot === source && viewer.dataset.depth !== undefined ? depth : null;
|
| 954 |
+
viewer.toggleAttribute('data-probing', Boolean(current));
|
| 955 |
+
const shown = Boolean(current && pointer);
|
| 956 |
+
$('index').hidden = $('spot').hidden = !shown;
|
| 957 |
+
$('probe').hidden = true;
|
| 958 |
+
if (!shown) return;
|
| 959 |
+
const column = Math.min(WIDTH - 1, Math.floor(pointer.x * WIDTH));
|
| 960 |
+
const row = Math.min(HEIGHT - 1, Math.floor(pointer.y * HEIGHT));
|
| 961 |
+
const inverse = Math.max(0, current.inverse[row * WIDTH + column]) || 0;
|
| 962 |
+
$('index').style.left = `${100 * Math.max(0, 1 - inverse / (current.scale / HEADROOM))}%`;
|
| 963 |
+
const box = viewer.getBoundingClientRect();
|
| 964 |
+
const [x, y] = [pointer.x * box.width, pointer.y * box.height];
|
| 965 |
+
$('spot').style.transform = `translate(${x}px, ${y}px)`;
|
| 966 |
+
const meters = metersOf(inverse);
|
| 967 |
+
if (meters === null) return;
|
| 968 |
+
const label = $('probe');
|
| 969 |
+
label.hidden = false;
|
| 970 |
+
label.textContent = Number.isFinite(meters) ? distance(meters) : '∞';
|
| 971 |
+
const left = x + 14 + label.offsetWidth > box.width ? x - 14 - label.offsetWidth : x + 14;
|
| 972 |
+
const top = y - 12 - label.offsetHeight < 0 ? y + 14 : y - 12 - label.offsetHeight;
|
| 973 |
+
label.style.transform = `translate(${left}px, ${top}px)`;
|
| 974 |
+
}
|
| 975 |
+
|
| 976 |
+
// ---- What is looked at --------------------------------------------------------------------
|
| 977 |
+
|
| 978 |
+
function endCamera() {
|
| 979 |
+
if (!source?.live) return;
|
| 980 |
+
// The last frame stays, as a still, with its depth.
|
| 981 |
+
source.live = false;
|
| 982 |
+
for (const track of video.srcObject?.getTracks() ?? []) track.stop();
|
| 983 |
+
video.srcObject = null;
|
| 984 |
+
$('camera').setAttribute('aria-pressed', 'false');
|
| 985 |
+
$('camera-label').textContent = 'Camera';
|
| 986 |
+
$('tag-photo').textContent = 'Photo';
|
| 987 |
+
render();
|
| 988 |
+
}
|
| 989 |
+
|
| 990 |
+
function look(next, sample) {
|
| 991 |
+
endCamera();
|
| 992 |
+
source = next;
|
| 993 |
+
pointer = null;
|
| 994 |
+
cancelAnimationFrame(sweep);
|
| 995 |
+
delete viewer.dataset.depth;
|
| 996 |
+
split(100);
|
| 997 |
+
for (const button of $('samples').children) button.setAttribute('aria-pressed', String(button.dataset.sample === sample));
|
| 998 |
+
warn('');
|
| 999 |
+
render();
|
| 1000 |
+
refresh();
|
| 1001 |
+
}
|
| 1002 |
+
|
| 1003 |
+
async function open(blob, sample, turn = ++asked) {
|
| 1004 |
+
let image;
|
| 1005 |
+
try {
|
| 1006 |
+
image = await createImageBitmap(blob, {imageOrientation: 'from-image'});
|
| 1007 |
+
} catch {
|
| 1008 |
+
return warn('This browser cannot read that file as a photo.');
|
| 1009 |
+
}
|
| 1010 |
+
if (turn !== asked) return image.close();
|
| 1011 |
+
grab(image);
|
| 1012 |
+
present();
|
| 1013 |
+
image.close();
|
| 1014 |
+
look({}, sample);
|
| 1015 |
+
}
|
| 1016 |
+
|
| 1017 |
+
async function choose(sample) {
|
| 1018 |
+
const turn = ++asked;
|
| 1019 |
+
try {
|
| 1020 |
+
const response = await fetch(`samples/${sample}.jpg`);
|
| 1021 |
+
if (!response.ok) throw new Error(response.status);
|
| 1022 |
+
await open(await response.blob(), sample, turn);
|
| 1023 |
+
} catch {
|
| 1024 |
+
warn('That sample photo did not load.');
|
| 1025 |
+
}
|
| 1026 |
+
}
|
| 1027 |
+
|
| 1028 |
+
async function camera() {
|
| 1029 |
+
if (source?.live) return endCamera();
|
| 1030 |
+
let stream;
|
| 1031 |
+
try {
|
| 1032 |
+
stream = await navigator.mediaDevices.getUserMedia({
|
| 1033 |
+
video: {facingMode: {ideal: 'environment'}, width: {ideal: 1280}, height: {ideal: 720}},
|
| 1034 |
+
});
|
| 1035 |
+
video.srcObject = stream;
|
| 1036 |
+
await video.play();
|
| 1037 |
+
} catch (error) {
|
| 1038 |
+
for (const track of stream?.getTracks() ?? []) track.stop();
|
| 1039 |
+
return warn(error?.name === 'NotAllowedError'
|
| 1040 |
+
? 'The camera is blocked for this page. Allow it in the browser\'s site settings, then press Camera again.'
|
| 1041 |
+
: 'No camera could be started.');
|
| 1042 |
+
}
|
| 1043 |
+
// A camera that faces its user shows them as a mirror does.
|
| 1044 |
+
const mirrored = stream.getVideoTracks()[0].getSettings().facingMode !== 'environment';
|
| 1045 |
+
grab(video, mirrored);
|
| 1046 |
+
present();
|
| 1047 |
+
look({live: true, mirrored});
|
| 1048 |
+
$('camera').setAttribute('aria-pressed', 'true');
|
| 1049 |
+
$('camera-label').textContent = 'Stop camera';
|
| 1050 |
+
$('tag-photo').textContent = 'Camera';
|
| 1051 |
+
}
|
| 1052 |
+
|
| 1053 |
+
// ---- Controls -----------------------------------------------------------------------------
|
| 1054 |
+
|
| 1055 |
+
function changed() {
|
| 1056 |
+
if (transfer && !needed(transfer.file)) transfer.controller.abort();
|
| 1057 |
+
for (const file of held.keys()) if (!needed(file)) held.delete(file);
|
| 1058 |
+
render();
|
| 1059 |
+
refresh();
|
| 1060 |
+
}
|
| 1061 |
+
|
| 1062 |
+
for (const [sample, label] of SAMPLES) {
|
| 1063 |
+
const button = Object.assign(document.createElement('button'), {type: 'button'});
|
| 1064 |
+
button.dataset.sample = sample;
|
| 1065 |
+
button.setAttribute('aria-pressed', 'false');
|
| 1066 |
+
button.append(Object.assign(new Image(), {src: `samples/${sample}-thumb.jpg`, alt: label, width: 320, height: 180}));
|
| 1067 |
+
button.addEventListener('click', () => choose(sample));
|
| 1068 |
+
$('samples').append(button);
|
| 1069 |
+
}
|
| 1070 |
+
for (const input of document.querySelectorAll('input[name="model"], input[name="precision"]')) {
|
| 1071 |
+
input.addEventListener('change', () => {
|
| 1072 |
+
want[input.name] = input.value;
|
| 1073 |
+
changed();
|
| 1074 |
+
});
|
| 1075 |
+
}
|
| 1076 |
+
$('metric').addEventListener('change', () => {
|
| 1077 |
+
want.metric = $('metric').checked;
|
| 1078 |
+
changed();
|
| 1079 |
+
});
|
| 1080 |
+
$('file').addEventListener('change', () => {
|
| 1081 |
+
const [file] = $('file').files;
|
| 1082 |
+
if (file) open(file);
|
| 1083 |
+
$('file').value = '';
|
| 1084 |
+
});
|
| 1085 |
+
if (navigator.mediaDevices?.getUserMedia) $('camera').addEventListener('click', camera);
|
| 1086 |
+
else $('camera').disabled = true;
|
| 1087 |
+
|
| 1088 |
+
// A photo dropped or pasted anywhere.
|
| 1089 |
+
const dropped = (transferred) => [...(transferred?.files ?? [])].find((file) => file.type.startsWith('image/'));
|
| 1090 |
+
addEventListener('dragover', (event) => {
|
| 1091 |
+
event.preventDefault();
|
| 1092 |
+
viewer.classList.add('dropping');
|
| 1093 |
+
});
|
| 1094 |
+
addEventListener('dragleave', (event) => {
|
| 1095 |
+
if (!event.relatedTarget) viewer.classList.remove('dropping');
|
| 1096 |
+
});
|
| 1097 |
+
addEventListener('drop', (event) => {
|
| 1098 |
+
event.preventDefault();
|
| 1099 |
+
viewer.classList.remove('dropping');
|
| 1100 |
+
const file = dropped(event.dataTransfer);
|
| 1101 |
+
if (file && !stopped) open(file);
|
| 1102 |
+
});
|
| 1103 |
+
addEventListener('paste', (event) => {
|
| 1104 |
+
const file = dropped(event.clipboardData);
|
| 1105 |
+
if (file && !stopped) open(file);
|
| 1106 |
+
});
|
| 1107 |
+
|
| 1108 |
+
// Dragging sideways anywhere on the picture moves the divider; pointing reads the depth.
|
| 1109 |
+
let press = null;
|
| 1110 |
+
const place = (event) => {
|
| 1111 |
+
const box = viewer.getBoundingClientRect();
|
| 1112 |
+
return {
|
| 1113 |
+
x: Math.min(1, Math.max(0, (event.clientX - box.left) / box.width)),
|
| 1114 |
+
y: Math.min(1, Math.max(0, (event.clientY - box.top) / box.height)),
|
| 1115 |
+
};
|
| 1116 |
+
};
|
| 1117 |
+
viewer.addEventListener('pointerdown', (event) => {
|
| 1118 |
+
if (viewer.dataset.depth === undefined || event.button !== 0 || event.target.closest('.notice')) return;
|
| 1119 |
+
const onDivider = Boolean(event.target.closest('.divider'));
|
| 1120 |
+
press = {id: event.pointerId, x: event.clientX, dragging: onDivider};
|
| 1121 |
+
viewer.setPointerCapture(event.pointerId);
|
| 1122 |
+
pointer = onDivider ? null : place(event);
|
| 1123 |
+
probe();
|
| 1124 |
+
});
|
| 1125 |
+
viewer.addEventListener('pointermove', (event) => {
|
| 1126 |
+
if (press?.id === event.pointerId) {
|
| 1127 |
+
if (Math.abs(event.clientX - press.x) > 5) press.dragging = true;
|
| 1128 |
+
if (press.dragging) {
|
| 1129 |
+
pointer = null;
|
| 1130 |
+
probe();
|
| 1131 |
+
return drag(100 * place(event).x);
|
| 1132 |
+
}
|
| 1133 |
+
}
|
| 1134 |
+
if (event.pointerType !== 'mouse' && !press) return;
|
| 1135 |
+
pointer = event.target.closest('.divider, .notice') ? null : place(event);
|
| 1136 |
+
probe();
|
| 1137 |
+
});
|
| 1138 |
+
for (const type of ['pointerup', 'pointercancel']) {
|
| 1139 |
+
viewer.addEventListener(type, (event) => {
|
| 1140 |
+
if (press?.id === event.pointerId) press = null;
|
| 1141 |
+
});
|
| 1142 |
+
}
|
| 1143 |
+
viewer.addEventListener('pointerleave', (event) => {
|
| 1144 |
+
if (event.pointerType !== 'mouse' || press) return;
|
| 1145 |
+
pointer = null;
|
| 1146 |
+
probe();
|
| 1147 |
+
});
|
| 1148 |
+
$('divider').addEventListener('keydown', (event) => {
|
| 1149 |
+
const moves = {ArrowLeft: -2, ArrowRight: 2, ArrowDown: -2, ArrowUp: 2, PageDown: -10, PageUp: 10, Home: -100, End: 100};
|
| 1150 |
+
if (!(event.key in moves)) return;
|
| 1151 |
+
event.preventDefault();
|
| 1152 |
+
drag(resting + moves[event.key] * (event.shiftKey ? 5 : 1));
|
| 1153 |
+
});
|
| 1154 |
+
new ResizeObserver(render).observe($('ticks'));
|
| 1155 |
+
|
| 1156 |
+
// ---- Start --------------------------------------------------------------------------------
|
| 1157 |
+
|
| 1158 |
+
const ramp = $('ramp').getContext('2d');
|
| 1159 |
+
const colors = ramp.createImageData(213, 1);
|
| 1160 |
+
for (let i = 0; i < 213; i++) colors.data.set([...TURBO.subarray(3 * (212 - i), 3 * (213 - i)), 255], 4 * i);
|
| 1161 |
+
ramp.putImageData(colors, 0, 0);
|
| 1162 |
+
render();
|
| 1163 |
+
choose(SAMPLES[0][0]);
|
| 1164 |
+
|
| 1165 |
+
try {
|
| 1166 |
+
progress('Starting');
|
| 1167 |
+
const float16 = await start();
|
| 1168 |
+
if (!float16) {
|
| 1169 |
+
// Nothing to choose: without the shader-f16 feature LiteRT computes in float32.
|
| 1170 |
+
want.precision = 'fp32';
|
| 1171 |
+
document.querySelector('input[value="fp16"]').disabled = true;
|
| 1172 |
+
document.querySelector('input[value="fp32"]').checked = true;
|
| 1173 |
+
$('precision-hint').textContent = 'This GPU or browser has no float16 shaders, so the models run in float32.';
|
| 1174 |
+
}
|
| 1175 |
+
if (/AppleWebKit/.test(navigator.userAgent) && !/Chrome|Chromium|Edg\//.test(navigator.userAgent)) {
|
| 1176 |
+
standing = 'Safari\'s WebGPU gives wrong depth with these models (seen in Safari 27.2). Chrome and Firefox on a computer work.';
|
| 1177 |
+
warn('');
|
| 1178 |
+
}
|
| 1179 |
+
if (navigator.connection?.saveData) {
|
| 1180 |
+
notify(`This runs after a ${Math.round(MODELS.s.bytes / 1e6)} MB download, and data saving is on.`, 'Download and run', () => {
|
| 1181 |
+
$('notice').hidden = true;
|
| 1182 |
+
ready = true;
|
| 1183 |
+
refresh();
|
| 1184 |
+
});
|
| 1185 |
+
} else {
|
| 1186 |
+
ready = true;
|
| 1187 |
+
refresh();
|
| 1188 |
+
}
|
| 1189 |
+
} catch (error) {
|
| 1190 |
+
stopped = true;
|
| 1191 |
+
$('panel').inert = true;
|
| 1192 |
+
notify(error.message);
|
| 1193 |
+
}
|
| 1194 |
+
</script>
|
| 1195 |
+
</body>
|
| 1196 |
</html>
|
samples/forest-thumb.jpg
ADDED
|
|
samples/forest.jpg
ADDED
|
Git LFS Details
|
samples/kitchen-thumb.jpg
ADDED
|
|
samples/kitchen.jpg
ADDED
|
Git LFS Details
|
samples/macaques-thumb.jpg
ADDED
|
|
samples/macaques.jpg
ADDED
|
Git LFS Details
|
samples/mountains-thumb.jpg
ADDED
|
|
samples/mountains.jpg
ADDED
|
Git LFS Details
|