File size: 9,654 Bytes
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
// Pure helpers (no DOM, no model) so they can be unit-tested in Node.
// Coordinates are always in ORIGINAL image pixels unless stated otherwise.

export const LIMITS = {
  maxImageBytes: 15 * 1024 * 1024,
  maxImageMegapixels: 24,
  maxWorkSide: 1600, // images larger than this are downscaled before inference (SAM resizes to 1008 anyway)
  maxVideoBytes: 60 * 1024 * 1024,
  maxVideoSeconds: 20,
  maxVideoFrames: 60,
  maxPoints: 12,
  videoWorkSide: 960,
};

export const IMAGE_TYPES = ['image/jpeg', 'image/png', 'image/webp'];
export const VIDEO_TYPES = ['video/mp4', 'video/webm', 'video/quicktime', 'video/ogg'];

/** Validate an uploaded file. Returns {ok, error?} (error in Spanish). */
export function validateFile({ type, size }, kind) {
  const types = kind === 'video' ? VIDEO_TYPES : IMAGE_TYPES;
  const max = kind === 'video' ? LIMITS.maxVideoBytes : LIMITS.maxImageBytes;
  if (!types.includes(type)) return { ok: false, error: `Formato no admitido (${type || 'desconocido'}).` };
  if (!(size > 0)) return { ok: false, error: 'El archivo está vacío.' };
  if (size > max) return { ok: false, error: `El archivo supera ${Math.round(max / 1048576)} MB.` };
  return { ok: true };
}

/** Fit (w,h) inside maxSide keeping aspect ratio; never upscales. */
export function fitSize(w, h, maxSide) {
  if (!(w > 0 && h > 0)) throw new Error('Dimensiones inválidas');
  const s = Math.min(1, maxSide / Math.max(w, h));
  return { width: Math.max(1, Math.round(w * s)), height: Math.max(1, Math.round(h * s)), scale: s };
}

export function checkMegapixels(w, h) {
  return w * h <= LIMITS.maxImageMegapixels * 1e6;
}

/** Map a pointer position on a displayed element to image pixel coords (clamped). */
export function displayToImage(px, py, rect, imgW, imgH) {
  const x = ((px - rect.left) / rect.width) * imgW;
  const y = ((py - rect.top) / rect.height) * imgH;
  return [clamp(x, 0, imgW - 1), clamp(y, 0, imgH - 1)];
}

export function clamp(v, lo, hi) { return Math.min(hi, Math.max(lo, v)); }

/** Normalise a box drawn in any direction to [x0,y0,x1,y1] clamped to the image. Returns null if too small. */
export function normBox(a, b, imgW, imgH, minSide = 4) {
  const x0 = clamp(Math.min(a[0], b[0]), 0, imgW - 1), x1 = clamp(Math.max(a[0], b[0]), 0, imgW - 1);
  const y0 = clamp(Math.min(a[1], b[1]), 0, imgH - 1), y1 = clamp(Math.max(a[1], b[1]), 0, imgH - 1);
  if (x1 - x0 < minSide || y1 - y0 < minSide) return null;
  return [x0, y0, x1, y1];
}

/**
 * Build SAM processor prompt arguments for ONE object.
 * points: [{x,y,label:1|0}], box: [x0,y0,x1,y1] | null
 * Shapes follow transformers.js Sam2/Sam3 processor: points [[[[x,y],...]]], labels [[[l,...]]], boxes [[[x0,y0,x1,y1]]].
 */
export function buildPrompt(points, box) {
  const pts = (points || []).slice(0, LIMITS.maxPoints);
  if (!pts.length && !box) throw new Error('Añade al menos un punto o una caja.');
  const out = {};
  if (pts.length) {
    out.input_points = [[pts.map((p) => [round2(p.x), round2(p.y)])]];
    out.input_labels = [[pts.map((p) => (p.label === 0 ? 0 : 1))]];
  }
  if (box) out.input_boxes = [[box.map(round2)]];
  return out;
}
const round2 = (v) => Math.round(v * 100) / 100;

