File size: 13,164 Bytes
6989108
 
 
 
 
 
 
b0139c6
 
 
 
6989108
 
b0139c6
 
 
 
 
5b574e8
b0139c6
 
 
5c31b24
5b574e8
 
 
b0139c6
5b574e8
 
 
 
6989108
5b574e8
 
 
 
 
 
 
 
 
 
 
 
 
b0139c6
 
5b574e8
 
b0139c6
5b574e8
6989108
5b574e8
 
 
 
 
b0139c6
5b574e8
b0139c6
 
 
 
 
5b574e8
 
 
 
6989108
5b574e8
 
 
40b985d
b0139c6
5b574e8
40b985d
b0139c6
40b985d
 
 
b0139c6
40b985d
 
 
 
 
 
b0139c6
 
 
 
6989108
 
 
 
 
 
 
 
 
 
 
5b574e8
 
 
 
6989108
5b574e8
 
 
 
 
 
 
 
6989108
 
5b574e8
 
 
 
 
 
 
 
 
 
 
6989108
 
 
 
b0139c6
6989108
 
 
b0139c6
 
 
6989108
5b574e8
 
6989108
5b574e8
b0139c6
6989108
 
b0139c6
40b985d
2ec90db
6989108
 
2ec90db
40b985d
 
6989108
 
b0139c6
 
 
 
6989108
5b574e8
 
c0b2859
 
6989108
 
 
 
 
 
 
c0b2859
b0139c6
6989108
 
5c31b24
5b574e8
5c31b24
 
6989108
 
 
187c581
 
cf0ab54
 
 
187c581
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6989108
b0139c6
6989108
c0b2859
5b574e8
 
 
 
 
 
 
c0b2859
 
5b574e8
 
 
 
6989108
187c581
6989108
187c581
6989108
0540d00
8883091
896714d
5c31b24
 
 
896714d
c0b2859
 
 
 
 
 
 
 
 
8883091
5b574e8
6989108
40b985d
6989108
4a36639
5b574e8
 
4a36639
6989108
 
 
5b574e8
 
6989108
 
 
 
 
5b574e8
6989108
 
5b574e8
6989108
5b574e8
6989108
 
 
 
24f0b50
 
 
 
 
 
 
 
 
 
 
 
 
6989108
 
 
 
24f0b50
6989108
4a36639
 
