File size: 23,995 Bytes
87b5db3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fc6a41
 
 
87b5db3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
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 = `<img alt="" loading="lazy" src="./examples/${thumb}"><span><strong>${ex.title}</strong><br><span class="kind">${ex.kind === 'video' ? 'Vídeo 4 s · seguimiento' : 'Imagen'}</span></span>`;
  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 <video>
    // would taint the canvas (getImageData would fail).
    if (ex.kind === 'video') await loadVideoSource(URL.createObjectURL(await (await fetch(`./examples/${ex.file}`)).blob()), ex.id);
    else {
      const blob = await (await fetch(`./examples/${ex.file}`)).blob();
      await loadImageSource(blob, ex.id);
    }
    S.points = ex.points.map((p) => ({ ...p })); S.box = ex.box ? [...ex.box] : null;
    if (ex.kind === 'video') els.fps.value = String(ex.fps), updatePlan();
    render();
    if (!S.ready) { els.resultText.textContent = 'Ejemplo cargado con sus puntos/caja. Carga el modelo (paso 1) para segmentar.'; els.results.dataset.status = 'idle'; return; }
    await analyse();
  } catch (e) { showError(e); }
}

// ---------------- inference
async function ensureEncoded() {
  const m = S.media;
  if (m.encoded) return;
  S.busy = true; updateButtons();
  els.results.dataset.status = 'encoding';
  els.resultText.textContent = `Analizando la imagen con el codificador (${S.device === 'webgpu' ? 'WebGPU' : 'WASM'})… sólo se hace una vez por imagen.`;
  try {
    const buf = new Uint8ClampedArray(m.rgba).buffer;
    const r = await call({ type: 'encode', key: m.key, width: m.width, height: m.height, buffer: buf }, [buf]);
    if (S.media !== m) return;
    m.encoded = true; m.encodeMs = r.encodeMs;
  } finally { S.busy = false; updateButtons(); }
}

async function analyse() {
  if (!S.ready || !S.media || S.busy) return;
  try {
    await ensureEncoded();
    if (S.points.length || S.box) await decode();
    else { els.resultText.textContent = `Imagen analizada en ${Math.round(S.media.encodeMs)} ms. Haz clic sobre un objeto.`; els.results.dataset.status = 'encoded'; }
  } catch (e) { showError(e); }
}

async function decode() {
  const m = S.media;
  if (!m?.encoded || S.busy) return;
  if (!S.points.length && !S.box) { S.mask = null; render(); return; }
  S.busy = true; updateButtons(); els.results.dataset.status = 'decoding';
  try {
    const r = await call({ type: 'decode', key: m.key, points: S.points, box: S.box });
    if (S.media !== m) return;
    S.mask = new Uint8Array(r.mask.buffer || r.mask);
    S.maskInfo = describeMask(S.mask, r.width, r.height);
    S.last = r;
    showResult();
  } catch (e) { showError(e); }
  finally { S.busy = false; updateButtons(); render(); }
}

function showResult() {
  const r = S.last, i = S.maskInfo, m = S.media;
  const dev = r.device === 'webgpu' ? 'WebGPU' : 'WASM';
  els.results.dataset.status = 'ok';
  els.results.dataset.area = i.area; els.results.dataset.areaPct = i.areaPct; els.results.dataset.bbox = JSON.stringify(i.bbox);
  els.results.dataset.encodeMs = Math.round(m.encodeMs); els.results.dataset.decodeMs = Math.round(r.decodeMs); els.results.dataset.device = r.device;
  els.results.innerHTML = '';
  const p = document.createElement('p'); p.id = 'result-text'; p.className = 'status';
  p.textContent = i.area ? 'Máscara calculada (inferencia real en tu navegador).' : 'El modelo devolvió una máscara vacía: prueba otro punto o una caja.';
  const ul = document.createElement('ul'); ul.className = 'kv';
  const kv = [
    ['Área', `${i.area.toLocaleString('es')} px (${String(i.areaPct).replace('.', ',')} %)`],
    ['Caja de la máscara', i.bbox ? i.bbox.join(', ') : '—'],
    ['Calidad estimada por el modelo (IoU pred.)', r.scores.map((s, k) => (k === r.chosen ? `[${s.toFixed(2)}]` : s.toFixed(2))).join(' · ')],
    ['Codificador', `${Math.round(m.encodeMs)} ms`], ['Decodificador', `${Math.round(r.decodeMs)} ms`], ['Dispositivo', dev],
    ['Prompts', `${S.points.filter((q) => q.label).length} incluir · ${S.points.filter((q) => !q.label).length} excluir · ${S.box ? '1 caja' : 'sin caja'}`],
  ];
  for (const [k, v] of kv) { const li = document.createElement('li'); li.textContent = `${k}: ${v}`; ul.append(li); }
  els.results.append(p, ul);
  els.resultText = p;
}

