diff --git a/apps/api/src/autopilot/service.ts b/apps/api/src/autopilot/service.ts index a4c9026..592bece 100644 --- a/apps/api/src/autopilot/service.ts +++ b/apps/api/src/autopilot/service.ts @@ -254,8 +254,9 @@ export class AutopilotService { picked += toFlag.length; } if (reportage) { - // A sharp sequence: the second frame is a half body, the third a three-quarter. The first keeps the - // whole person. A plan that cannot be made (too small, or the same as the full crop) is skipped. + // A sharp sequence may carry a tighter plan (three-quarter; a half that would cut the hands is not made). + // The first frame keeps the whole person. A plan that would cut a neck or the hands, or that matches + // the open crop, is skipped: that frame stays open. for (const { f: framePick, key } of chosen) { if (!key) continue; const planned = framePick.rec?.plans?.[key]; diff --git a/apps/api/src/composition/compose.ts b/apps/api/src/composition/compose.ts index 11a93d4..d5fc583 100644 --- a/apps/api/src/composition/compose.ts +++ b/apps/api/src/composition/compose.ts @@ -444,9 +444,24 @@ function strayLimb(p: Detection, tallest: number): boolean { } /** - * A second or third plan of the same moment (half body, three-quarter). The bottom sits mid-limb, the head and - * the hands inside the subject box stay, and the foot is not where the cut falls. Null when that plan cannot - * keep the delivery's resolution. + * Where a sequence plan may end on this person, measured on their own box. A line taken from the top of the + * group lands on the neck of whoever stands lower. The hands stay: at the hip on a standing figure, and at the + * bottom of the box when the person is leaning (the box is wide because the arms are in it). Null: this plan + * would cut the neck or the hands, so the open crop stands and no second frame is made just to show a worse one. + */ +function planCut(p: Detection, at: number): number | null { + const line = p.y0 + at * h(p); + if (line < p.y0 + 0.28 * h(p)) return null; + if (line < p.y0 + 0.55 * h(p)) return null; + if (h(p) < 1.8 * Math.max(w(p), 1e-6) && line < p.y1 - 0.08 * h(p)) return null; + if (p.fragment && line < p.y1 - 0.04 * h(p)) return null; + return line; +} + +/** + * A second or third plan of the same moment (half body, three-quarter). The bottom sits below the hands, the + * head stays, and the foot is not where the cut falls. Null when that plan would cut a neck or a hand, or cannot + * keep the delivery's resolution: the photo keeps the open crop. */ export function planCrop(d: Detections, which: keyof typeof PLANS, aspect: number | null = null, opts: ComposeOptions = {}): Box | null { const reportage = opts.context === "reportage"; @@ -456,7 +471,9 @@ export function planCrop(d: Detections, which: keyof typeof PLANS, aspect: numbe const tallest = Math.max(...inSubject.map(h)); const at = PLANS[which]; if (at > FOOT_ZONE || JOINTS.some((j) => Math.abs(at - j) < JOINT_BAND)) return null; - const bottom = s.y0 + at * tallest; + const lines = inSubject.map((p) => planCut(p, at)); + if (lines.some((line) => line === null)) return null; + const bottom = Math.max(...(lines as number[])); const ratio = (aspect ?? d.width / d.height) * (d.height / d.width); const others = d.people.filter((p) => p.score >= MIN_PERSON && !inSubject.includes(p)); const made = frame(s, ratio, bottom, 0.04 * tallest, tallest, others, inSubject, reason, opts.minWidth ?? 0, true, true, reportage); diff --git a/apps/api/test/compose.test.ts b/apps/api/test/compose.test.ts index 8c5066c..48414b5 100644 --- a/apps/api/test/compose.test.ts +++ b/apps/api/test/compose.test.ts @@ -241,20 +241,27 @@ describe("reportage", () => { expect(c.rect.x1 - c.rect.x0).toBeLessThan(0.92); }); - it("makes a half-body plan above the hips and a three-quarter above the knee, head included", () => { + it("makes a three-quarter plan below the hands, and refuses a half that would cut them", () => { const p = person(0.4, 0.1, 0.55, 0.9); const d = { ...frame, people: [p], balls: [] }; - const half = planCrop(d, "half")!; + expect(planCrop(d, "half")).toBeNull(); const three = planCrop(d, "threeQuarter")!; const at = (y: number) => (y - p.y0) / (p.y1 - p.y0); - expect(half.y0).toBeLessThanOrEqual(p.y0); - expect(half.x0).toBeLessThanOrEqual(p.x0); - expect(half.x1).toBeGreaterThanOrEqual(p.x1); - expect(at(half.y1)).toBeGreaterThan(0.35); - expect(at(half.y1)).toBeLessThan(0.5); + expect(three.y0).toBeLessThanOrEqual(p.y0); + expect(three.x0).toBeLessThanOrEqual(p.x0); + expect(three.x1).toBeGreaterThanOrEqual(p.x1); expect(at(three.y1)).toBeGreaterThan(0.58); expect(at(three.y1)).toBeLessThan(0.72); - expect(half.y1).toBeLessThan(p.y1); expect(three.y1).toBeLessThan(p.y1); }); + + it("refuses a sequence crop that would cut a lower neck or the hands of someone leaning", () => { + const high = person(0.3, 0.12, 0.48, 0.92); + const low = person(0.5, 0.28, 0.68, 0.96); + const group = { ...frame, people: [high, low], balls: [] }; + expect(planCrop(group, "half", null, { context: "reportage" })).toBeNull(); + const leaning = person(0.2, 0.18, 0.72, 0.86); + expect(planCrop({ ...frame, people: [leaning], balls: [] }, "threeQuarter", null, { context: "reportage" })).toBeNull(); + expect(planCrop({ ...frame, people: [leaning], balls: [] }, "half", null, { context: "reportage" })).toBeNull(); + }); });