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 = `
${sc ? `persona ${sc.person.toFixed(2)} · vajilla ${sc.dish.toFixed(2)}` : 'sin analizar'}
${st ? STATES[st].label : '—'}
`; el.querySelector('.name').textContent = z.name; box.appendChild(el); }); if (S.scores) { $('summary').innerHTML = `${counts.limpiar} por limpiar · ${counts.ocupada} ocupadas · ${counts.libre} libres` + (S.lastLatency ? ` · inferencia ${(S.lastLatency / 1000).toFixed(2)} s (${S.device})` : '') + (S.lastTime != null ? ` · t=${S.lastTime.toFixed(1)} s` : ''); const r = $('results'); r.dataset.states = JSON.stringify(Object.fromEntries(S.zones.map((z) => [z.name, S.states[z.name]]))); r.dataset.latencyMs = String(Math.round(S.lastLatency || 0)); r.dataset.device = S.device || ''; } else $('summary').textContent = S.zones.length ? `${S.zones.length} zonas definidas. Pulsa «Analizar».` : 'Dibuja zonas de mesa sobre la imagen.'; $('export-state').disabled = !S.scores; } function updateButtons() { $('analyze-btn').disabled = !(S.ready && S.source && S.zones.length && !S.busy); $('stop-btn').hidden = !(S.busy && S.source?.kind === 'video'); } function log(msg) { const li = document.createElement('li'); li.textContent = msg; $('log').prepend(li); } // ---------- Análisis ---------- function applyScores(scores, isVideo, t) { S.scores = scores; scores.forEach((sc) => { const raw = decide(sc, S.th); if (isVideo) { const s = smooth(S.smoothState[sc.name], raw, 2); S.smoothState[sc.name] = s; if (s.changed) log(`t=${t.toFixed(1)} s · ${sc.name} → ${STATES[s.shown].label}`); S.states[sc.name] = s.shown; } else S.states[sc.name] = raw; }); } async function analyzeImage() { const src = S.source; const t0 = performance.now(); const r = await detectOn(src.el, src.W, src.H); S.frameDims = [r.W, r.H]; S.lastLatency = r.latencyMs; S.lastTime = null; applyScores(scoreZones(r.detections, S.zones, r.W, r.H, PERSON_QUERIES, DISH_QUERIES), false); log(`Imagen analizada en ${((performance.now() - t0) / 1000).toFixed(2)} s · ${S.zones.map((z) => `${z.name}: ${STATES[S.states[z.name]].label}`).join(' · ')}`); } function seek(t) { return new Promise((res) => { video.onseeked = () => res(); video.currentTime = t; }); } async function analyzeVideo() { const interval = Math.max(0.5, Math.min(30, +$('interval').value || 2)); const times = sampleTimes(video.duration, interval, MAX_FRAMES); S.smoothState = {}; S.states = {}; log(`Vídeo: ${times.length} fotogramas cada ${interval} s (suavizado: 2 lecturas iguales para cambiar de estado)`); for (const t of times) { if (S.stop) { log('Análisis detenido.'); break; } await seek(t); const r = await detectOn(video, video.videoWidth, video.videoHeight); S.frameDims = [r.W, r.H]; S.lastLatency = r.latencyMs; S.lastTime = t; applyScores(scoreZones(r.detections, S.zones, r.W, r.H, PERSON_QUERIES, DISH_QUERIES), true, t); render(); renderBoard(); } } async function analyze() { if (S.busy || !S.ready || !S.source || !S.zones.length) return; S.busy = true; S.stop = false; updateButtons(); $('results').dataset.status = 'running'; try { if (S.source.kind === 'image') await analyzeImage(); else await analyzeVideo(); $('results').dataset.status = 'ok'; $('results').dataset.exampleId = S.source.exampleId || 'upload'; } catch (e) { $('results').dataset.status = 'error'; log('Error: ' + e.message); } S.busy = false; updateButtons(); render(); renderBoard(); } // ---------- Ejemplos y entradas ---------- function loadImg(url) { return new Promise((res, rej) => { const im = new Image(); im.onload = () => res(im); im.onerror = () => rej(new Error('No se pudo leer la imagen.')); im.src = url; }); } // Los ejemplos se sirven con redirección a un CDN de otro origen: leerlos como blob evita que el canvas quede 'tainted'. async function blobUrl(path) { const r = await fetch(new URL(path, document.baseURI).href); if (!r.ok) throw new Error('No se pudo descargar el ejemplo (' + r.status + ').'); return URL.createObjectURL(await r.blob()); } async function runExample(ex) { const im = await loadImg(await blobUrl(ex.file)); S.zones = ex.zones.map((z) => ({ name: z.name, box: [...z.box] })); setSource({ kind: 'image', el: im, W: im.naturalWidth, H: im.naturalHeight, exampleId: ex.id }); if (S.ready) await analyze(); else { S.pendingExample = ex; loadModel(); } } EXAMPLES.forEach((ex) => { const b = document.createElement('button'); b.dataset.example = ex.id; b.innerHTML = ``; b.querySelector('span').textContent = ex.title; b.onclick = () => runExample(ex); $('examples').appendChild(b); }); $('img-input').onchange = async (e) => { const f = e.target.files[0]; e.target.value = ''; if (!f) return; if (!['image/jpeg', 'image/png', 'image/webp'].includes(f.type)) return alert('Formato no admitido: usa JPG, PNG o WebP.'); if (f.size > MAX_IMAGE_BYTES) return alert('La imagen supera 15 MB.'); const url = URL.createObjectURL(f); try { const im = await loadImg(url); if (im.naturalWidth * im.naturalHeight > 40e6) return alert('La imagen supera 40 MP.'); S.zones = []; setSource({ kind: 'image', el: im, W: im.naturalWidth, H: im.naturalHeight }); } catch (err) { alert(err.message); } }; function openVideo(url, zones, exampleId) { if (video.src.startsWith('blob:')) URL.revokeObjectURL(video.src); video.src = url; return new Promise((res) => { video.onloadeddata = async () => { if (video.duration > MAX_VIDEO_SECONDS) { alert('El vídeo dura más de 3 minutos.'); return res(false); } await seek(0); S.zones = zones; setSource({ kind: 'video', el: video, W: video.videoWidth, H: video.videoHeight, exampleId }); log(`Vídeo cargado: ${video.duration.toFixed(1)} s, ${video.videoWidth}×${video.videoHeight}. ${zones.length ? '' : ' Dibuja las zonas sobre el primer fotograma.'}`); res(true); }; video.onerror = () => { alert('El navegador no puede decodificar este vídeo.'); res(false); }; }); } $('vid-input').onchange = (e) => { const f = e.target.files[0]; e.target.value = ''; if (!f) return; if (f.size > MAX_VIDEO_BYTES) return alert('El vídeo supera 60 MB.'); openVideo(URL.createObjectURL(f), []); }; { const b = document.createElement('button'); b.dataset.example = 'video-comedor-1'; b.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();