/** Index of the best candidate mask given iou_scores data (Float32Array/array). */
export function bestMaskIndex(scores) {
  let best = 0;
  for (let i = 1; i < scores.length; i++) if (scores[i] > scores[best]) best = i;
  return best;
}

/** Extract mask k from a bool/uint8 tensor data laid out [..., K, H, W]. Returns Uint8Array(H*W) of 0/1. */
export function sliceMask(data, k, H, W) {
  const n = H * W, out = new Uint8Array(n), off = k * n;
  for (let i = 0; i < n; i++) out[i] = data[off + i] ? 1 : 0;
  return out;
}

export function maskArea(mask) {
  let a = 0;
  for (let i = 0; i < mask.length; i++) a += mask[i];
  return a;
}

/** Tight bbox [x0,y0,x1,y1] (inclusive pixel coords) of a mask, or null if empty. */
export function maskBBox(mask, W, H) {
  let x0 = W, y0 = H, x1 = -1, y1 = -1;
  for (let y = 0; y < H; y++) {
    const row = y * W;
    for (let x = 0; x < W; x++) if (mask[row + x]) {
      if (x < x0) x0 = x; if (x > x1) x1 = x; if (y < y0) y0 = y; if (y > y1) y1 = y;
    }
  }
  return x1 < 0 ? null : [x0, y0, x1, y1];
}

export function maskCentroid(mask, W, H) {
  let sx = 0, sy = 0, n = 0;
  for (let y = 0; y < H; y++) for (let x = 0; x < W; x++) if (mask[y * W + x]) { sx += x; sy += y; n++; }
  return n ? [sx / n, sy / n] : null;
}

/** A point guaranteed to lie on the mask: centroid if inside, else nearest mask pixel (sampled search). */
export function interiorPoint(mask, W, H) {
  const c = maskCentroid(mask, W, H);
  if (!c) return null;
  const cx = Math.round(c[0]), cy = Math.round(c[1]);
  if (mask[cy * W + cx]) return [cx, cy];
  // Prefer the pixel with the largest distance to the mask border among a coarse grid: approximated by
  // choosing the mask pixel nearest to the centroid (cheap and deterministic).
  let best = null, bd = Infinity;
  for (let y = 0; y < H; y++) for (let x = 0; x < W; x++) if (mask[y * W + x]) {
    const d = (x - cx) ** 2 + (y - cy) ** 2;
    if (d < bd) { bd = d; best = [x, y]; }
  }
  return best;
}

export function boxIoU(a, b) {
  const ix = Math.max(0, Math.min(a[2], b[2]) - Math.max(a[0], b[0]));
  const iy = Math.max(0, Math.min(a[3], b[3]) - Math.max(a[1], b[1]));
  const inter = ix * iy;
  const ua = (a[2] - a[0]) * (a[3] - a[1]) + (b[2] - b[0]) * (b[3] - b[1]) - inter;
  return ua > 0 ? inter / ua : 0;
}

export function maskIoU(a, b) {
  if (a.length !== b.length) throw new Error('Tamaños de máscara distintos');
  let inter = 0, uni = 0;
  for (let i = 0; i < a.length; i++) { const p = a[i] ? 1 : 0, q = b[i] ? 1 : 0; inter += p & q; uni += p | q; }
  return uni ? inter / uni : 0;
}

/** Fraction of mask pixels that fall inside box (inclusive). */
export function fractionInside(mask, W, H, box) {
  let inside = 0, n = 0;
  for (let y = 0; y < H; y++) for (let x = 0; x < W; x++) if (mask[y * W + x]) {
    n++; if (x >= box[0] && x <= box[2] && y >= box[1] && y <= box[3]) inside++;
  }
  return n ? inside / n : 0;
}

export function expandBox(box, frac, W, H) {
  const w = box[2] - box[0], h = box[3] - box[1];
  return [clamp(box[0] - w * frac, 0, W - 1), clamp(box[1] - h * frac, 0, H - 1), clamp(box[2] + w * frac, 0, W - 1), clamp(box[3] + h * frac, 0, H - 1)];
}

