diff --git a/apps/api/src/autopilot/curate.ts b/apps/api/src/autopilot/curate.ts index 9141069..560f05f 100644 --- a/apps/api/src/autopilot/curate.ts +++ b/apps/api/src/autopilot/curate.ts @@ -1,7 +1,8 @@ /** * "Only the best" (docs/photo-editing-rules.md §5): for clients that want a short gallery of the match, not every * usable frame. The match is cut into short windows, and each window may add its share of the gallery size, with - * the share it did not use carried forward (half-time adds nothing, a busy spell may take a little more). Within a + * the share it did not use carried forward (half-time adds nothing, a busy spell may take a little more). + * The asked size is a target: a rich match may pass it by about 15% (never more than 25 frames); a thin one stays under. Within a * window the frames with the most action win: the ball in play, a duel or a celebration, a subject big in the * frame and sharp; one frame per burst, and not two from the same few seconds. Anything well below the match's * typical frame is left out even when the window has room. @@ -65,6 +66,15 @@ const median = (xs: number[]) => { return s[Math.floor(s.length / 2)]!; }; +/** + * How many frames a gallery may reach. The asked size is the target: about 15% more when the match is rich, + * at least 2 and never more than 25 past it, so a short gallery stays close and a long one can breathe. + */ +export function galleryCeiling(count: number): number { + const extra = Math.min(25, Math.max(2, Math.round(count * 0.15))); + return count + extra; +} + /** * Decides the windows that have closed and are not decided yet. `decided` holds the candidates already decided * (chosen or not), so earlier choices count against the gallery size. @@ -75,11 +85,14 @@ export function curate(candidates: Candidate[], decided: { id: string; chosen: b const settleMs = opts.settleMs ?? 60_000; const done = new Map(decided.map((d) => [d.id, d.chosen])); let chosenSoFar = [...done.values()].filter(Boolean).length; - const cap = Math.round(opts.count * 1.1); + const cap = galleryCeiling(opts.count); const perWindow = (opts.count * windowMs) / opts.durationMs; - // The match's typical frame, for the floor: half of the median of the frames with a subject. + const windows = Math.max(1, opts.durationMs / windowMs); + // The match's typical frame. The floor drops weak frames; only a frame at or above the typical one may use the slack. const scored = candidates.map((c) => ({ c, s: actionScore(c) })); - const floor = scored.filter((x) => x.s > 0).length >= 10 ? 0.5 * median(scored.filter((x) => x.s > 0).map((x) => x.s)) : 0; + const positives = scored.filter((x) => x.s > 0).map((x) => x.s); + const typical = positives.length >= 10 ? median(positives) : Number.POSITIVE_INFINITY; + const floor = positives.length >= 10 ? 0.5 * typical : 0; const byWindow = new Map(); for (const x of scored) { @@ -98,15 +111,29 @@ export function curate(candidates: Candidate[], decided: { id: string; chosen: b const end = opts.start + (w + 1) * windowMs; if (end + settleMs > opts.now) break; // The share up to the end of this window, minus what was already chosen; at most three windows' worth at once. - const target = Math.min(cap, Math.round(perWindow * (w + 1))); - const room = Math.max(0, Math.min(target - chosenSoFar, Math.ceil(3 * perWindow), cap - chosenSoFar)); + const target = Math.min(opts.count, Math.round(perWindow * (w + 1))); + const room = Math.max(0, Math.min(target - chosenSoFar, Math.ceil(3 * perWindow), opts.count - chosenSoFar)); const ranked = list.filter((x) => x.s > 0 && x.s >= floor).sort((a, b) => b.s - a.s); const chosen: Candidate[] = []; + const fits = (c: Candidate) => !chosen.some((o) => o.burst === c.burst || Math.abs(o.at - c.at) < minGapMs); for (const { c } of ranked) { if (chosen.length >= room) break; - if (chosen.some((o) => o.burst === c.burst || Math.abs(o.at - c.at) < minGapMs)) continue; + if (!fits(c)) continue; chosen.push(c); } + // Slack follows the match, about 15% by the end. A quiet spell leaves it; a rich window may spend up to two frames of it. + const slackByNow = Math.round(((cap - opts.count) * (w + 1)) / windows); + const alreadyOver = Math.max(0, chosenSoFar - opts.count); + let extraLeft = Math.min(2, Math.max(0, slackByNow - alreadyOver), cap - chosenSoFar - chosen.length); + if (extraLeft > 0 && typical !== Number.POSITIVE_INFINITY) { + for (const { c, s } of ranked) { + if (extraLeft <= 0) break; + if (chosen.includes(c)) continue; + if (s < typical || !fits(c)) continue; + chosen.push(c); + extraLeft -= 1; + } + } chosenSoFar += chosen.length; out.push({ window: w, ids: list.map((x) => x.c.id), chosen: chosen.sort((a, b) => a.at - b.at).map((c) => c.id) }); } diff --git a/apps/api/test/autopilot.test.ts b/apps/api/test/autopilot.test.ts index f0891e8..69c71ce 100644 --- a/apps/api/test/autopilot.test.ts +++ b/apps/api/test/autopilot.test.ts @@ -5,7 +5,7 @@ import sharp from "sharp"; import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest"; import { AutopilotService } from "../src/autopilot/service.js"; import { bestOf, groupBursts, reportagePickCount } from "../src/autopilot/bursts.js"; -import { type Candidate, actionScore, curate } from "../src/autopilot/curate.js"; +import { type Candidate, actionScore, curate, galleryCeiling } from "../src/autopilot/curate.js"; import { correctionOf, fold, learntStyle, recordCorrection, styleOf } from "../src/autopilot/learning.js"; import type { Detections } from "../src/composition/compose.js"; import type { SubjectDetector } from "../src/composition/detect.js"; @@ -103,7 +103,10 @@ describe("only the best", () => { const opts = { count: 30, start, durationMs: 105 * min, now: start + 200 * min }; const all = curate(frames, [], opts).flatMap((d) => d.chosen); expect(all.length).toBeGreaterThanOrEqual(28); - expect(all.length).toBeLessThanOrEqual(33); + expect(all.length).toBeLessThanOrEqual(galleryCeiling(30)); + expect(galleryCeiling(30)).toBe(35); + expect(galleryCeiling(100)).toBe(115); + expect(galleryCeiling(300)).toBe(325); expect(all).toContain(great.id); expect(all).not.toContain(twin.id); expect(all).not.toContain(rejected.id); diff --git a/apps/app/src/i18n.ts b/apps/app/src/i18n.ts index 3058c52..c028332 100644 --- a/apps/app/src/i18n.ts +++ b/apps/app/src/i18n.ts @@ -562,7 +562,7 @@ export const t = { selectionBursts: "As escolhidas de cada sequência", selectionBest: "Só as melhores do jogo", bestCount: "Fotos na galeria (aprox.)", - bestHelp: "Uma galeria curta de momentos de jogo: bola em jogo, duelos e festejos, jogadores grandes e nítidos no enquadramento, espalhados pelo jogo (a cada 5 minutos entram as melhores desse período). Numa sequência fica a de frente e com os olhos abertos, quando dá para distinguir. Aquecimento e intervalo ficam de fora; as fotos com estrelas têm prioridade e as rejeitadas nunca vão.", + bestHelp: "Uma galeria curta de momentos de jogo: bola em jogo, duelos e festejos, jogadores grandes e nítidos no enquadramento, espalhados pelo jogo (a cada 5 minutos entram as melhores desse período). O número é a meta: num jogo rico pode ir um pouco acima (cerca de 15%, nunca mais de 25 fotos), num jogo fraco fica aquém. Numa sequência fica a de frente e com os olhos abertos, quando dá para distinguir. Aquecimento e intervalo ficam de fora; as fotos com estrelas têm prioridade e as rejeitadas nunca vão.", restTarget: "As outras escolhidas para", restHelp: "Cada foto vai só para um cliente. A galeria acima leva a segunda melhor de cada sequência escolhida; todas as outras escolhidas seguem para aqui quando fecha cada período de 5 minutos.", diff --git a/docs/photo-editing-rules.md b/docs/photo-editing-rules.md index 8ada579..568b0d0 100644 --- a/docs/photo-editing-rules.md +++ b/docs/photo-editing-rules.md @@ -168,7 +168,9 @@ With autopilot "only the best", windows still shape the first pass (`apps/api/sr - the match (from kick-off, its typical length by sport: football 115 min with the break, handball 80) is cut into 5-minute windows; each window may add its share of the gallery, and a share it did not use (the break, - a quiet spell) carries forward, at most three windows' worth at once; at most 10% over the size in all; + a quiet spell) carries forward, at most three windows' worth at once. The size is a target (`galleryCeiling`): + a rich match may add about 15% (at least 2 frames, never more than 25 past the number) from frames at or above + the match's typical one; a thin match stays under; - one candidate per burst (its best frame), ranked by game moment: the frame score, more with the ball in play (×1.3), a duel or a celebration (several people in the subject), less when the subject is small (far away); the photographer's stars count (+20% each), a rejected frame never goes;