function showError(e) {
  els.results.dataset.status = 'error';
  els.resultText.textContent = 'Error: ' + (e?.message || e);
  els.resultText.classList.add('err');
  setTimeout(() => els.resultText.classList.remove('err'), 4000);
}

// ---------------- drawing
function render() {
  const m = S.media; if (!m) return;
  let rgba = m.rgba;
  if (S.mask && els.showOverlay.checked && S.mask.length === m.width * m.height) rgba = overlayMask(rgba, S.mask, m.width, m.height);
  setStageSize(m.width, m.height);
  ctx.putImageData(new ImageData(new Uint8ClampedArray(rgba), m.width, m.height), 0, 0);
  drawPrompts(S.points, S.box);
  if (S.dragStart && S.dragNow) { const b = normBox(S.dragStart, S.dragNow, m.width, m.height, 1); if (b) drawBox(b, true); }
}
function drawBox(b, dashed = false) {
  const lw = Math.max(2, Math.round(els.stage.width / 400));
  ctx.save(); ctx.lineWidth = lw; ctx.strokeStyle = '#ffc800'; if (dashed) ctx.setLineDash([lw * 3, lw * 2]);
  ctx.strokeRect(b[0], b[1], b[2] - b[0], b[3] - b[1]); ctx.restore();
}
function drawPrompts(points, box) {
  if (box) drawBox(box);
  const r = Math.max(5, Math.round(els.stage.width / 140));
  for (const p of points) {
    ctx.beginPath(); ctx.arc(p.x, p.y, r, 0, Math.PI * 2); ctx.fillStyle = p.label ? '#16a34a' : '#dc2626'; ctx.fill();
    ctx.lineWidth = 2; ctx.strokeStyle = '#fff'; ctx.stroke();
    ctx.fillStyle = '#fff'; ctx.font = `bold ${r * 1.6}px system-ui`; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.fillText(p.label ? '+' : '−', p.x, p.y + 1);
  }
}

// ---------------- interaction
const mode = () => document.querySelector('input[name=mode]:checked').value;
const toImg = (ev) => displayToImage(ev.clientX, ev.clientY, els.stage.getBoundingClientRect(), S.media.width, S.media.height);
els.stage.addEventListener('pointerdown', (ev) => { if (!S.media || S.track?.running) return; S.dragStart = toImg(ev); S.dragNow = null; els.stage.setPointerCapture(ev.pointerId); });
els.stage.addEventListener('pointermove', (ev) => { if (!S.dragStart || mode() !== 'box') return; S.dragNow = toImg(ev); render(); });
els.stage.addEventListener('pointerup', (ev) => {
  if (!S.dragStart) return;
  const a = S.dragStart, b = toImg(ev); S.dragStart = null; S.dragNow = null;
  if (mode() === 'box') {
    const bx = normBox(a, b, S.media.width, S.media.height);
    if (!bx) { render(); return; }
    S.box = bx;
  } else {
    if (S.points.length >= LIMITS.maxPoints) { els.resultText.textContent = `Máximo ${LIMITS.maxPoints} puntos.`; return; }
    let label = mode() === 'pos' ? 1 : 0; if (ev.shiftKey) label = 1 - label;
    S.points.push({ x: Math.round(b[0]), y: Math.round(b[1]), label });
  }
  S.track = null; resetScrub();
  render(); updateButtons();
  if (S.ready) analyse();
});
els.undo.addEventListener('click', () => { if (S.points.length) S.points.pop(); else S.box = null; S.track = null; resetScrub(); render(); updateButtons(); if (S.ready) S.points.length || S.box ? analyse() : (S.mask = null, render()); });
els.clear.addEventListener('click', () => { S.points = []; S.box = null; S.mask = null; S.track = null; resetScrub(); render(); updateButtons(); els.results.dataset.status = S.media?.encoded ? 'encoded' : 'idle'; });
els.showOverlay.addEventListener('change', () => (S.track?.frames.length ? showTrackFrame(+els.scrub.value) : render()));

