diff --git a/apps/api/src/faces/rank.test.ts b/apps/api/src/faces/rank.test.ts index bfcfcae..09df804 100644 --- a/apps/api/src/faces/rank.test.ts +++ b/apps/api/src/faces/rank.test.ts @@ -3,12 +3,15 @@ import { claimsToKeep, faceAndNumberScore, numberAndKitScore, numberOnlyScore } describe("suggestion rank", () => { it("puts face+number above number+kit, and that above number alone", () => { + const { faceNumberKitScore } = await import("./rank.js"); + const top = faceNumberKitScore(0.7, 0.95, 1); const both = faceAndNumberScore(0.7, 0.95); const kit = numberAndKitScore(0.95, 1); const only = numberOnlyScore(0.95); + expect(top).toBeGreaterThan(both); expect(both).toBeGreaterThan(kit); expect(kit).toBeGreaterThan(only); - expect(both).toBeLessThanOrEqual(0.99); + expect(top).toBeLessThanOrEqual(0.99); expect(kit).toBeLessThanOrEqual(0.82); }); diff --git a/apps/api/src/faces/rank.ts b/apps/api/src/faces/rank.ts index 3f65711..c985d00 100644 --- a/apps/api/src/faces/rank.ts +++ b/apps/api/src/faces/rank.ts @@ -1,14 +1,20 @@ import { cosine } from "./math.js"; /** - * How sure a name is, highest first. The best guess wins; a weaker one never outranks a stronger one. + * How sure a name is, highest first. A sure face is never thrown away because a number was also read. * - * 1. Face and shirt agree (same player) — strongest. - * 2. Face alone, when the shirt was not read. - * 3. Clear shirt plus kit colour matching a teammate we already trust (face+number, or a confirmation). - * 4. Clear shirt, only one player in the game wears it, kit not yet known — quieter, and only one face cluster. + * 1. Face + number + kit colour of that player's team. + * 2. Face + number. + * 3. Face alone. + * 4. Number + kit, only when the face itself is not sure. + * 5. Number alone, only when the face itself is not sure. */ +/** Face, the number, and the shirt colour of that player's team. */ +export function faceNumberKitScore(face: number, jersey: number, kitSure: number): number { + return Math.round(Math.min(0.99, face + 0.15 * jersey + 0.04 * kitSure) * 1000) / 1000; +} + /** Face and the number on the kit are the same person. */ export function faceAndNumberScore(face: number, jersey: number): number { return Math.round(Math.min(0.99, face + 0.15 * jersey) * 1000) / 1000; diff --git a/apps/api/src/faces/service.ts b/apps/api/src/faces/service.ts index b5e5465..ee40b23 100644 --- a/apps/api/src/faces/service.ts +++ b/apps/api/src/faces/service.ts @@ -24,7 +24,7 @@ import { numberContext, numbersIn, peopleOnRoster, rosterOf, type NumberContext import { samePlayer } from "../sports/identity.js"; import { eventSport, normSport } from "./sport.js"; import { addToMean, cosine, inFocus, mergeMeans, MIN_SHARPNESS, OPEN_MARGIN, THRESHOLDS, openFloor } from "./math.js"; -import { claimsToKeep, faceAndNumberScore, numberAndKitScore, numberOnlyScore } from "./rank.js"; +import { claimsToKeep, faceAndNumberScore, faceNumberKitScore, numberAndKitScore, numberOnlyScore } from "./rank.js"; import type { FaceEngine } from "./process.js"; import { FaceFollower } from "./track.js"; import { randomUUID } from "node:crypto"; @@ -606,19 +606,23 @@ export class FaceService { let suggestionScore: number | null = null; const read = await this.groupNumber(g.id); const firstNumber = first && read ? (await numberOf([first.p.id])).get(first.p.id) ?? null : null; - const faceAgrees = ok && !!read && firstNumber === read.number; - // Clear shirt, face not the wearer (or no face match, or the face's person has no number here): the number wins. - // A single clear reading is enough — waiting for two left a lone #9 on the shorts unnamed. - const preferNumber = - !!read && - allowNumber && - (!ok || faceAgrees === false || (ok && firstNumber == null)); - if (faceAgrees && first && read) { - // Rank 1: face and shirt are the same player. + if (ok && first) { + // A face we already recognise stays. The number and the kit only make that guess stronger. + // They must not replace it: that is what dropped the #9 whose face was already right. suggestedPersonId = first.p.id; - suggestionScore = faceAndNumberScore(first.s, read.score); - } else if (preferNumber && read) { - // The number decides among faces that look near, then the squad who wears it (kit colour if both teams). + suggestionScore = first.s; + if (read && firstNumber === read.number) { + const team = await this.teamByKit(g.id, eventId); + const onTeam = team ? await this.playersOfTeam([first.p], team.teamId) : null; + suggestionScore = + team && onTeam.has(first.p.id) + ? faceNumberKitScore(first.s, read.score, team.sure) + : faceAndNumberScore(first.s, read.score); + } else if (read && firstNumber && firstNumber !== read.number && read.votes >= 2) { + suggestionScore = Math.round(first.s * 0.8 * 1000) / 1000; + } + } else if (read && allowNumber) { + // No sure face. The number decides, and the kit colour keeps the other team out. const close = ranked.filter((r) => r.s >= (bounded ? match * 0.8 : floor)); const nums = await numberOf(close.map((r) => r.p.id)); let near = close.filter((r) => nums.get(r.p.id) === read.number); @@ -634,17 +638,6 @@ export class FaceService { const byNumber = ctx ? await this.suggestByNumber(g.id, ctx, read) : null; if (byNumber) ({ personId: suggestedPersonId, score: suggestionScore } = byNumber); } - // Number found nobody: keep a sure face, but quieter when the shirt disagreed. - if (!suggestedPersonId && ok && first) { - suggestedPersonId = first.p.id; - suggestionScore = - firstNumber && firstNumber !== read.number - ? Math.round(first.s * 0.8 * 1000) / 1000 - : first.s; - } - } else if (ok && first) { - suggestedPersonId = first.p.id; - suggestionScore = first.s; } if (suggestedPersonId && (rejected?.has(suggestedPersonId) || off.has(suggestedPersonId))) { suggestedPersonId = null; diff --git a/apps/api/test/faces.test.ts b/apps/api/test/faces.test.ts index 9b93578..29f7251 100644 --- a/apps/api/test/faces.test.ts +++ b/apps/api/test/faces.test.ts @@ -427,13 +427,13 @@ describe("faces", () => { expect(groups[0]!.person).toBeNull(); }); - it("prefers the clear shirt number over a face that matches someone else", async () => { + it("keeps a sure face when the shirt reads another number", async () => { const { token, day1, day2, orgId } = await setup(); await photo("2026:09:23 21:00:00", [1]); await drain(); const listed = (eventId: string) => ctx.app.inject({ method: "GET", url: `/v1/events/${eventId}/face-groups`, headers: bearer(token) }).then( - (r) => r.json() as { groups: { id: string; suggestion: { name: string } | null }[] }, + (r) => r.json() as { groups: { id: string; suggestion: { name: string; score: number } | null }[] }, ); const [g] = (await listed(day1.id)).groups; await ctx.app.inject({ @@ -444,14 +444,17 @@ describe("faces", () => { }); // Same club as day2's home so suggestByNumber can find them on that game. await ctx.db.person.create({ data: { organizationId: orgId, name: "Avançado Nove", club: "ABC", number: "9" } }); - // A week later: the face alone looks like Miguel, but the shirt reads 9 — the number wins. + // A week later the face is Miguel. The shirt reads 9: the face stays. The number does not replace him. await photo("2026:09:30 21:00:00", [1]); await drain(); const later = (await listed(day2.id)).groups[0]!; expect(later.suggestion!.name).toBe("Miguel Silva"); + const byFace = later.suggestion!; await ctx.db.face.updateMany({ where: { groupId: later.id }, data: { jerseyNumber: "9", jerseyScore: 0.93 } }); await faces.refreshSuggestions(day2.id); - expect((await listed(day2.id)).groups[0]!.suggestion).toMatchObject({ name: "Avançado Nove" }); + const after = (await listed(day2.id)).groups[0]!.suggestion!; + expect(after).toMatchObject({ name: "Miguel Silva" }); + expect(after.score).toBeLessThan(byFace.score); }); it("tells apart two players with the same number, one per team, by the colour of the kit", async () => { diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 1afa658..539eb8e 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -219,12 +219,14 @@ group. Face and number are used together: -- **Face and number agree:** the strongest guess (face score + 0.15 × the reading, at most 0.99). +- **Face + number + kit colour** of that player's team is the strongest guess. **Face + number** is next. + A face that already matches is never replaced because a number was also read — that dropped players whose face + was right and whose shorts number was visible. The number only raises the score when it is the same player's. - **Face sure, no number read:** the face's suggestion. Faces come from confirmed groups and from the official headshots (Liga, UEFA, FPA licence photos) that squads bring as reference photos, so even a first match has faces to go by. -- **Clear number, no face to compare (or the face is someone else):** the squad player who wears it, but only if the - kit colour matches a teammate already trusted (face+number or a confirmation). The other team's kit never takes the name. - Without a known kit yet, one wearer is still suggested, quieter (under a real face match). +- **No sure face, clear number:** the squad player who wears it, but only if the kit colour matches a teammate already + trusted. The other team's kit never takes the name. Club crests are not a separate model: chest sponsors are not the + badge, and the shirt colour already separates two kits. - Two face groups that do not look like the same person never both keep that name: the stronger guess wins. - **Both teams have that number:** the kit colour decides. Each face keeps the median colour of the middle of its chest; each team's colour is the median of its players already confirmed in the event, or suggested with face and