/** Nearest-neighbour resize of a binary mask. */
export function resizeMask(mask, W, H, W2, H2) {
  const out = new Uint8Array(W2 * H2);
  for (let y = 0; y < H2; y++) {
    const sy = Math.min(H - 1, Math.floor(((y + 0.5) * H) / H2));
    for (let x = 0; x < W2; x++) {
      const sx = Math.min(W - 1, Math.floor(((x + 0.5) * W) / W2));
      out[y * W2 + x] = mask[sy * W + sx];
    }
  }
  return out;
}

/**
 * Prompt for the next video frame from the previous frame's mask ("re-prompting" propagation, a heuristic of
 * this app — NOT SAM's memory-based video tracker). Box = previous bbox expanded by `margin`; point = interior point.
 * Masks/coords in frame pixels (all frames share the same size).
 */
export function nextFramePrompt(prevMask, W, H, margin = 0.1) {
  const bb = maskBBox(prevMask, W, H);
  if (!bb) return null;
  const p = interiorPoint(prevMask, W, H);
  return { points: [{ x: p[0], y: p[1], label: 1 }], box: expandBox(bb, margin, W, H) };
}

/** Decide whether the object was lost on a frame. */
export function isLost({ objectScoreLogit, area, prevArea }) {
  if (objectScoreLogit !== undefined && objectScoreLogit < 0) return true; // model says "object absent"
  if (!area) return true;
  if (prevArea && (area > prevArea * 4 || area < prevArea / 4)) return true; // implausible jump
  return false;
}

/** Timestamps (s) to sample from a video of `duration` at `fps`, capped to maxFrames and maxSeconds. */
export function sampleTimes(duration, fps, maxFrames = LIMITS.maxVideoFrames, maxSeconds = LIMITS.maxVideoSeconds) {
  if (!(duration > 0) || !(fps > 0)) return [];
  const end = Math.min(duration, maxSeconds);
  const out = [];
  for (let i = 0; ; i++) {
    const t = i / fps;
    if (t >= end - 1e-6 || out.length >= maxFrames) break;
    out.push(Math.round(t * 1000) / 1000);
  }
  return out;
}

/** Composite a coloured, semi-transparent mask + 1px contour over RGBA pixels (returns new Uint8ClampedArray). */
export function overlayMask(rgba, mask, W, H, color = [0, 200, 255], alpha = 0.45) {
  const out = new Uint8ClampedArray(rgba);
  for (let y = 0; y < H; y++) for (let x = 0; x < W; x++) {
    const i = y * W + x;
    if (!mask[i]) continue;
    const edge = x === 0 || y === 0 || x === W - 1 || y === H - 1 || !mask[i - 1] || !mask[i + 1] || !mask[i - W] || !mask[i + W];
    const a = edge ? 1 : alpha, j = i * 4;
    for (let c = 0; c < 3; c++) out[j + c] = Math.round(out[j + c] * (1 - a) + color[c] * a);
  }
  return out;
}

/** RGBA cut-out: original pixels where mask=1, transparent elsewhere. */
export function cutout(rgba, mask) {
  const out = new Uint8ClampedArray(rgba.length);
  for (let i = 0; i < mask.length; i++) if (mask[i]) {
    const j = i * 4; out[j] = rgba[j]; out[j + 1] = rgba[j + 1]; out[j + 2] = rgba[j + 2]; out[j + 3] = rgba[j + 3];
  }
  return out;
}

/** Human summary of a mask in Spanish. */
export function describeMask(mask, W, H) {
  const area = maskArea(mask);
  const bb = maskBBox(mask, W, H);
  return { area, areaPct: Math.round((1000 * area) / (W * H)) / 10, bbox: bb };
}

export function formatBytes(b) {
  if (b >= 1e6) return (b / 1e6).toFixed(1).replace('.', ',') + ' MB';
  if (b >= 1e3) return Math.round(b / 1e3) + ' kB';
  return b + ' B';
}