From f5f7c2fa5930851add3dbb28b738fff1af8e2bd5 Mon Sep 17 00:00:00 2001 From: spark Date: Tue, 3 Nov 2026 21:32:16 +0000 Subject: [PATCH] fix(autopilot): never crop to a soft or faceless body in front (BIT-169) AF on a blurred jumper was winning the subject even when a sharp face sat behind. Require the aimed person to be sharp and to have a visible face when faces are known; always pass faces into the crop planner. --- apps/api/src/autopilot/service.ts | 10 ++++- apps/api/src/composition/compose.ts | 38 +++++++++++++------ .../api/src/composition/focus-subject.test.ts | 20 +++++++++- docs/photo-editing-rules.md | 9 +++-- 4 files changed, 59 insertions(+), 18 deletions(-) diff --git a/apps/api/src/autopilot/service.ts b/apps/api/src/autopilot/service.ts index 7db75ac..0c53d0b 100644 --- a/apps/api/src/autopilot/service.ts +++ b/apps/api/src/autopilot/service.ts @@ -136,8 +136,14 @@ export class AutopilotService { const detections = load ? await (await load).detect(preview) : null; const focusFile = photo.originalStored ? storage.resolve(photo.storageKey) : preview; const focus = focusFromExifJson(photo.exif) ?? (existsSync(focusFile) ? await readFocusPoint(focusFile) : null); - // Faces tell a whole figure from a bust. They may not be ready yet; the crop still refuses a slice. - const faces = reportage ? (await db.face.findMany({ where: { photoId }, select: { x0: true, y0: true, x1: true, y1: true } })).map((f) => ({ x0: f.x0, y0: f.y0, x1: f.x1, y1: f.y1 })) : []; + // Faces tell a whole figure from a bust, and block a soft / faceless body in front from being the subject. + // They may not be ready yet; the crop still refuses a soft AF point without a face when faces exist. + const faces = (await db.face.findMany({ where: { photoId }, select: { x0: true, y0: true, x1: true, y1: true } })).map((f) => ({ + x0: f.x0, + y0: f.y0, + x1: f.x1, + y1: f.y1, + })); const planned = planFrame({ detections, width: W, diff --git a/apps/api/src/composition/compose.ts b/apps/api/src/composition/compose.ts index 6039c45..cbf4935 100644 --- a/apps/api/src/composition/compose.ts +++ b/apps/api/src/composition/compose.ts @@ -3,7 +3,8 @@ * first pass. Pure geometry over detections (people, balls, and how sharp each person is), so it is fast, * testable and easy to tune. Tuned against Peter's own Lightroom crops (handball, October 2026): * - * - The subject is who is in focus, not who is biggest: blurred players in the foreground are scenery. + * - The subject is who is in focus with a visible face, not who is biggest: blurred / faceless players in + * the foreground are scenery (even when the AF point landed on them). * - With a ball in play, the ball and the sharp players contesting it come along. Which ball that is * depends on the sport: handball plays it in the hands, football plays it on the ground. * - The crop is usually two thirds of the frame (the same shape), the subject in the middle across, the upper @@ -200,17 +201,30 @@ function forReportage(d: Detections, faces: readonly Box[]): Detections { } /** Who the photo is about. Null when there is nobody clear enough (empty pitch, distant crowd). */ -export function findSubject(d: Detections, focus?: FocusHint | null, opts?: { reportage?: boolean; sport?: string | null }): { box: Box; reason: Composition["reason"]; people: Detection[]; ball?: Box } | null { +export function findSubject( + d: Detections, + focus?: FocusHint | null, + opts?: { reportage?: boolean; sport?: string | null; faces?: readonly Box[] }, +): { box: Box; reason: Composition["reason"]; people: Detection[]; ball?: Box } | null { const people = d.people.filter((p) => p.score >= MIN_PERSON && h(p) >= MIN_HEIGHT); if (!people.length) return null; - // The main subject: the person the camera focused on, when that point lands on someone. Otherwise in focus - // first, then a reasonable size, then whole in the photo. In reportage a whole figure beats a sharper bust. + // The main subject: the person the camera focused on, when that point lands on someone who is sharp and + // whose face is visible. A soft / faceless body in the foreground (AF on the jump in front) is scenery — + // never the crop's subject when a sharp face sits behind. Otherwise: in focus, then size, then whole. + const faces = opts?.faces ?? []; + const faced = faces.length ? people.filter((p) => faceOf(p, faces)) : people; + const candidates = faced.length ? faced : people; const weight = (p: Detection) => sharp(p) ** 2 * Math.min(1, h(p) / 0.3) * (atEdge(p) ? 0.4 : 1) * (0.5 + 0.5 * p.score); - const whole = opts?.reportage ? people.filter((p) => !p.fragment) : people; - const pool = whole.length ? whole : people; - const aimed = focus ? personAtFocus(pool, focus) : null; - const main = aimed ?? pool.reduce((a, b) => (weight(b) > weight(a) ? b : a)); - if (!aimed && weight(main) < 0.05) return null; + const whole = opts?.reportage ? candidates.filter((p) => !p.fragment) : candidates; + const pool = whole.length ? whole : candidates; + const sharpest = Math.max(1e-6, ...people.map(sharp)); + const aimed = focus ? personAtFocus(pool, focus) ?? personAtFocus(people, focus) : null; + const aimedOk = + !!aimed && + sharp(aimed) >= TUNING.inFocus * sharpest && + (!faces.length || faced.length === 0 || !!faceOf(aimed, faces)); + const main = (aimedOk ? aimed : null) ?? pool.reduce((a, b) => (weight(b) > weight(a) ? b : a)); + if (!aimedOk && weight(main) < 0.05) return null; // Reportage keeps a conversation together: someone in the shade is still part of it. A match stays stricter. // A body the frame has already cut in half is not part of that conversation, unless it is the subject. const withMain = opts?.reportage ? 0.35 : TUNING.inFocus; @@ -283,7 +297,8 @@ function personAtFocus(people: Detection[], focus: FocusHint): Detection | null */ export function compose(d: Detections, aspect: number | null = null, opts: ComposeOptions = {}): Composition | null { const reportage = opts.context === "reportage"; - const found = findSubject(reportage ? forReportage(d, opts.faces ?? []) : d, opts.focus, { reportage, sport: opts.sport ?? null }); + const faces = opts.faces ?? []; + const found = findSubject(reportage ? forReportage(d, faces) : d, opts.focus, { reportage, sport: opts.sport ?? null, faces }); if (!found) return null; const { box: s, reason, people: inSubject, ball } = found; const ratio = (aspect ?? d.width / d.height) * (d.height / d.width); // width/height in frame fractions @@ -515,7 +530,8 @@ function planCut(p: Detection, at: number): number | null { */ export function planCrop(d: Detections, which: keyof typeof PLANS, aspect: number | null = null, opts: ComposeOptions = {}): Box | null { const reportage = opts.context === "reportage"; - const found = findSubject(reportage ? forReportage(d, opts.faces ?? []) : d, opts.focus, { reportage, sport: opts.sport ?? null }); + const faces = opts.faces ?? []; + const found = findSubject(reportage ? forReportage(d, faces) : d, opts.focus, { reportage, sport: opts.sport ?? null, faces }); if (!found) return null; const { box: s, reason, people: inSubject } = found; const tallest = Math.max(...inSubject.map(h)); diff --git a/apps/api/src/composition/focus-subject.test.ts b/apps/api/src/composition/focus-subject.test.ts index 937dfe2..6dc2c4a 100644 --- a/apps/api/src/composition/focus-subject.test.ts +++ b/apps/api/src/composition/focus-subject.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import { findSubject, type Detection } from "./compose.js"; +import { compose, findSubject, type Detection } from "./compose.js"; const person = (x0: number, y0: number, x1: number, y1: number, score = 0.9): Detection => ({ x0, y0, x1, y1, score, sharpness: 1 }); const frame = { width: 6000, height: 4000, balls: [] }; @@ -20,4 +20,22 @@ describe("focus point", () => { const missed = findSubject({ ...frame, people: [big, aimed] }, { x: 0.95, y: 0.05 })!; expect(missed.box.x0).toBeLessThan(0.2); }); + + it("ignores AF on a soft faceless jumper in front when a sharp face sits behind", () => { + const jumper = { ...person(0.35, 0.05, 0.72, 0.95, 0.95), sharpness: 0.2 }; + const withBall = { ...person(0.28, 0.28, 0.42, 0.72, 0.9), sharpness: 1 }; + const mate = { ...person(0.18, 0.3, 0.3, 0.7, 0.85), sharpness: 0.95 }; + const faces = [ + { x0: 0.3, y0: 0.28, x1: 0.38, y1: 0.38 }, + { x0: 0.2, y0: 0.3, x1: 0.27, y1: 0.39 }, + ]; + const found = findSubject({ width: 6000, height: 4000, people: [jumper, withBall, mate], balls: [] }, { x: 0.55, y: 0.35 }, { faces })!; + expect(found.people.map((p) => p.x0)).toContain(withBall.x0); + expect(found.people.map((p) => p.x0)).not.toContain(jumper.x0); + const c = compose({ width: 6000, height: 4000, people: [jumper, withBall, mate], balls: [] }, null, { + focus: { x: 0.55, y: 0.35 }, + faces, + })!; + expect(c.subject.y0).toBeGreaterThan(jumper.y0 + 0.1); + }); }); diff --git a/docs/photo-editing-rules.md b/docs/photo-editing-rules.md index cd7f062..2fa8d1f 100644 --- a/docs/photo-editing-rules.md +++ b/docs/photo-editing-rules.md @@ -20,10 +20,11 @@ Following the AP and NPPA codes: ## 2. Crop - **Who:** the subject is the person the camera focused on, when the file has a focus point (EXIF SubjectArea: - the autofocus point, shown by Photo Mechanic). That person wins even if someone else is larger. With no point, - or a point on nobody, the subject is the person in focus, not the biggest one. With a ball in play, the ball and - everyone contesting it come along. Without one, the subject and the sharp people with them (a duel, a - celebration, a line-up). + the autofocus point, shown by Photo Mechanic) **and that person is sharp with a fully visible face**. A soft or + faceless body in the foreground (AF landed on the jump in front) is scenery — never the crop — when a sharp + face sits behind. With no usable point, the subject is the sharp person with a face, not the biggest one. With + a ball in play, the ball and everyone contesting it come along. Without one, the subject and the sharp people + with them (a duel, a celebration, a line-up). Non-person photos (empty pitch, kit detail) are unchanged. - **Who is not the subject:** they are in whole, or they are out. A person the photo has already cut in half stays out when the crop can move into the clean space around the subject. When leaving them out would cut the subject or the ball, the crop does the best it can. The subject themselves may stand at the frame edge. -- 2.47.3