Segizu's picture
Geometría 3D Lab v1: MoGe-2 ViT-S en el navegador (medidas 3D métricas)
ab0a0fc verified
Raw History Blame Contribute Delete
15.5 kB
import './style.css';
import { MODEL, LIMITS, EXAMPLES, RUNTIME } from './config.js';
import { workSize, rgbaToNCHW, depthStats, depthToRGBA, normalsToRGBA, measure, calibrationFactor, toPLY, validateFile, validateDims } from './geometry.js';
import { PointViewer } from './viewer.js';
const $ = (s) => document.querySelector(s);
const fmtM = (m) => (m < 1 ? `${(m * 100).toFixed(1).replace('.', ',')} cm` : `${m.toFixed(2).replace('.', ',')} m`);
const fmtN = (x, d = 2) => x.toFixed(d).replace('.', ',');
const DISPLAY_MAX = 1600;
const worker = new Worker(new URL('./worker.js', import.meta.url), { type: 'module' });
const state = { loaded: false, busy: false, base: null, baseRGBA: null, workRGBA: null, geo: null, normal: null, stats: null,
factor: 1, measurements: [], pending: null, view: 'original', example: null, jobId: 0, device: null };
const viewer = new PointViewer($('#viewer3d'));
const results = $('#results');
$('#hdr-weights').textContent = `${(MODEL.bytes / 1e6).toFixed(1).replace('.', ',')} MB`;
$('#pin-model').textContent = `${MODEL.repo}@${MODEL.revision.slice(0, 7)}`;
$('#pin-ort').textContent = RUNTIME.ort;
$('#chk-webgpu').checked = 'gpu' in navigator;
$('#iso').textContent = self.crossOriginIsolated ? 'sí (WASM multihilo)' : 'no (WASM 1 hilo: abre la URL directa *.static.hf.space)';
for (const s of LIMITS.workSides) $('#sel-side').add(new Option(`${s} px`, s, s === LIMITS.defaultWorkSide, s === LIMITS.defaultWorkSide));
for (const t of LIMITS.tokens) $('#sel-tokens').add(new Option(`${t} tokens`, t, t === LIMITS.defaultTokens, t === LIMITS.defaultTokens));
// ---------- ejemplos ----------
const exBox = $('#examples');
for (const ex of EXAMPLES) {
const b = document.createElement('button');
b.className = 'example'; b.dataset.id = ex.id; b.disabled = true;
b.innerHTML = `<img src="${ex.file}" alt="" loading="lazy"><span>${ex.title}</span>`;
b.onclick = () => runExample(ex);
exBox.appendChild(b);
}
const setEnabled = () => {
const on = state.loaded && !state.busy;
document.querySelectorAll('.example').forEach((b) => (b.disabled = !on));
$('#file').disabled = !on; $('#btn-rerun').disabled = !on || !state.base;
$('#btn-load').disabled = state.loaded || state.busy;
};
// ---------- modelo ----------
$('#btn-load').onclick = () => {
state.busy = true; setEnabled();
setStatus('Descargando modelo…');
worker.postMessage({ type: 'load', webgpu: $('#chk-webgpu').checked, threads: Math.min(4, navigator.hardwareConcurrency || 4) });
};
function setStatus(t, cls = '') { const e = $('#model-status'); e.textContent = t; e.className = cls; }
const pending = new Map();
worker.onmessage = ({ data }) => {
if (data.type === 'progress') {
$('#progress').value = data.loaded / data.total;
setStatus(data.verifying ? 'Verificando SHA-256 y creando la sesión…' : `Descargando modelo… ${(data.loaded / 1e6).toFixed(0)} / ${(data.total / 1e6).toFixed(0)} MB`);
} else if (data.type === 'loaded') {
state.loaded = true; state.busy = false; state.device = data.device;
const dev = data.device === 'webgpu' ? 'WebGPU' : `WASM/CPU (${data.threads} hilo${data.threads > 1 ? 's' : ''})`;
setStatus(`Modelo listo · ${dev} · ${data.fromCache ? 'desde caché' : `descarga ${(data.downloadMs / 1000).toFixed(1)} s`} · sesión ${(data.initMs / 1000).toFixed(1)} s`, 'ok');
if (data.warn) $('#warn').textContent = data.warn;
$('#progress').value = 1; setEnabled();
} else if (data.type === 'result' || data.type === 'error') {
const p = pending.get(data.id); pending.delete(data.id);
if (data.type === 'error' && data.stage === 'load') { state.busy = false; setStatus(`No se pudo cargar el modelo: ${data.message}`, 'err'); setEnabled(); return; }
p?.(data);
}
};
const callInfer = (msg, transfer) => new Promise((res) => { const id = ++state.jobId; pending.set(id, res); worker.postMessage({ ...msg, id, type: 'infer' }, transfer); });
// ---------- imágenes ----------
async function bitmapFrom(src) {
const blob = src instanceof Blob ? src : await (await fetch(src)).blob();
return createImageBitmap(blob);
}
function drawTo(bmp, W, H) {
const c = new OffscreenCanvas(W, H), x = c.getContext('2d');
x.imageSmoothingQuality = 'high'; x.drawImage(bmp, 0, 0, W, H);
return x.getImageData(0, 0, W, H).data;
}
async function setBase(bmp) {
const [W, H] = workSize(bmp.width, bmp.height, DISPLAY_MAX);
state.base = { bmp, W, H };
state.baseRGBA = drawTo(bmp, W, H);
}
async function runExample(ex) {
results.dataset.status = 'running';
state.example = ex;
results.dataset.exampleId = ex.id;
$('#example-hint').textContent = ex.hint;
await setBase(await bitmapFrom(ex.file));
await analyze(ex.measurements);
$('#calib-hint').textContent = ex.calibrate ? ex.calibrate.note : 'Si conoces la longitud real de algún segmento medido, escríbela y pulsa «Calibrar» (se usa la última medida).';
$('#calib-known').value = ex.calibrate ? String(ex.calibrate.known).replace('.', ',') : '';
}
$('#file').onchange = async (e) => {
const f = e.target.files?.[0]; if (!f) return;
const err = validateFile(f, LIMITS);
if (err) return showError(err);
let bmp;
try { bmp = await createImageBitmap(f); } catch { return showError('No se pudo decodificar la imagen.'); }
const e2 = validateDims(bmp.width, bmp.height, LIMITS.maxPixels);
if (e2) return showError(e2);
results.dataset.status = 'running';
state.example = null; results.dataset.exampleId = 'upload';
$('#example-hint').textContent = `Imagen propia (${bmp.width}×${bmp.height}). Haz clic en dos puntos para medir.`;
$('#calib-hint').textContent = '';
await setBase(bmp);
await analyze([]);
};
$('#btn-rerun').onclick = () => analyze(state.measurements.map(({ label, a, b, expected, expectedNote }) => ({ label, a, b, expected, expectedNote })));
function showError(msg) { results.dataset.status = 'error'; $('#stats').innerHTML = `<p class="err">${msg}</p>`; }
async function analyze(presetMeasurements) {
state.busy = true; setEnabled();
results.dataset.status = 'running';
$('#stats').innerHTML = '<p>Calculando geometría 3D… (en CPU puede tardar decenas de segundos)</p>';
const side = +$('#sel-side').value, tokens = +$('#sel-tokens').value;
const [W, H] = workSize(state.base.W, state.base.H, side);
const t0 = performance.now();
state.workRGBA = drawTo(state.base.bmp, W, H);
const pixels = rgbaToNCHW(state.workRGBA, W, H);
const tPre = performance.now() - t0;
const r = await callInfer({ pixels: pixels.buffer, W, H, numTokens: tokens }, [pixels.buffer]);
state.busy = false; setEnabled();
if (r.type === 'error') return showError(`Error en la inferencia: ${r.message}`);
state.geo = r.geo; state.normal = r.normal; state.factor = 1; state.pending = null;
$('#calib-status').textContent = 'Sin calibrar (escala métrica del modelo).';
state.stats = depthStats(r.geo.depth);
state.measurements = presetMeasurements.map((m) => ({ ...m }));
state.timing = { preMs: tPre, inferMs: r.inferMs, postMs: r.postMs, device: r.device, tokens, side: `${W}×${H}` };
recompute();
renderStats();
draw();
viewer.setCloud(state.geo, state.workRGBA, state.factor, state.geo.fovY, state.stats.median);
viewer.setMeasurements(state.measurements, state.factor);
Object.assign(results.dataset, { status: 'ok', inferMs: Math.round(r.inferMs), postMs: Math.round(r.postMs), device: r.device,
fovX: state.geo.fovX.toFixed(2), scale: state.geo.scale.toFixed(4), work: `${W}x${H}` });
}
function recompute() {
for (const m of state.measurements) m.result = measure(state.geo, m.a, m.b, state.base.W, state.base.H, state.factor);
results.dataset.measurements = JSON.stringify(state.measurements.map((m) => ({ label: m.label, a: m.a, b: m.b, length: m.result?.length ?? null, depthA: m.result?.depthA, depthB: m.result?.depthB, expected: m.expected ?? null })));
results.dataset.factor = String(state.factor);
renderMeasurements();
}
function renderStats() {
const g = state.geo, s = state.stats, t = state.timing;
const gt = state.example?.gtFovX ? ` (verdad: ${state.example.gtFovX}°)` : '';
$('#stats').innerHTML = `
<p class="ok"><b>Inferencia real en tu navegador</b> · ${t.device === 'webgpu' ? 'WebGPU' : 'WASM/CPU'} · imagen de trabajo ${t.side} · ${t.tokens} tokens</p>
<ul>
<li>Campo de visión estimado: <b>${fmtN(g.fovX, 1)}° horizontal</b>${gt} · ${fmtN(g.fovY, 1)}° vertical · focal ≈ ${Math.round(g.fxPx * state.base.W / g.W)} px (en la imagen de ${state.base.W} px)</li>
<li>Profundidad (m${state.factor !== 1 ? ', calibrada' : ''}): mín ${fmtN(s.min * state.factor)} · mediana <b>${fmtN(s.median * state.factor)}</b> · p95 ${fmtN(s.p95 * state.factor)} · máx ${fmtN(s.max * state.factor)}</li>
<li>Píxeles válidos (máscara del modelo): ${fmtN(s.validPct, 1)} % · escala métrica predicha: ${fmtN(g.scale, 3)} · factor de calibración: ${fmtN(state.factor, 3)}</li>
<li>Tiempos: preproceso ${Math.round(t.preMs)} ms · <b>modelo ${Math.round(t.inferMs)} ms</b> · post-proceso ${Math.round(t.postMs)} ms</li>
</ul>`;
$('#legend').innerHTML = `<span>${fmtN(s.p05 * state.factor)} m (cerca)</span><span class="bar"></span><span>${fmtN(s.p95 * state.factor)} m (lejos)</span>`;
}
function renderMeasurements() {
const ul = $('#measure-list'); ul.innerHTML = '';
state.measurements.forEach((m, i) => {
const li = document.createElement('li');
const r = m.result;
const exp = m.expectedNote ? ` <small>(${m.expectedNote}${r && m.expected ? `; diferencia ${r.length >= m.expected ? '+' : ''}${fmtN((100 * (r.length - m.expected)) / m.expected, 0)} %` : ''})</small>` : '';
li.innerHTML = `<b>${i + 1}. ${m.label || 'Medida'}</b>: ${r ? `<span class="len">${fmtM(r.length)}</span> · prof. ${fmtN(r.depthA)} m → ${fmtN(r.depthB)} m` : 'sin datos válidos en esos píxeles'}${exp}`;
ul.appendChild(li);
});
$('#btn-calib').disabled = !state.measurements.some((m) => m.result);
}
// ---------- dibujo / interacción ----------
const canvas = $('#img-canvas'), ctx = canvas.getContext('2d');
document.querySelectorAll('.view-tab').forEach((b) => (b.onclick = () => { state.view = b.dataset.view; document.querySelectorAll('.view-tab').forEach((x) => x.classList.toggle('active', x === b)); draw(); }));
function draw() {
if (!state.base) return;
const { W, H } = state.base;
canvas.width = W; canvas.height = H;
$('#img-wrap').hidden = state.view === '3d'; $('#viewer3d').hidden = state.view !== '3d';
if (state.view === '3d') { viewer.resize(); return; }
if (state.view === 'original' || !state.geo) ctx.drawImage(state.base.bmp, 0, 0, W, H);
else {
const g = state.geo;
const rgba = state.view === 'depth' ? depthToRGBA(g.depth, state.stats) : normalsToRGBA(state.normal, g.valid);
const c = new OffscreenCanvas(g.W, g.H); c.getContext('2d').putImageData(new ImageData(rgba, g.W, g.H), 0, 0);
ctx.imageSmoothingEnabled = true; ctx.drawImage(c, 0, 0, W, H);
}
ctx.lineWidth = Math.max(2, W / 400); ctx.font = `${Math.max(14, Math.round(W / 55))}px system-ui, sans-serif`;
state.measurements.forEach((m, i) => {
ctx.strokeStyle = '#00e5ff'; ctx.fillStyle = '#00e5ff';
ctx.beginPath(); ctx.moveTo(...m.a); ctx.lineTo(...m.b); ctx.stroke();
for (const p of [m.a, m.b]) { ctx.beginPath(); ctx.arc(p[0], p[1], ctx.lineWidth * 2, 0, 7); ctx.fill(); }
const txt = `${i + 1}: ${m.result ? fmtM(m.result.length) : '—'}`;
const mx = (m.a[0] + m.b[0]) / 2 + 8, my = (m.a[1] + m.b[1]) / 2 - 8;
ctx.lineWidth = 4; ctx.strokeStyle = '#000'; ctx.strokeText(txt, mx, my); ctx.fillText(txt, mx, my); ctx.lineWidth = Math.max(2, W / 400);
});
if (state.pending) { ctx.fillStyle = '#ffeb3b'; ctx.beginPath(); ctx.arc(state.pending[0], state.pending[1], ctx.lineWidth * 2.5, 0, 7); ctx.fill(); }
}
canvas.addEventListener('click', (e) => {
if (!state.geo || state.busy) return;
const r = canvas.getBoundingClientRect();
const p = [Math.round(((e.clientX - r.left) / r.width) * canvas.width), Math.round(((e.clientY - r.top) / r.height) * canvas.height)];
if (!state.pending) { state.pending = p; draw(); return; }
if (state.measurements.length >= LIMITS.maxMeasurements) state.measurements.shift();
state.measurements.push({ label: 'Medida manual', a: state.pending, b: p });
state.pending = null; recompute(); draw(); viewer.setMeasurements(state.measurements, state.factor);
});
$('#btn-clear-meas').onclick = () => { state.measurements = []; state.pending = null; recompute(); draw(); viewer.setMeasurements([], 1); };
// ---------- calibración ----------
$('#btn-calib').onclick = () => {
const known = parseFloat(String($('#calib-known').value).replace(',', '.'));
const idx = state.example?.calibrate && state.measurements[state.example.calibrate.index]?.result ? state.example.calibrate.index : state.measurements.findLastIndex((m) => m.result);
const m = state.measurements[idx];
try {
const raw = m.result.length / state.factor;
state.factor = calibrationFactor(raw, known);
} catch (e) { return alert('Introduce una longitud real positiva en metros (p. ej. 0,94).'); }
$('#calib-status').textContent = `Calibrado con la medida ${idx + 1} = ${fmtM(known)} → factor ×${fmtN(state.factor, 3)} aplicado a toda la escena.`;
recompute(); renderStats(); draw();
viewer.setCloud(state.geo, state.workRGBA, state.factor, state.geo.fovY, state.stats.median); viewer.setMeasurements(state.measurements, state.factor);
};
$('#btn-calib-reset').onclick = () => { state.factor = 1; $('#calib-status').textContent = 'Sin calibrar (escala métrica del modelo).'; recompute(); renderStats(); draw();
if (state.geo) { viewer.setCloud(state.geo, state.workRGBA, 1, state.geo.fovY, state.stats.median); viewer.setMeasurements(state.measurements, 1); } };
// ---------- exportar ----------
const download = (blob, name) => { const a = document.createElement('a'); a.href = URL.createObjectURL(blob); a.download = name; a.click(); setTimeout(() => URL.revokeObjectURL(a.href), 5000); };
const baseName = () => `geometria3d-${state.example?.id || 'imagen'}`;
$('#btn-ply').onclick = () => { if (!state.geo) return; const { buffer } = toPLY(state.geo, state.workRGBA, state.factor, 1); download(new Blob([buffer]), `${baseName()}.ply`); };
$('#btn-png').onclick = () => { if (!state.geo) return; canvas.toBlob((b) => download(b, `${baseName()}-${state.view}.png`)); };
$('#btn-json').onclick = () => {
if (!state.geo) return;
const g = state.geo;
const rep = { app: 'Geometria3D Lab', model: `${MODEL.repo}@${MODEL.revision}`, runtime: `onnxruntime-web@${RUNTIME.ort}`, createdAt: new Date().toISOString(),
image: { id: state.example?.id || 'upload', displaySize: [state.base.W, state.base.H], workSize: [g.W, g.H] }, timing: state.timing,
camera: { fovXDeg: g.fovX, fovYDeg: g.fovY, fxPxWork: g.fxPx, fyPxWork: g.fyPx, shift: g.shift, metricScale: g.scale },
calibrationFactor: state.factor, depthStatsM: Object.fromEntries(Object.entries(state.stats).map(([k, v]) => [k, k === 'validPct' ? v : v * state.factor])),
measurements: state.measurements.map((m) => ({ label: m.label, aPx: m.a, bPx: m.b, lengthM: m.result?.length ?? null, expectedM: m.expected ?? null, a3d: m.result?.a3d, b3d: m.result?.b3d })) };
download(new Blob([JSON.stringify(rep, null, 2)], { type: 'application/json' }), `${baseName()}.json`);
};
setEnabled();