Spaces:
Running
Running
File size: 1,574 Bytes
d18636b | 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 | // Utilidades puras (testeadas con vitest).
export function l2normalize(v) {
let s = 0; for (let i = 0; i < v.length; i++) s += v[i] * v[i];
s = Math.sqrt(s) || 1;
const out = new Float32Array(v.length); for (let i = 0; i < v.length; i++) out[i] = v[i] / s;
return out;
}
export function dot(a, b) { let s = 0; for (let i = 0; i < a.length; i++) s += a[i] * b[i]; return s; }
// Devuelve [{index, score}] ordenado de mayor a menor; excludeIndex opcional (consulta = imagen de la galería).
export function rank(query, vectors, k = 12, excludeIndex = -1) {
const r = [];
vectors.forEach((v, i) => { if (i !== excludeIndex && v) r.push({ index: i, score: dot(query, v) }); });
r.sort((a, b) => b.score - a.score);
return r.slice(0, k);
}
// Escala relativa 0..1 para las barras (las similitudes texto-imagen de SigLIP son pequeñas y no calibradas).
export function relativeBars(results) {
if (!results.length) return [];
const max = results[0].score, min = results[results.length - 1].score;
const span = max - min || 1;
return results.map((r) => ({ ...r, bar: (r.score - min) / span }));
}
// Rectángulo normalizado a partir de dos puntos (cualquier orden), recortado a [0,1].
export function normRect(x0, y0, x1, y1) {
const c = (v) => Math.min(1, Math.max(0, v));
const a = [c(Math.min(x0, x1)), c(Math.min(y0, y1)), c(Math.max(x0, x1)), c(Math.max(y0, y1))];
return a;
}
export function fitSize(w, h, maxSide) {
const s = Math.min(1, maxSide / Math.max(w, h));
return [Math.max(1, Math.round(w * s)), Math.max(1, Math.round(h * s))];
}
|