import { MODEL_ID, MODEL_REVISION, VARIANTS, variantBytes, EXAMPLES, VIDEO_EXAMPLE } from './config.js';
import { LIMITS, validateFile, fitSize, checkMegapixels, displayToImage, normBox, overlayMask, cutout, describeMask, sampleTimes, formatBytes } from './lib.js';
const $ = (id) => document.getElementById(id);
const els = {
variant: $('variant'), webgpu: $('chk-webgpu'), envHint: $('env-hint'), load: $('btn-load'), progWrap: $('progress-wrap'), prog: $('progress'), progText: $('progress-text'), modelStatus: $('model-status'),
examples: $('examples'), fileImage: $('file-image'), fileVideo: $('file-video'), mediaStatus: $('media-status'),
undo: $('btn-undo'), clear: $('btn-clear'), showOverlay: $('chk-overlay'), stage: $('stage'), results: $('results'), resultText: $('result-text'),
dlOverlay: $('btn-dl-overlay'), dlMask: $('btn-dl-mask'), dlCutout: $('btn-dl-cutout'),
videoCard: $('video-card'), fps: $('fps'), videoPlan: $('video-plan'), track: $('btn-track'), stop: $('btn-stop'), dlWebm: $('btn-dl-webm'),
trackWrap: $('track-wrap'), trackProg: $('track-progress'), trackText: $('track-text'), scrub: $('scrub'), scrubOut: $('scrub-out'),
};
const ctx = els.stage.getContext('2d', { willReadFrequently: true });
const WASM_BASE = new URL('./wasm/', document.baseURI).href;
// ---------------- state
const S = {
worker: null, ready: false, device: null, busy: false, msgId: 0,
media: null, // { kind:'image'|'video', key, width, height, rgba, exampleId, video?, times?, frameW?, frameH? }
points: [], box: null, mask: null, maskInfo: null, last: null,
dragStart: null, dragNow: null,
track: null, // { frames:[{rgba, mask, info}], index }
};
let keyCounter = 0;
for (const [k, v] of Object.entries(VARIANTS)) {
const o = document.createElement('option'); o.value = k; o.textContent = v.label; els.variant.append(o);
}
async function detectWebGPU() {
let ok = false, f16 = false;
try { const a = navigator.gpu && (await navigator.gpu.requestAdapter()); ok = !!a; f16 = !!a?.features?.has('shader-f16'); } catch {}
els.webgpu.checked = ok; els.webgpu.disabled = !ok;
els.envHint.textContent = ok
? `WebGPU disponible${f16 ? ' (con shader-f16)' : ' (sin shader-f16: si q4f16 falla, prueba int8)'}. Se usará la GPU de tu equipo.`
: 'WebGPU no disponible: se usará WASM (CPU). El codificador puede tardar de 30 s a varios minutos por imagen según tu CPU.';
if (!self.crossOriginIsolated) els.envHint.textContent += ' WASM en 1 hilo (página no aislada); abre el Space en su URL directa *.hf.space para usar hasta 4 hilos.';
}
detectWebGPU();
// ---------------- examples
function addExample(ex, thumb) {
const b = document.createElement('button');
b.type = 'button'; b.className = 'example'; b.dataset.id = ex.id; b.setAttribute('role', 'listitem'); b.setAttribute('aria-pressed', 'false');
b.innerHTML = `
${ex.title}
${ex.kind === 'video' ? 'Vídeo 4 s · seguimiento' : 'Imagen'}`;
b.addEventListener('click', () => runExample(ex));
els.examples.append(b);
}
EXAMPLES.forEach((ex) => addExample(ex, ex.file));
addExample(VIDEO_EXAMPLE, 'example-desk-pan-poster.jpg');
function markExample(id) { for (const b of els.examples.children) b.setAttribute('aria-pressed', String(b.dataset.id === id)); }
// ---------------- worker plumbing
function newWorker() {
S.worker?.terminate();
S.worker = new Worker(new URL('./worker.js', import.meta.url), { type: 'module' });
S.worker.addEventListener('message', onWorkerMessage);
}
const waiters = new Map();
function call(msg, transfer = []) {
const id = ++S.msgId;
return new Promise((resolve, reject) => { waiters.set(id, { resolve, reject }); S.worker.postMessage({ ...msg, id }, transfer); });
}
const fileProgress = new Map();
function onWorkerMessage(ev) {
const m = ev.data;
if (m.type === 'progress') {
const d = m.data;
if (d.file && d.total) fileProgress.set(d.file, [d.loaded || 0, d.total]);
if (d.status === 'done' && d.file && fileProgress.has(d.file)) { const [, t] = fileProgress.get(d.file); fileProgress.set(d.file, [t, t]); }
let l = 0, t = 0; for (const [a, b] of fileProgress.values()) { l += a; t += b; }
const expected = variantBytes(els.variant.value);
const total = Math.max(t, expected);
els.prog.value = total ? l / total : 0;
els.progText.textContent = `${formatBytes(l)} / ${formatBytes(total)}`;
return;
}
if (m.type === 'ready') { onReady(m); return; }
if (m.type === 'webgpu-failed') {
els.modelStatus.textContent = `WebGPU falló (${m.reason}). Reintentando con WASM…`;
newWorker(); S.worker.postMessage({ type: 'load', wantWebGPU: false, wasmBase: WASM_BASE, variant: els.variant.value, fallbackReason: m.reason });
return;
}
if (m.type === 'need-frame') { supplyFrame(m.index); return; }
if (m.type === 'track-frame') { onTrackFrame(m); return; }
if (m.type === 'error' && !m.id) { setModelError(m.message); return; }
if (m.type === 'error' && m.stage === 'load') { setModelError(m.message); return; }
const w = waiters.get(m.id);
if (!w) return;
waiters.delete(m.id);
m.type === 'error' ? w.reject(new Error(m.message)) : w.resolve(m);
}
function setModelError(msg) {
els.modelStatus.textContent = 'No se pudo cargar el modelo: ' + msg; els.modelStatus.classList.add('err');
els.load.disabled = false; els.progWrap.hidden = true;
}
els.load.addEventListener('click', () => {
els.load.disabled = true; els.variant.disabled = true; els.webgpu.disabled = true;
els.modelStatus.classList.remove('err'); fileProgress.clear();
els.progWrap.hidden = false; els.modelStatus.textContent = `Descargando ~${formatBytes(variantBytes(els.variant.value))} (sólo la primera vez; luego queda en caché)…`;
newWorker();
S.worker.postMessage({ type: 'load', wantWebGPU: els.webgpu.checked, wasmBase: WASM_BASE, variant: els.variant.value });
});
function onReady(m) {
S.ready = true; S.device = m.device;
els.progWrap.hidden = true;
const dev = m.device === 'webgpu' ? 'WebGPU' : `WASM/CPU (${m.threads} hilo${m.threads > 1 ? 's' : ''})`;
els.modelStatus.textContent = `Modelo listo · ${dev} · pesos ${m.variant}${m.fallbackReason ? ' · (WebGPU no funcionó)' : ''}`;
els.results.dataset.device = m.device;
if (S.media) analyse();
}
// ---------------- media loading
function setStageSize(w, h) { els.stage.width = w; els.stage.height = h; }
function resetPrompts() { S.points = []; S.box = null; S.mask = null; S.maskInfo = null; S.last = null; S.track = null; updateButtons(); }
async function loadImageSource(source, exampleId = null) {
const bmp = await createImageBitmap(source);
if (!checkMegapixels(bmp.width, bmp.height)) throw new Error(`Imagen demasiado grande (${(bmp.width * bmp.height / 1e6).toFixed(1)} MP > ${LIMITS.maxImageMegapixels} MP).`);
const { width, height } = fitSize(bmp.width, bmp.height, LIMITS.maxWorkSide);
setStageSize(width, height); ctx.drawImage(bmp, 0, 0, width, height); bmp.close?.();
const rgba = ctx.getImageData(0, 0, width, height).data;
stopTrackingUI(); resetPrompts();
S.media = { kind: 'image', key: 'k' + ++keyCounter, width, height, rgba, exampleId, encoded: false };
els.videoCard.hidden = true;
els.mediaStatus.textContent = `Imagen ${width}×${height} px${exampleId ? ' (ejemplo)' : ''}.`;
render();
}
async function loadVideoSource(url, exampleId = null) {
const video = document.createElement('video');
video.muted = true; video.playsInline = true; video.preload = 'auto'; video.src = url;
await new Promise((res, rej) => { video.onloadeddata = res; video.onerror = () => rej(new Error('El navegador no puede decodificar este vídeo.')); });
const { width, height } = fitSize(video.videoWidth, video.videoHeight, LIMITS.videoWorkSide);
stopTrackingUI(); resetPrompts();
S.media = { kind: 'video', key: 'k' + ++keyCounter, width, height, video, exampleId, url, encoded: false };
await seekDraw(0);
S.media.rgba = ctx.getImageData(0, 0, width, height).data;
els.videoCard.hidden = false;
updatePlan();
els.mediaStatus.textContent = `Vídeo ${video.videoWidth}×${video.videoHeight} (trabajo ${width}×${height}), ${video.duration.toFixed(1)} s${video.duration > LIMITS.maxVideoSeconds ? ` — sólo se procesan los primeros ${LIMITS.maxVideoSeconds} s` : ''}.`;
render();
}
function seekDraw(t) {
const { video, width, height } = S.media;
return new Promise((res, rej) => {
const done = () => { clearTimeout(to); setStageSize(width, height); ctx.drawImage(video, 0, 0, width, height); res(); };
const to = setTimeout(() => rej(new Error('Tiempo de espera al buscar en el vídeo.')), 10000);
if (Math.abs(video.currentTime - t) < 1e-3 && video.readyState >= 2) { done(); return; }
video.addEventListener('seeked', done, { once: true });
video.currentTime = t;
});
}
function updatePlan() {
if (S.media?.kind !== 'video') return;
const times = sampleTimes(S.media.video.duration, +els.fps.value);
S.media.times = times;
els.videoPlan.textContent = `${times.length} fotogramas a procesar (máx. ${LIMITS.maxVideoFrames}).`;
}
els.fps.addEventListener('change', updatePlan);
els.fileImage.addEventListener('change', async () => {
const f = els.fileImage.files[0]; els.fileImage.value = '';
if (!f) return;
const v = validateFile(f, 'image');
if (!v.ok) { els.mediaStatus.textContent = v.error; return; }
markExample(null);
try { await loadImageSource(f); if (S.ready) analyse(); } catch (e) { els.mediaStatus.textContent = e.message; }
});
els.fileVideo.addEventListener('change', async () => {
const f = els.fileVideo.files[0]; els.fileVideo.value = '';
if (!f) return;
const v = validateFile(f, 'video');
if (!v.ok) { els.mediaStatus.textContent = v.error; return; }
markExample(null);
try { await loadVideoSource(URL.createObjectURL(f)); if (S.ready) analyse(); } catch (e) { els.mediaStatus.textContent = e.message; }
});
async function runExample(ex) {
if (S.busy) return;
markExample(ex.id);
els.results.dataset.exampleId = ex.id; els.results.dataset.status = 'loading';
try {
// Fetch as a same-origin blob: on the Space, large files are redirected to the HF CDN and a cross-origin