6989108
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Depth Pro 2</title>
<style>
  :root {
    --text: #1d1d1f; --text-2: #6e6e73; --accent: #0071e3;
    --fill: #f5f5f7; --line: #e8e8ed;
  }
  * { box-sizing: border-box; }
  body {
    margin: 0; background: #fff; color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Inter, Roboto, sans-serif;
    -webkit-font-smoothing: antialiased;
    display: flex; flex-direction: column; align-items: center; padding: 56px 20px 72px;
  }
  header { text-align: center; max-width: 760px; margin-bottom: 44px; }
  h1 { font-size: clamp(48px, 9vw, 80px); font-weight: 700; letter-spacing: -0.035em; margin: 0; }
  .subtitle { font-size: clamp(19px, 3.2vw, 28px); font-weight: 600; color: var(--text-2); margin: 14px 0 24px; }
  .authors { font-size: 14px; color: var(--text-2); line-height: 1.7; margin: 0; }
  .affil { font-size: 13px; font-weight: 600; color: var(--text-2); margin: 10px 0 0; }
  .windows {
    width: min(1060px, 100%);
    display: grid; grid-template-columns: 1fr 1fr; gap: 20px;
  }
  @media (max-width: 720px) { .windows { grid-template-columns: 1fr; } }
  .win {
    position: relative; aspect-ratio: 4 / 3; border-radius: 24px; overflow: hidden;
    background: var(--fill); box-shadow: 0 30px 80px -20px rgba(0, 0, 0, 0.28), 0 2px 10px rgba(0, 0, 0, 0.06);
  }
  .win.dragover { outline: 3px dashed var(--accent); outline-offset: -12px; }
  .win.clickable { cursor: pointer; }
  .label {
    position: absolute; top: 14px; left: 14px; z-index: 4; padding: 6px 14px; border-radius: 980px;
    background: rgba(29, 29, 31, 0.75); color: #fff; font-size: 13px; font-weight: 600;
    pointer-events: none; backdrop-filter: blur(6px);
  }
  .win img, .win video {
    position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain;
    background: #0b0b12; user-select: none; -webkit-user-drag: none;
  }
  .pane {
    position: absolute; inset: 0; display: flex; flex-direction: column; gap: 10px;
    align-items: center; justify-content: center; text-align: center; padding: 20px;
  }
  .pane .glyph {
    width: 64px; height: 64px; border-radius: 20px; background: #fff;
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.08);
    display: flex; align-items: center; justify-content: center; font-size: 28px;
  }
  .pane strong { font-size: 18px; font-weight: 600; }
  .pane .hint { font-size: 14px; color: var(--text-2); max-width: 340px; }
  .pane .fine { font-size: 12px; color: #a1a1a6; }
  .pane .link { color: var(--accent); font-weight: 600; cursor: pointer; text-decoration: none; }
  .pane input { display: none; }
  .cta {
    margin-top: 6px; padding: 11px 24px; border-radius: 980px; border: none; cursor: pointer;
    background: var(--accent); color: #fff; font-size: 15px; font-weight: 500;
    font-family: inherit; transition: transform 0.15s ease, opacity 0.15s ease;
  }
  .cta:hover { opacity: 0.9; }
  .cta:active { transform: scale(0.97); }
  #outPlaceholder .spin {
    width: 34px; height: 34px; border-radius: 50%;
    border: 3px solid rgba(29, 29, 31, 0.15); border-top-color: var(--accent);
    animation: spin 0.9s linear infinite;
  }
  #outPlaceholder .spin { display: none; }
  #outPlaceholder.working .spin { display: block; }
  #outPlaceholder.working strong { color: var(--text-2); }
  #busy {
    position: absolute; inset: 0; z-index: 5; display: none;
    flex-direction: column; gap: 14px; align-items: center; justify-content: center;
    background: rgba(11, 11, 18, 0.55); color: #fff; font-size: 15px; font-weight: 600;
    text-align: center; padding: 20px; backdrop-filter: blur(3px);
  }
  #busy.on { display: flex; }
  #busy .spin {
    width: 38px; height: 38px; border-radius: 50%;
    border: 3px solid rgba(255, 255, 255, 0.25); border-top-color: #fff;
    animation: spin 0.9s linear infinite;
  }
  #busy.error { color: #ffb4b4; }
  #busy.error .spin { display: none; }
  @keyframes spin { to { transform: rotate(360deg); } }
  #status { min-height: 22px; margin: 20px 0 0; font-size: 14px; color: var(--text-2); text-align: center; }
  #status.error { color: #c02626; font-weight: 600; }
  footer { margin-top: 36px; font-size: 12px; color: #a1a1a6; text-align: center; line-height: 1.7; }
  footer a { color: var(--text-2); }
</style>
</head>
<body>
<header>
  <h1>Depth Pro 2</h1>
  <p class="subtitle">Sharp and Temporally Consistent Metric Monocular Depth</p>
  <p class="authors">Peiyun Hu, Marko Mihajlovic, Lyndon Duong, Lahav Lipson, Wei Dong,<br>
     Ama&euml;l Delaunoy, Stephan R. Richter, Vladlen Koltun</p>
  <p class="affil">Apple</p>
</header>

<div class="windows">
  <div class="win clickable" id="inWin">
    <span class="label">Input</span>
    <img id="srcImg" alt="input" hidden>
    <video id="srcVid" muted loop playsinline hidden></video>
    <div class="pane" id="drop">
      <div class="glyph">&#128247;</div>
      <strong>Drop an image or video</strong>
      <span class="hint">Metric depth for every pixel, in meters</span>
      <button class="cta" id="browse">Choose photo or video</button>
      <span class="fine">or try an <span class="link" id="example">example image</span> &middot;
        videos: first 60 frames, up to 960&nbsp;px</span>
      <input type="file" id="file" accept="image/*,video/*">
    </div>
  </div>
  <div class="win" id="outWin">
    <span class="label">Depth Pro 2</span>
    <img id="depthImg" alt="depth" hidden>
    <video id="depthVid" muted loop playsinline hidden></video>
    <div class="pane" id="outPlaceholder">
      <div class="spin"></div>
      <strong id="outTitle">Depth appears here</strong>
      <span class="hint">Red is near, blue is far, white is the infinitely far &mdash; distances in meters</span>
    </div>
    <div id="busy"><div class="spin"></div><span id="busyText"></span></div>
  </div>
</div>
<p id="status"></p>

<footer>
  Runs <a href="https://huggingface.co/apple/DepthPro2-H">apple/DepthPro2-H</a> (872M, bfloat16) on ZeroGPU &middot;
  <a href="https://github.com/apple-aiml-research/ml-depth-pro-2">research use only</a>
</footer>

<script>
  // UI wiring must not depend on any import: clicks always respond, even before or without the
  // Gradio client. The client is imported on demand inside the first request.
  const $ = (id) => document.getElementById(id);
  const inWin = $("inWin"), outWin = $("outWin");
  const drop = $("drop"), outPlaceholder = $("outPlaceholder"), outTitle = $("outTitle");
  const srcImg = $("srcImg"), srcVid = $("srcVid"), depthImg = $("depthImg"), depthVid = $("depthVid");
  const status = $("status");
  const busy = $("busy"), busyText = $("busyText");
  const fileInput = $("file");

  let clientPromise = null;

  function setStatus(text, error = false, spin = true) {
    status.textContent = text;
    status.classList.toggle("error", error);
    busy.classList.toggle("on", !error && spin && text !== "");
    busy.classList.toggle("error", error);
    busyText.textContent = text;
  }

  window.addEventListener("error", (e) => setStatus(`Script error: ${e.message}`, true));
  window.addEventListener("unhandledrejection", (e) =>
    setStatus(`Error: ${e.reason?.message || e.reason}`, true));

  function show(kind, sourceUrl, depthUrl) {
    drop.style.display = "none";
    outPlaceholder.style.display = "none";
    // play() rejections (e.g. a codec the browser lacks) must not surface as raw errors.
    const play = (el) => el.play?.().catch(() => {});
    const isVideo = kind === "video";
    srcImg.hidden = isVideo;  depthImg.hidden = isVideo;
    srcVid.hidden = !isVideo; depthVid.hidden = !isVideo;
    if (isVideo) {
      srcVid.src = sourceUrl; depthVid.src = depthUrl;
      const sync = () => { depthVid.currentTime = srcVid.currentTime; };
      srcVid.addEventListener("loadeddata", sync, { once: true });
      play(srcVid); play(depthVid);
      depthVid.onerror = () => setStatus("The depth video failed to load.", true);
    } else {
      srcImg.src = sourceUrl; depthImg.src = depthUrl;
      depthImg.onload = () =>
        setStatus(`\u2713 Depth loaded \u2014 click the input window to try another photo`, false, false);
      depthImg.onerror = () =>
        setStatus(`The depth output failed to decode (${Math.round(depthUrl.length / 1024)} KB received) \u2014 reload the page and try once more.`, true);
    }
  }

  const CLIENT_SOURCES = [
    "/gradio-client.js",
    "https://cdn.jsdelivr.net/npm/@gradio/client@2.7.1/dist/index.min.js",
    "https://unpkg.com/@gradio/client@2.7.1/dist/index.min.js",
    "https://cdn.statically.io/npm/@gradio/client@2.7.1/dist/index.min.js",
  ];
  async function loadClientModule() {
    for (const src of CLIENT_SOURCES) {
      try {
        return await import(src);
      } catch (err) {
        console.error("Client source failed:", src, err);
      }
    }
    throw new Error("Could not load the Gradio client from this Space or any CDN.");
  }

  let handleFile = null;
  async function getClient() {
    if (!clientPromise) {
      clientPromise = loadClientModule().then((m) => {
        handleFile = m.handle_file;
        return m.Client.connect(window.location.origin);
      });
    }
    return clientPromise;
  }

  async function processFile(file) {
    if (!file) return;
    const isVideo = file.type.startsWith("video/");
    let sourceUrl = URL.createObjectURL(file);
    // The input window shows the source immediately; the output window shows work in progress.
    drop.style.display = "none";
    outPlaceholder.style.display = "";
    outPlaceholder.classList.add("working");
    outTitle.textContent = `Estimating depth${isVideo ? " (first 60 frames)" : ""}\u2026`;
    if (isVideo) {
      srcImg.hidden = true; srcVid.hidden = false;
      srcVid.src = sourceUrl;
      srcVid.play?.().catch(() => {});
    } else {
      srcVid.hidden = true; srcImg.hidden = false;
      srcImg.src = sourceUrl;
    }
    try {
      const client = await getClient();
      const result = await client.predict(isVideo ? "/predict_depth_video" : "/predict_depth", {
        [isVideo ? "video" : "image"]: handleFile(file),
      });
      const payload = result.data?.[0];
      let depthUrl = typeof payload === "string" ? payload : payload?.image_data;
      if (!depthUrl || depthUrl === sourceUrl) {
        throw new Error(
          `The server returned no depth output \u2014 payload: ${JSON.stringify(payload)?.slice(0, 160)}`,
        );
      }
      if (isVideo) {
        // For videos the server also returns the input re-encoded to H.264: the original file is
        // often HEVC, which Chrome cannot decode, so play the server copy in the input window.
        if (payload?.source?.startsWith("data:")) {
          sourceUrl = URL.createObjectURL(await (await fetch(payload.source)).blob());
        }
        if (depthUrl.startsWith("data:")) {
          depthUrl = URL.createObjectURL(await (await fetch(depthUrl)).blob());
        }
      }
      outPlaceholder.classList.remove("working");
      show(isVideo ? "video" : "image", sourceUrl, depthUrl);
      setStatus("");
    } catch (err) {
      console.error(err);
      outPlaceholder.classList.remove("working");
      outTitle.textContent = "Depth appears here";
      setStatus(String(err?.message || err), true);
    }
  }

  $("browse").addEventListener("click", (e) => { e.stopPropagation(); fileInput.click(); });
  inWin.addEventListener("click", () => fileInput.click());
  fileInput.addEventListener("change", () => {
    if (fileInput.files.length) processFile(fileInput.files[0]);
    fileInput.value = "";
  });
  for (const type of ["dragenter", "dragover"]) {
    inWin.addEventListener(type, (e) => { e.preventDefault(); inWin.classList.add("dragover"); });
  }
  for (const type of ["dragleave", "drop"]) {
    inWin.addEventListener(type, (e) => { e.preventDefault(); inWin.classList.remove("dragover"); });
  }
  inWin.addEventListener("drop", (e) => {
    const file = e.dataTransfer?.files?.[0];
    if (file) processFile(file);
  });

  // Wikimedia's thumbnail endpoint returns HTTP 400 for this file, so fetch the original and
  // decode-validate it before sending — an error page saved as example.jpg once reached the server.
  const EXAMPLE_URL = "https://upload.wikimedia.org/wikipedia/commons/6/68/Orange_tabby_cat_sitting_on_fallen_leaves-Hisashi-01A.jpg";
  async function fetchExampleFile() {
    const blob = await (await fetch(EXAMPLE_URL)).blob();
    try {
      await createImageBitmap(blob);
    } catch (err) {
      throw new Error("The example image could not be fetched as a valid picture \u2014 try uploading one instead.");
    }
    return new File([blob], "example.jpg", { type: blob.type || "image/jpeg" });
  }

  $("example").addEventListener("click", async (e) => {
    e.stopPropagation();
    setStatus("Fetching the example image\u2026");
    try {
      await processFile(await fetchExampleFile());
    } catch (err) {
      console.error(err);
      setStatus(String(err?.message || err), true);
    }
  });
</script>
</body>
</html>