From 4b7de68e5104be399d4d8d8aee0cedd70c0b640f Mon Sep 17 00:00:00 2001 From: spark Date: Sat, 7 Nov 2026 15:45:14 +0000 Subject: [PATCH] feat(autopilot): learn a crop by where it sits on the person A correction now records the cut on the body, the space above the head and whether the arms are taken off. The next crop is placed the same way. --- apps/api/src/autopilot/cropBias.ts | 23 +++++++++++++ apps/api/src/autopilot/learning.ts | 48 +++++++++++++++++++++------ apps/api/src/composition/geometry.ts | 29 ++++++++++++++++ apps/api/src/composition/planFrame.ts | 12 +++++-- apps/api/test/autopilot.test.ts | 17 ++++++++++ apps/app/src/i18n.ts | 10 ++++++ apps/app/src/library/Autopilot.tsx | 8 +++-- docs/photo-editing-rules.md | 2 +- packages/sdk/src/types.ts | 2 +- 9 files changed, 135 insertions(+), 16 deletions(-) diff --git a/apps/api/src/autopilot/cropBias.ts b/apps/api/src/autopilot/cropBias.ts index 49a9876..0ff5437 100644 --- a/apps/api/src/autopilot/cropBias.ts +++ b/apps/api/src/autopilot/cropBias.ts @@ -17,4 +17,27 @@ export interface CropBias { scale: number; dAngle: number; scene: "ball" | "group" | "person" | null; + /** + * Where the crop sits on the person, not how much smaller it is. + * 0 is the top of the body, 1 is the feet. Under 1 means the legs were cut. + */ + cut?: number | null; + /** Space above the head, as a fraction of the body. Negative: the head is cut. */ + head?: number | null; + /** How much of the body's width the side edges remove (arms and hands). */ + sides?: number | null; +} + +/** Where a crop sits on a person. Null when the body box is too small to read. */ +export function frameIntent(crop: Box, subject: Box): { cut: number; head: number; sides: number } | null { + const height = subject.y1 - subject.y0; + const width = subject.x1 - subject.x0; + if (height < 0.08 || width < 0.02) return null; + const cut = (crop.y1 - subject.y0) / height; + if (cut < 0.2 || cut > 1.45) return null; + const head = (subject.y0 - crop.y0) / height; + const left = Math.max(0, crop.x0 - subject.x0) / width; + const right = Math.max(0, subject.x1 - crop.x1) / width; + const round = (n: number) => Math.round(n * 1000) / 1000; + return { cut: round(cut), head: round(head), sides: round((left + right) / 2) }; } diff --git a/apps/api/src/autopilot/learning.ts b/apps/api/src/autopilot/learning.ts index 0cdd6ae..cf04ff9 100644 --- a/apps/api/src/autopilot/learning.ts +++ b/apps/api/src/autopilot/learning.ts @@ -1,7 +1,7 @@ import type { EditCommand } from "@photoreport/contracts"; import type { Db } from "../db.js"; import type { Box } from "../composition/compose.js"; -import type { CropBias, FoldedCrop } from "./cropBias.js"; +import { type CropBias, type FoldedCrop, frameIntent } from "./cropBias.js"; import { TONE_KEYS, WB_KEYS } from "./look.js"; export type { CropBias, FoldedCrop } from "./cropBias.js"; @@ -11,8 +11,8 @@ export { TONE_KEYS, WB_KEYS }; * Learning from corrections (docs/photo-editing-rules.md §4): when someone changes what the autopilot did to a * photo — in Revelar or by writing a soft crop into the file in Lightroom / Photo Mechanic — the difference is * kept (one row per photo, the latest state wins). The autopilot then applies the consistent part of the recent - * differences for the same photographer to its next edits: tone, white balance, and crop geometry (shift, scale, - * angle), including a looser frame when they consistently open the crop. + * differences for the same photographer to its next edits: tone, white balance, and where the crop sits on the + * person (legs cut, space above the head, arms taken off), with a plain tighter or looser frame only as fallback. * * Creative looks are left alone. A correction counts only when most recent ones agree on the direction. */ @@ -38,6 +38,9 @@ const CROP_DY = "__crop.dy"; const CROP_SCALE = "__crop.scale"; const CROP_ANGLE = "__crop.dAngle"; const CROP_SCENE = "__crop.scene"; +const CROP_CUT = "__crop.cut"; +const CROP_HEAD = "__crop.head"; +const CROP_SIDES = "__crop.sides"; const SCENE_CODE: Record<"ball" | "group" | "person", number> = { ball: 1, group: 2, person: 3 }; const SCENE_FROM: Record = { 1: "ball", 2: "group", 3: "person" }; @@ -65,19 +68,21 @@ const area = (b: Box) => (b.x1 - b.x0) * (b.y1 - b.y0); const cx = (b: Box) => (b.x0 + b.x1) / 2; const cy = (b: Box) => (b.y0 + b.y1) / 2; -export function cropBiasOf(auto: FoldedCrop, final: FoldedCrop, scene: CropBias["scene"] = null): CropBias | null { +export function cropBiasOf(auto: FoldedCrop, final: FoldedCrop, scene: CropBias["scene"] = null, subject: Box | null = null): CropBias | null { if (area(auto.rect) <= 0) return null; const scale = Math.sqrt(area(final.rect) / area(auto.rect)); const dx = cx(final.rect) - cx(auto.rect); const dy = cy(final.rect) - cy(auto.rect); const dAngle = final.angle - auto.angle; - if (Math.abs(dx) < 0.005 && Math.abs(dy) < 0.005 && Math.abs(scale - 1) < 0.03 && Math.abs(dAngle) < 0.15) return null; + const onPerson = subject ? frameIntent(final.rect, subject) : null; + if (!onPerson && Math.abs(dx) < 0.005 && Math.abs(dy) < 0.005 && Math.abs(scale - 1) < 0.03 && Math.abs(dAngle) < 0.15) return null; return { dx: Math.round(dx * 1000) / 1000, dy: Math.round(dy * 1000) / 1000, scale: Math.round(scale * 1000) / 1000, dAngle: Math.round(dAngle * 100) / 100, scene, + ...(onPerson ?? {}), }; } @@ -87,6 +92,9 @@ function packBias(deltas: Record, bias: CropBias): void { deltas[CROP_SCALE] = bias.scale; deltas[CROP_ANGLE] = bias.dAngle; if (bias.scene) deltas[CROP_SCENE] = SCENE_CODE[bias.scene]; + if (typeof bias.cut === "number") deltas[CROP_CUT] = bias.cut; + if (typeof bias.head === "number") deltas[CROP_HEAD] = bias.head; + if (typeof bias.sides === "number") deltas[CROP_SIDES] = bias.sides; } export function unpackBias(deltas: Record): CropBias | null { @@ -99,6 +107,9 @@ export function unpackBias(deltas: Record): CropBias | null { scale, dAngle: typeof deltas[CROP_ANGLE] === "number" ? deltas[CROP_ANGLE]! : 0, scene: typeof sceneCode === "number" ? (SCENE_FROM[sceneCode] ?? null) : null, + ...(typeof deltas[CROP_CUT] === "number" ? { cut: deltas[CROP_CUT]! } : {}), + ...(typeof deltas[CROP_HEAD] === "number" ? { head: deltas[CROP_HEAD]! } : {}), + ...(typeof deltas[CROP_SIDES] === "number" ? { sides: deltas[CROP_SIDES]! } : {}), }; } @@ -117,6 +128,7 @@ export function correctionOf( auto: FoldedEdits, final: FoldedEdits, scene: CropBias["scene"] = null, + subject: Box | null = null, ): { deltas: Record; cropScale: number | null; cropBias: CropBias | null } | null { const deltas: Record = {}; for (const k of TONE_KEYS) { @@ -129,7 +141,7 @@ export function correctionOf( const d = final.values[k]! - auto.values[k]!; if (Math.abs(d) <= limitOf(k).max) deltas[k] = Math.round(d * 1000) / 1000; } - const cropBias = auto.crop && final.crop ? cropBiasOf(auto.crop, final.crop, scene) : null; + const cropBias = auto.crop && final.crop ? cropBiasOf(auto.crop, final.crop, scene, subject) : null; const cropScale = cropBias ? cropBias.scale : null; if (cropBias) packBias(deltas, cropBias); if (!Object.keys(toneOffsets(deltas)).length && !cropBias) return null; @@ -147,9 +159,11 @@ export async function recordCorrection(db: Db, photoId: string): Promise e.clientId !== "autopilot")) return false; const auto = fold(edits.filter((e) => e.clientId === "autopilot").map((e) => e.command as EditCommand)); const final = fold(edits.map((e) => e.command as EditCommand)); - const reason = (photo.autopilot as { reason?: string } | null)?.reason; + const stored = photo.autopilot as { reason?: string; subject?: Box } | null; + const reason = stored?.reason; const scene = reason === "ball" || reason === "group" || reason === "person" ? reason : null; - const c = correctionOf(auto, final, scene); + const subject = stored?.subject && typeof stored.subject.x0 === "number" ? stored.subject : null; + const c = correctionOf(auto, final, scene, subject); if (!c) { await db.developCorrection.deleteMany({ where: { photoId } }); return false; @@ -165,6 +179,16 @@ export async function recordCorrection(db: Db, photoId: string): Promise typeof x === "number"); + if (nums.length < MIN_SAMPLES) return null; + const m = median(nums); + const agree = nums.filter((x) => Math.abs(x - m) <= tol).length / nums.length; + if (agree < AGREEMENT) return null; + return Math.round(m * 1000) / 1000; +} + const median = (xs: number[]) => { const s = [...xs].sort((a, b) => a - b); const m = Math.floor(s.length / 2); @@ -225,14 +249,18 @@ export function styleOf( const mAng = median(angs); const agreeDx = dxs.filter((x) => Math.sign(x) === Math.sign(mDx) || Math.abs(mDx) < 0.01).length / dxs.length; const agreeDy = dys.filter((x) => Math.sign(x) === Math.sign(mDy) || Math.abs(mDy) < 0.01).length / dys.length; - if ((Math.abs(mDx) >= 0.01 && agreeDx >= AGREEMENT) || (Math.abs(mDy) >= 0.01 && agreeDy >= AGREEMENT) || cropFactor) { + const cut = agreed(pool.map((b) => b.cut), 0.1); + const head = agreed(pool.map((b) => b.head), 0.06); + const sides = agreed(pool.map((b) => b.sides), 0.08); + if ((Math.abs(mDx) >= 0.01 && agreeDx >= AGREEMENT) || (Math.abs(mDy) >= 0.01 && agreeDy >= AGREEMENT) || cropFactor || cut !== null) { cropBias = { dx: Math.abs(mDx) >= 0.01 && agreeDx >= AGREEMENT ? Math.round(Math.max(-0.12, Math.min(0.12, mDx)) * 1000) / 1000 : 0, dy: Math.abs(mDy) >= 0.01 && agreeDy >= AGREEMENT ? Math.round(Math.max(-0.12, Math.min(0.12, mDy)) * 1000) / 1000 : 0, scale: cropFactor ?? 1, dAngle: Math.abs(mAng) >= 0.2 ? Math.round(Math.max(-3, Math.min(3, mAng)) * 100) / 100 : 0, + ...(cut !== null ? { cut, head, sides: sides ?? 0 } : {}), }; - if (!cropFactor && cropBias.dx === 0 && cropBias.dy === 0 && cropBias.dAngle === 0) cropBias = null; + if (!cropFactor && cropBias.dx === 0 && cropBias.dy === 0 && cropBias.dAngle === 0 && cut === null) cropBias = null; } } else { // Legacy rows that only stored cropScale. diff --git a/apps/api/src/composition/geometry.ts b/apps/api/src/composition/geometry.ts index 0a5bc2d..bef5b66 100644 --- a/apps/api/src/composition/geometry.ts +++ b/apps/api/src/composition/geometry.ts @@ -74,6 +74,35 @@ export function biasCrop(r: Box, subject: Box, bias: { dx: number; dy: number; s return { x0, y0, x1: x0 + w, y1: y0 + h }; } +/** + * Place the crop on the person the way the photographer does: the bottom at `cut` (1 = feet, less = legs removed), + * the top at `head` above the head, the sides eating `sides` of the body. The crop keeps the shape of `base`. + * Unlike `biasCrop`, the body is allowed to leave the frame: that is the correction being copied. + */ +export function frameOnSubject(base: Box, subject: Box, intent: { cut?: number | null; head?: number | null; sides?: number | null }): Box { + const sh = Math.max(0.05, subject.y1 - subject.y0); + const sw = Math.max(0.02, subject.x1 - subject.x0); + const cut = Math.min(1.25, Math.max(0.4, intent.cut ?? (base.y1 - subject.y0) / sh)); + const head = Math.min(0.4, Math.max(-0.1, intent.head ?? (subject.y0 - base.y0) / sh)); + const sides = Math.min(0.4, Math.max(0, intent.sides ?? 0)); + let y1 = Math.min(1, Math.max(0.2, subject.y0 + cut * sh)); + let y0 = Math.min(y1 - 0.12, Math.max(0, subject.y0 - head * sh)); + if (y0 < 0) y0 = 0; + const ratio = (base.x1 - base.x0) / Math.max(0.05, base.y1 - base.y0); + let height = y1 - y0; + let width = height * ratio; + if (width > 1) { + width = 1; + height = width / ratio; + y0 = Math.max(0, y1 - height); + } + const cx = (subject.x0 + sides * sw / 2 + subject.x1 - sides * sw / 2) / 2; + let x0 = cx - width / 2; + if (x0 < 0) x0 = 0; + if (x0 + width > 1) x0 = Math.max(0, 1 - width); + return { x0, y0, x1: x0 + width, y1: y0 + height }; +} + /** Expand `r` by `factor` (>1) about the subject's centre, without leaving the photo. */ function looser(r: Box, subject: Box, factor: number): Box { const w = Math.min(1, (r.x1 - r.x0) * factor); diff --git a/apps/api/src/composition/planFrame.ts b/apps/api/src/composition/planFrame.ts index 3af1f7c..8876132 100644 --- a/apps/api/src/composition/planFrame.ts +++ b/apps/api/src/composition/planFrame.ts @@ -1,6 +1,6 @@ import type { EditCommand } from "@photoreport/contracts"; import { type Box, type Detections, type FocusHint, clipsBall, compose, minCropWidth, planCrop } from "./compose.js"; -import { biasCrop, constrainRotated, tighter } from "./geometry.js"; +import { biasCrop, constrainRotated, frameOnSubject, tighter } from "./geometry.js"; import { type Look, WB_KEYS, TONE_KEYS, detailForCrop, lookValues, toneApplied } from "../autopilot/look.js"; import type { CropBias } from "../autopilot/cropBias.js"; @@ -96,7 +96,15 @@ export function planFrame(input: { let rect = composition.rect; const bias = input.cropBias; const factor = bias?.scale ?? input.cropFactor ?? null; - if (bias && (bias.dx || bias.dy || (factor && factor !== 1))) { + if (bias && typeof bias.cut === "number") { + // The photographer's cut on the body (legs, head, arms), not a uniform shrink. + rect = frameOnSubject(composition.rect, composition.subject, bias); + if (bias.dx) { + const w = rect.x1 - rect.x0; + const x0 = Math.min(Math.max(0, rect.x0 + bias.dx), 1 - w); + rect = { ...rect, x0, x1: x0 + w }; + } + } else if (bias && (bias.dx || bias.dy || (factor && factor !== 1))) { rect = biasCrop(composition.rect, composition.subject, { dx: bias.dx, dy: bias.dy, scale: factor ?? 1 }); } else if (factor && factor !== 1) { rect = factor < 1 ? tighter(composition.rect, composition.subject, factor) : biasCrop(composition.rect, composition.subject, { dx: 0, dy: 0, scale: factor }); diff --git a/apps/api/test/autopilot.test.ts b/apps/api/test/autopilot.test.ts index 69c71ce..f6c933c 100644 --- a/apps/api/test/autopilot.test.ts +++ b/apps/api/test/autopilot.test.ts @@ -166,6 +166,23 @@ describe("learning from corrections", () => { expect(openStyle.cropFactor).toBeGreaterThan(1); expect(openStyle.cropBias?.dx).toBeCloseTo(0.02, 2); }); + + it("learns where the crop cuts the person, not only the ratio", () => { + const subject = { x0: 0.3, y0: 0.15, x1: 0.7, y1: 0.95 }; + const auto = fold([crop(0.1, 0.9)]); + // Bottom at the waist: (0.55 - 0.15) / 0.8 = 0.5. Sides eat into the body. + const final = fold([{ command: "crop.set", params: { rect: { x0: 0.38, y0: 0.12, x1: 0.62, y1: 0.55 }, angle: 0, aspect: null } }]); + const c = correctionOf(auto, final, "person", subject); + expect(c?.cropBias?.cut).toBeCloseTo(0.5, 2); + expect(c?.cropBias?.sides).toBeGreaterThan(0.08); + const rows = [0.5, 0.52, 0.48, 0.51, 0.49].map((cut) => ({ + deltas: { "__crop.scale": 0.8, "__crop.dx": 0, "__crop.dy": 0, "__crop.dAngle": 0, "__crop.cut": cut, "__crop.head": 0.04, "__crop.sides": 0.12 }, + cropScale: 0.8, + })); + const style = styleOf(rows, null); + expect(style.cropBias?.cut).toBeCloseTo(0.5, 2); + expect(style.cropBias?.sides).toBeCloseTo(0.12, 2); + }); }); describe("autopilot", () => { diff --git a/apps/app/src/i18n.ts b/apps/app/src/i18n.ts index 4decd2d..d3a480b 100644 --- a/apps/app/src/i18n.ts +++ b/apps/app/src/i18n.ts @@ -572,6 +572,16 @@ export const t = { nothingLearnt: "Corrige no Revelar as fotos que o piloto editou: ao fim de 5 correções no mesmo sentido, passa a editar como tu.", tighter: (pct: number) => `recorte ${pct}% mais apertado`, looser: (pct: number) => `recorte ${pct}% mais aberto`, + cutBody: (cut: number) => { + if (cut < 0.55) return "Cortas as pessoas pelo peito"; + if (cut < 0.72) return "Cortas as pessoas pela cintura"; + if (cut < 0.88) return "Cortas as pessoas a meio da perna"; + if (cut < 1.02) return "Deixas o corpo inteiro"; + return "Deixas chão por baixo dos pés"; + }, + cutHead: "Cortas a cabeça", + headroom: "Deixas espaço por cima da cabeça", + cutArms: "Tiras os braços do enquadramento", forget: "Esquecer", controls: { "light.exposure": "Exposição", diff --git a/apps/app/src/library/Autopilot.tsx b/apps/app/src/library/Autopilot.tsx index fd3a697..4f696b0 100644 --- a/apps/app/src/library/Autopilot.tsx +++ b/apps/app/src/library/Autopilot.tsx @@ -194,8 +194,12 @@ export function Learnt({ api, event }: { api: ApiClient; event: EventSummary }) {L.controls[k] ?? k} {fmt(k, v)} ))} - {style.cropFactor && style.cropFactor < 0.97 &&
  • {L.tighter(Math.round((1 - style.cropFactor) * 100))}
  • } - {style.cropFactor && style.cropFactor > 1.03 &&
  • {L.looser(Math.round((style.cropFactor - 1) * 100))}
  • } + {typeof style.cropBias?.cut === "number" &&
  • {L.cutBody(style.cropBias.cut)}
  • } + {typeof style.cropBias?.head === "number" && style.cropBias.head < -0.02 &&
  • {L.cutHead}
  • } + {typeof style.cropBias?.head === "number" && style.cropBias.head > 0.08 &&
  • {L.headroom}
  • } + {typeof style.cropBias?.sides === "number" && style.cropBias.sides > 0.08 &&
  • {L.cutArms}
  • } + {typeof style.cropBias?.cut !== "number" && style.cropFactor && style.cropFactor < 0.97 &&
  • {L.tighter(Math.round((1 - style.cropFactor) * 100))}
  • } + {typeof style.cropBias?.cut !== "number" && style.cropFactor && style.cropFactor > 1.03 &&
  • {L.looser(Math.round((style.cropFactor - 1) * 100))}
  • }

    {L.learntHelp(style.samples, style.venue)}{" "} diff --git a/docs/photo-editing-rules.md b/docs/photo-editing-rules.md index 568b0d0..7bac7d6 100644 --- a/docs/photo-editing-rules.md +++ b/docs/photo-editing-rules.md @@ -152,7 +152,7 @@ measurement, the median of the last 60 corrections, at the same venue when there - only the controls it sets itself (tone, white balance) and crop tightness, never a creative look; - only with 5 or more corrections, and only when at least 70% of them go the same way (one odd photo changes nothing); tiny differences are noise and very large ones creative choices, both ignored; -- the crop only gets tighter, never cutting into the subject (looser would eat the margins the subject needs). +- the crop is read on the person, not only as a ratio: where the bottom cuts the body (chest, waist, mid-leg, feet), space above the head, and whether the sides take the arms. When those agree, the next crop is placed the same way on the next person. A plain tighter or looser frame is only the fallback when the body position was not known. A learnt crop still may not slice a ball the frame had kept whole. The autopilot's panel in the app shows what it has learnt and has "Esquecer". The burst picks are not learnt yet. diff --git a/packages/sdk/src/types.ts b/packages/sdk/src/types.ts index 2925fc4..96e9eb9 100644 --- a/packages/sdk/src/types.ts +++ b/packages/sdk/src/types.ts @@ -336,7 +336,7 @@ export interface AutopilotStyle { offsets: Record; cropFactor: number | null; /** Centre shift / open / straighten from crop corrections (in-app or Lightroom soft crop). */ - cropBias: { dx: number; dy: number; scale: number; dAngle: number } | null; + cropBias: { dx: number; dy: number; scale: number; dAngle: number; cut?: number | null; head?: number | null; sides?: number | null } | null; } export interface DeliveryTargetInput { -- 2.47.3