function updateButtons() {
  const has = S.points.length || S.box;
  els.undo.disabled = !has || S.busy; els.clear.disabled = !has || S.busy;
  const hasMask = !!(S.mask && S.maskInfo?.area);
  els.dlOverlay.disabled = els.dlMask.disabled = els.dlCutout.disabled = !hasMask;
  els.track.disabled = !(S.ready && S.media?.kind === 'video' && has) || S.busy;
  els.stop.disabled = !S.track?.running;
  els.dlWebm.disabled = !(S.track && !S.track.running && S.track.frames.length);
}

// ---------------- downloads
function download(blob, name) { const a = document.createElement('a'); a.href = URL.createObjectURL(blob); a.download = name; a.click(); setTimeout(() => URL.revokeObjectURL(a.href), 5000); }
function rgbaToBlob(rgba, w, h) { const c = new OffscreenCanvas(w, h); c.getContext('2d').putImageData(new ImageData(new Uint8ClampedArray(rgba), w, h), 0, 0); return c.convertToBlob({ type: 'image/png' }); }
const curFrame = () => (S.track?.frames.length ? S.track.frames[+els.scrub.value] : { rgba: S.media.rgba, mask: S.mask });
els.dlOverlay.addEventListener('click', async () => { const f = curFrame(); download(await rgbaToBlob(overlayMask(f.rgba, f.mask, S.media.width, S.media.height), S.media.width, S.media.height), 'rotoscopia-superpuesto.png'); });
els.dlMask.addEventListener('click', async () => { const f = curFrame(); const g = new Uint8ClampedArray(f.mask.length * 4); for (let i = 0; i < f.mask.length; i++) { const v = f.mask[i] ? 255 : 0; g[i * 4] = g[i * 4 + 1] = g[i * 4 + 2] = v; g[i * 4 + 3] = 255; } download(await rgbaToBlob(g, S.media.width, S.media.height), 'rotoscopia-mascara.png'); });
els.dlCutout.addEventListener('click', async () => { const f = curFrame(); download(await rgbaToBlob(cutout(f.rgba, f.mask), S.media.width, S.media.height), 'rotoscopia-recorte.png'); });

// ---------------- video tracking
async function supplyFrame(i) {
  const m = S.media;
  try {
    await seekDraw(m.times[i]);
    const rgba = ctx.getImageData(0, 0, m.width, m.height).data;
    S.track.pending.set(i, rgba);
    const buf = new Uint8ClampedArray(rgba).buffer;
    S.worker.postMessage({ type: 'frame', index: i, width: m.width, height: m.height, buffer: buf }, [buf]);
  } catch (e) { S.worker.postMessage({ type: 'frame', index: i, error: e.message }); }
}
function onTrackFrame(r) {
  const t = S.track; if (!t) return;
  const rgba = t.pending.get(r.index); t.pending.delete(r.index);
  const mask = new Uint8Array(r.mask.buffer || r.mask);
  t.frames.push({ rgba, mask, info: { area: r.area, lost: r.lost, ms: r.encodeMs + r.decodeMs, prompt: r.prompt } });
  t.totalMs += r.encodeMs + r.decodeMs;
  const n = S.media.times.length;
  els.trackProg.value = t.frames.length / n;
  els.trackText.textContent = `${t.frames.length}/${n} · ${Math.round(r.encodeMs + r.decodeMs)} ms/fotograma${r.lost ? ' · objeto perdido en este fotograma' : ''}`;
  els.scrub.max = String(t.frames.length - 1); els.scrub.value = els.scrub.max; els.scrub.disabled = false;
  showTrackFrame(t.frames.length - 1);
}
function showTrackFrame(i) {
  const f = S.track?.frames[i]; if (!f) return;
  const m = S.media;
  const img = els.showOverlay.checked ? overlayMask(f.rgba, f.mask, m.width, m.height, f.info.lost ? [255, 80, 80] : [0, 200, 255]) : f.rgba;
  setStageSize(m.width, m.height); ctx.putImageData(new ImageData(new Uint8ClampedArray(img), m.width, m.height), 0, 0);
  els.scrubOut.textContent = `${i + 1} (t = ${m.times[i].toFixed(2)} s, área ${f.info.area} px${f.info.lost ? ', perdido' : ''})`;
}
function resetScrub() { els.scrub.disabled = true; els.scrub.max = '0'; els.scrub.value = '0'; els.scrubOut.textContent = '–'; }
function stopTrackingUI() { if (S.track?.running) S.worker?.postMessage({ type: 'stop' }); S.track = null; resetScrub(); els.trackWrap.hidden = true; }
els.scrub.addEventListener('input', () => showTrackFrame(+els.scrub.value));

