import { MODEL_ID, MODEL_REVISION, MODEL_SHA256, PERSON_QUERIES, DISH_QUERIES, DEFAULT_THRESHOLDS, MAX_IMAGE_BYTES, MAX_VIDEO_BYTES, MAX_VIDEO_SECONDS, MAX_FRAMES, MAX_SIDE, MAX_ZONES } from './config.js'; import { EXAMPLES, VIDEO_EXAMPLE } from './zones.js'; import { STATES, normalizeBox, scoreZones, decide, smooth, sampleTimes, fitSize } from './lib.js'; const $ = (id) => document.getElementById(id); const canvas = $('canvas'), ctx = canvas.getContext('2d'); const video = $('video'); const LABELS = [...PERSON_QUERIES, ...DISH_QUERIES]; const S = { worker: null, ready: false, device: null, busy: false, stop: false, source: null, // {kind:'image'|'video', el, W, H, exampleId} zones: [], // [{name, box:[x,y,w,h] normalizado}] scores: null, // último scoreZones frameDims: null, // [W,H] del frame inferido smoothState: {}, // por mesa (vídeo) states: {}, // estado mostrado por mesa th: { ...DEFAULT_THRESHOLDS }, draft: null, lastLatency: null, lastTime: null, }; // ---------- Modelo (Web Worker) ---------- let reqId = 0; const pending = new Map(); function makeWorker() { const w = new Worker(new URL('./worker.js', import.meta.url), { type: 'module' }); w.onmessage = (ev) => onWorker(ev.data); return w; } const fileProg = {}; function onWorker(m) { if (m.type === 'progress') { const d = m.data; if (d.file && d.total) fileProg[d.file] = [d.loaded || 0, d.total]; const tot = Object.values(fileProg).reduce((a, [, t]) => a + t, 0), got = Object.values(fileProg).reduce((a, [l]) => a + l, 0); if (tot) { $('load-progress').value = (100 * got) / tot; $('model-status').textContent = `Descargando… ${(got / 1048576).toFixed(0)} / ${(tot / 1048576).toFixed(0)} MiB`; } } else if (m.type === 'ready') { S.ready = true; S.device = m.device; $('load-progress').hidden = true; $('model-status').textContent = `Modelo listo · ${m.device === 'webgpu' ? 'WebGPU' : `WASM/CPU (${m.threads} hilo${m.threads > 1 ? 's' : ''})`}${m.fallbackReason ? ' · WebGPU falló, se usa WASM' : ''}`; $('load-btn').disabled = true; updateButtons(); if (S.pendingExample) { const e = S.pendingExample; S.pendingExample = null; runExample(e); } else if (S.pendingVideo) { S.pendingVideo = false; analyze(); } } else if (m.type === 'webgpu-failed') { S.worker.terminate(); S.worker = makeWorker(); S.worker.postMessage({ type: 'load', wantWebGPU: false, wasmBase: wasmBase(), fallbackReason: m.reason }); } else if (m.type === 'result') { pending.get(m.id)?.resolve(m); pending.delete(m.id); } else if (m.type === 'error') { if (m.id != null && pending.has(m.id)) { pending.get(m.id).reject(new Error(m.message)); pending.delete(m.id); } else { $('model-status').textContent = `Error al cargar: ${m.message}`; $('load-btn').disabled = false; } } } const wasmBase = () => new URL('./wasm/', document.baseURI).href; function loadModel() { if (S.worker) return; $('load-btn').disabled = true; $('load-progress').hidden = false; $('model-status').textContent = 'Preparando descarga…'; S.worker = makeWorker(); S.worker.postMessage({ type: 'load', wantWebGPU: $('webgpu-chk').checked, wasmBase: wasmBase() }); } function detectOn(drawable, w0, h0) { const [W, H] = fitSize(w0, h0, MAX_SIDE); const oc = new OffscreenCanvas(W, H), octx = oc.getContext('2d'); octx.drawImage(drawable, 0, 0, W, H); const data = octx.getImageData(0, 0, W, H); const id = ++reqId; return new Promise((resolve, reject) => { pending.set(id, { resolve: (m) => resolve({ ...m, W, H }), reject }); S.worker.postMessage({ type: 'detect', id, width: W, height: H, buffer: data.data.buffer, labels: LABELS, threshold: 0.05 }, [data.data.buffer]); }); } // ---------- Fuente y dibujo ---------- function setSource(src) { S.source = src; S.scores = null; S.states = {}; S.smoothState = {}; const W = 960, H = Math.round((960 * src.H) / src.W); canvas.width = W; canvas.height = H; $('log').innerHTML = ''; $('results').dataset.status = 'idle'; render(); renderBoard(); updateButtons(); } function render() { ctx.clearRect(0, 0, canvas.width, canvas.height); if (S.source) ctx.drawImage(S.source.el, 0, 0, canvas.width, canvas.height); else { ctx.fillStyle = '#ddd'; ctx.font = '20px system-ui'; ctx.fillText('Elige un ejemplo o sube una imagen/vídeo', 30, 50); } const cw = canvas.width, ch = canvas.height; S.zones.forEach((z, i) => { const st = S.states[z.name]; const col = st ? STATES[st].color : '#f59e0b'; const [x, y, w, h] = z.box; ctx.lineWidth = 4; ctx.strokeStyle = col; ctx.strokeRect(x * cw, y * ch, w * cw, h * ch); const label = `${z.name}${st ? ' · ' + STATES[st].label : ''}`; ctx.font = 'bold 16px system-ui'; const tw = ctx.measureText(label).width + 10; ctx.fillStyle = col; ctx.fillRect(x * cw, y * ch, tw, 24); ctx.fillStyle = '#fff'; ctx.fillText(label, x * cw + 5, y * ch + 17); if ($('show-dishes').checked && S.scores?.[i] && S.frameDims) { const [FW, FH] = S.frameDims; ctx.lineWidth = 2; ctx.strokeStyle = '#fde047'; for (const d of S.scores[i].dishes) if (d.score >= S.th.dish) ctx.strokeRect((d.box.xmin / FW) * cw, (d.box.ymin / FH) * ch, ((d.box.xmax - d.box.xmin) / FW) * cw, ((d.box.ymax - d.box.ymin) / FH) * ch); } }); if (S.draft) { const [x, y, w, h] = normalizeBox(S.draft); ctx.setLineDash([6, 4]); ctx.strokeStyle = '#f59e0b'; ctx.lineWidth = 3; ctx.strokeRect(x * cw, y * ch, w * cw, h * ch); ctx.setLineDash([]); } } function renderBoard() { const box = $('tables'); box.innerHTML = ''; const counts = { libre: 0, ocupada: 0, limpiar: 0 }; S.zones.forEach((z, i) => { const st = S.states[z.name]; if (st) counts[st]++; const sc = S.scores?.[i]; const el = document.createElement('div'); el.className = 'tcard'; el.style.borderLeftColor = st ? STATES[st].color : '#999'; el.innerHTML = `
Vídeo 12 s: la Mesa 3 se limpia';
b.onclick = async () => {
const ex = EXAMPLES[0];
const ok = await openVideo(await blobUrl(VIDEO_EXAMPLE), ex.zones.map((z) => ({ name: z.name, box: [...z.box] })), 'video-comedor-1');
if (!ok) return; if (S.ready) await analyze(); else { loadModel(); S.pendingVideo = true; }
};
$('examples').appendChild(b);
}
// ---------- Dibujo de zonas ----------
const pt = (e) => { const r = canvas.getBoundingClientRect(); return [(e.clientX - r.left) / r.width, (e.clientY - r.top) / r.height]; };
canvas.addEventListener('pointerdown', (e) => { if (e.button !== 0 || !S.source || S.busy) return; const [x, y] = pt(e); S.draft = [x, y, 0, 0]; canvas.setPointerCapture(e.pointerId); });
canvas.addEventListener('pointermove', (e) => { if (!S.draft) return; const [x, y] = pt(e); S.draft[2] = x - S.draft[0]; S.draft[3] = y - S.draft[1]; render(); });
canvas.addEventListener('pointerup', () => {
if (!S.draft) return; const box = normalizeBox(S.draft); S.draft = null;
if (box[2] > 0.02 && box[3] > 0.02) {
if (S.zones.length >= MAX_ZONES) alert(`Máximo ${MAX_ZONES} zonas.`);
else { let k = S.zones.length + 1; while (S.zones.some((z) => z.name === `Mesa ${k}`)) k++; S.zones.push({ name: `Mesa ${k}`, box }); S.scores = null; S.states = {}; }
}
render(); renderBoard(); updateButtons();
});
canvas.addEventListener('contextmenu', (e) => {
e.preventDefault(); const [x, y] = pt(e);
const i = S.zones.findIndex((z) => x >= z.box[0] && x <= z.box[0] + z.box[2] && y >= z.box[1] && y <= z.box[1] + z.box[3]);
if (i >= 0) { S.zones.splice(i, 1); S.scores = null; S.states = {}; render(); renderBoard(); updateButtons(); }
});
$('clear-zones').onclick = () => { S.zones = []; S.scores = null; S.states = {}; render(); renderBoard(); updateButtons(); };
const download = (name, obj) => { const a = document.createElement('a'); a.href = URL.createObjectURL(new Blob([JSON.stringify(obj, null, 2)], { type: 'application/json' })); a.download = name; a.click(); setTimeout(() => URL.revokeObjectURL(a.href), 1000); };
$('export-zones').onclick = () => download('zonas-mesas.json', { formato: 'x,y,w,h normalizados 0..1', zonas: S.zones });
$('import-zones').onchange = async (e) => {
const f = e.target.files[0]; e.target.value = ''; if (!f) return;
try {
const j = JSON.parse(await f.text()); const zs = (j.zonas || j.zones || j).slice(0, MAX_ZONES);
S.zones = zs.map((z, i) => ({ name: String(z.name || `Mesa ${i + 1}`).slice(0, 40), box: normalizeBox(z.box.map(Number)) }));
S.scores = null; S.states = {}; render(); renderBoard(); updateButtons();
} catch { alert('JSON de zonas no válido.'); }
};
$('export-state').onclick = () => download('estado-mesas.json', {
generado: new Date().toISOString(), modelo: MODEL_ID, revision: MODEL_REVISION, sha256: MODEL_SHA256, umbrales: S.th, t_video_s: S.lastTime,
mesas: S.zones.map((z, i) => ({ nombre: z.name, zona: z.box, estado: S.states[z.name] || null, p_persona: S.scores?.[i]?.person ?? null, p_vajilla: S.scores?.[i]?.dish ?? null })),
});
// ---------- Controles ----------
$('load-btn').onclick = loadModel;
$('analyze-btn').onclick = analyze;
$('stop-btn').onclick = () => { S.stop = true; };
$('show-dishes').onchange = render;
for (const k of ['person', 'dish']) {
$(`th-${k}`).oninput = (e) => {
S.th[k] = +e.target.value; $(`th-${k}-out`).textContent = S.th[k].toFixed(2);
if (S.scores && S.source?.kind === 'image') { applyScores(S.scores, false); render(); renderBoard(); }
};
}
render(); renderBoard();