DepthPro2 / index.html
akhaliq's picture
akhaliq HF Staff
Fix mobile scroll: plain block layout, explicit overflow, tighter mobile header
68aa721 verified
Raw History Blame Contribute Delete
13.5 kB
<!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; }
html, body { overflow-x: hidden; }
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;
/* Plain block flow: some mobile browsers mis-measure a flex-column body and pin the page
to the viewport height, leaving nothing to scroll until the DOM changes. */
padding: 48px 20px 72px;
min-height: 100svh;
touch-action: pan-y;
}
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%); margin: 0 auto;
display: grid; grid-template-columns: 1fr 1fr; gap: 20px;
}
@media (max-width: 720px) {
.windows { grid-template-columns: 1fr; }
body { padding: 28px 16px 56px; }
header { margin-bottom: 24px; }
h1 { font-size: 40px; }
.subtitle { font-size: 18px; margin: 10px 0 16px; }
}
.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>