A weak likeness was compared with every person in the organisation when the match sheet did not explain it, so a player of one club could be named as a player of another. A known game now stays on its roster, and a past season's squad is stored with the calendar.
82 lines
3.5 KiB
TypeScript
82 lines
3.5 KiB
TypeScript
/** Small vector helpers for unit-length face embeddings. */
|
|
|
|
export function cosine(a: readonly number[], b: readonly number[]): number {
|
|
if (a.length !== b.length || a.length === 0) return -1;
|
|
let dot = 0;
|
|
let na = 0;
|
|
let nb = 0;
|
|
for (let i = 0; i < a.length; i++) {
|
|
dot += a[i]! * b[i]!;
|
|
na += a[i]! * a[i]!;
|
|
nb += b[i]! * b[i]!;
|
|
}
|
|
return na && nb ? dot / Math.sqrt(na * nb) : -1;
|
|
}
|
|
|
|
export function normalize(v: readonly number[]): number[] {
|
|
const n = Math.sqrt(v.reduce((s, x) => s + x * x, 0));
|
|
return n ? v.map((x) => x / n) : [...v];
|
|
}
|
|
|
|
/** Running mean of `count` vectors plus one more, re-normalised. */
|
|
export function addToMean(mean: readonly number[], count: number, v: readonly number[]): number[] {
|
|
if (!mean.length || count <= 0) return normalize(v);
|
|
return normalize(mean.map((m, i) => m * count + v[i]!));
|
|
}
|
|
|
|
/** Weighted mean of several means. */
|
|
export function mergeMeans(parts: { mean: readonly number[]; count: number }[]): number[] {
|
|
const usable = parts.filter((p) => p.mean.length && p.count > 0);
|
|
if (!usable.length) return [];
|
|
const dim = usable[0]!.mean.length;
|
|
const sum = new Array<number>(dim).fill(0);
|
|
for (const p of usable) for (let i = 0; i < dim; i++) sum[i]! += p.mean[i]! * p.count;
|
|
return normalize(sum);
|
|
}
|
|
|
|
/**
|
|
* Thresholds per recognition model. `match`: same person (LightCraft's measured starting points). `margin`:
|
|
* how far the best person must be ahead of the second before we suggest a name.
|
|
*/
|
|
/**
|
|
* `match`: a group or reference is suggested as a known person from this similarity (with `margin` over the
|
|
* runner-up). `group`: a face joins a group of the same event from this similarity. Grouping is stricter on
|
|
* purpose: a mixed group would name the wrong photos, while splitting one person in two costs a "Juntar" click.
|
|
* Tuned on a real football match (58 photos, 472 faces): at 0.40 blurred crowd faces merged into groups of 20-35.
|
|
*/
|
|
/** How far under `match` a face can sit and still be a possible, not a sure, name. */
|
|
export const POSSIBLE_BELOW = 0.06;
|
|
|
|
export const THRESHOLDS: Record<string, { match: number; group: number; margin: number }> = {
|
|
"auraface-v1": { match: 0.4, group: 0.45, margin: 0.05 },
|
|
"sface-2021dec": { match: 0.55, group: 0.6, margin: 0.05 },
|
|
};
|
|
|
|
/** No roster (a session, a name that is not a club): a barely-there likeness is not a name. */
|
|
export const OPEN_EXTRA = 0.12;
|
|
export const OPEN_MARGIN = 0.08;
|
|
export function openFloor(match: number): number {
|
|
return Math.min(0.9, match + OPEN_EXTRA);
|
|
}
|
|
|
|
/**
|
|
* Faces softer than this (variance of the Laplacian of the 112 px aligned face, grey levels) are kept, boxed
|
|
* in the loupe, but never grouped: out-of-focus faces (crowd, background) all look alike to the embedder.
|
|
*/
|
|
export const MIN_SHARPNESS = 45;
|
|
|
|
/**
|
|
* A face this much softer than the sharpest one in the same photo is out of the focus plane (the crowd behind the
|
|
* player, a bench in front), however textured: not worth identifying.
|
|
*/
|
|
export const RELATIVE_SHARPNESS = 0.25;
|
|
|
|
/**
|
|
* The faces worth identifying in one photo: the ones in focus. Faces with no measure (older analyses) stay.
|
|
* Editors only want names for who the photo is about; boxes on a blurred crowd are noise.
|
|
*/
|
|
export function inFocus<T extends { sharpness?: number | null }>(faces: T[]): T[] {
|
|
const sharpest = Math.max(0, ...faces.map((f) => f.sharpness ?? 0));
|
|
return faces.filter((f) => f.sharpness == null || (f.sharpness >= MIN_SHARPNESS && f.sharpness >= RELATIVE_SHARPNESS * sharpest));
|
|
}
|