els.track.addEventListener('click', async () => {
  const m = S.media; if (!m || m.kind !== 'video' || S.busy) return;
  updatePlan();
  S.track = { running: true, frames: [], pending: new Map(), totalMs: 0 };
  S.busy = true; m.encoded = false; // the worker drops still-image embeddings when tracking
  els.trackWrap.hidden = false; els.trackProg.value = 0; els.trackText.textContent = 'Iniciando…';
  els.results.dataset.status = 'tracking'; updateButtons();
  try {
    const r = await call({ type: 'track', nFrames: m.times.length, points: S.points, box: S.box });
    const t = S.track;
    const lost = t.frames.filter((f) => f.info.lost).length;
    els.trackText.textContent = `${r.stopped ? 'Detenido' : 'Listo'}: ${t.frames.length} fotogramas · media ${Math.round(t.totalMs / Math.max(1, t.frames.length))} ms/fotograma · perdidos ${lost}`;
    els.results.dataset.status = 'track-ok'; els.results.dataset.frames = t.frames.length; els.results.dataset.lost = lost;
    els.results.dataset.meanFrameMs = Math.round(t.totalMs / Math.max(1, t.frames.length)); els.results.dataset.areas = JSON.stringify(t.frames.map((f) => f.info.area));
  } catch (e) { showError(e); }
  finally { if (S.track) S.track.running = false; S.busy = false; updateButtons(); }
});
els.stop.addEventListener('click', () => S.worker?.postMessage({ type: 'stop' }));

els.dlWebm.addEventListener('click', async () => {
  const t = S.track, m = S.media; if (!t?.frames.length) return;
  const fps = +els.fps.value, c = document.createElement('canvas'); c.width = m.width; c.height = m.height;
  const cx = c.getContext('2d'); const stream = c.captureStream(fps);
  const type = ['video/webm;codecs=vp9', 'video/webm;codecs=vp8', 'video/webm'].find((x) => MediaRecorder.isTypeSupported(x));
  if (!type) { showError(new Error('Este navegador no puede grabar WebM.')); return; }
  const rec = new MediaRecorder(stream, { mimeType: type }); const chunks = [];
  rec.ondataavailable = (e) => e.data.size && chunks.push(e.data);
  const done = new Promise((r) => (rec.onstop = r));
  rec.start();
  for (const f of t.frames) { cx.putImageData(new ImageData(new Uint8ClampedArray(overlayMask(f.rgba, f.mask, m.width, m.height)), m.width, m.height), 0, 0); await new Promise((r) => setTimeout(r, 1000 / fps)); }
  rec.stop(); await done;
  download(new Blob(chunks, { type: 'video/webm' }), 'rotoscopia-seguimiento.webm');
});

// expose pins for debugging / automated smoke
window.__ROTOSCOPIA__ = { MODEL_ID, MODEL_REVISION, state: S };