diff --git a/apps/api/src/composition/compose.ts b/apps/api/src/composition/compose.ts index b921bba..2a61c27 100644 --- a/apps/api/src/composition/compose.ts +++ b/apps/api/src/composition/compose.ts @@ -9,9 +9,10 @@ * body a little above the middle and little or no empty pitch under the feet (spare height goes above, to the * crowd and the stands). A foot that is whole in the photo stays in; heads and the ball are never cut. * - Cutting another sharp person in half is avoided; cutting blurred ones doesn't matter. - * - A generic event, and any photo with no ball, is reportage: a whole hand and a whole foot stay in. A leg is - * cut only when the foot already leaves the photo. A stray hand or foot of someone else is cropped out. - * A head in a window or a torso does not beat a whole figure, and a crop never slices someone who is in it. + * - A generic event, and any photo with no ball, is reportage: a whole hand and a whole foot stay in. When the + * foot already leaves the photo, the crop ends mid-shin or mid-thigh, never on the sock. A person the frame + * has already cut in half stays out. A stray hand or foot of someone else is cropped out. A head in a window + * or a torso does not beat a whole figure, and a crop never slices someone who is in it. * - "tight" is the agencies' handball look, and only with a ball in play: the subject fills the height, cut * mid-shin or mid-thigh when the foot is already out. "full" keeps whole bodies when they fit. * @@ -199,19 +200,25 @@ export function findSubject(d: Detections, focus?: FocusHint | null, opts?: { re const main = aimed ?? pool.reduce((a, b) => (weight(b) > weight(a) ? b : a)); if (!aimed && weight(main) < 0.05) return null; // Reportage keeps a conversation together: someone in the shade is still part of it. A match stays stricter. - // Someone who merely touches the edge but is a whole figure is still in the conversation. A short box on the - // edge is a stray hand or foot. + // 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; const reach = opts?.reportage ? 0.85 : 0.45; - const leaves = (p: Detection) => atEdge(p) && (!opts?.reportage || h(p) < 0.5); - const focused = people.filter((p) => sharp(p) >= withMain * sharp(main) && !leaves(p)); + const cutOff = (p: Detection) => atEdge(p) && p !== main; + const focused = people.filter((p) => sharp(p) >= withMain * sharp(main) && !cutOff(p)); - // A ball in play near the subject: in the hands, at the feet, or in the air just above. + // A ball in play near the subject: in the hands, at the feet, or in the air just above. The one on the person + // wins over a spare ball sitting on the floor. const balls = d.balls.filter((b) => b.score >= MIN_BALL && w(b) < 0.6 * h(main) && w(b) > 0.02 * h(main)); - const ball = balls.find((b) => Math.abs(cx(b) - cx(main)) <= w(main) / 2 + 0.5 * h(main) && cy(b) > main.y0 - 0.4 * h(main) && cy(b) < main.y1 + 0.1 * h(main)); + const near = balls.filter((b) => Math.abs(cx(b) - cx(main)) <= w(main) / 2 + 0.5 * h(main) && cy(b) > main.y0 - 0.4 * h(main) && cy(b) < main.y1 + 0.1 * h(main)); + const ball = near.sort((a, b) => { + const held = (x: Box) => (overlap(x, main) > 0 ? 0 : 1); + const dist = (x: Box) => (cx(x) - cx(main)) ** 2 + (cy(x) - cy(main)) ** 2; + return held(a) - held(b) || dist(a) - dist(b); + })[0]; if (ball) { // Everyone big and close to the ball is in the duel, in focus or not (the goalkeeper facing the shot). - const contest = people.filter((p) => p === main || (h(p) >= 0.6 * h(main) && Math.abs(cx(p) - cx(ball)) <= 0.9 * h(main) && overlap(p, { x0: 0, y0: 0, x1: 1, y1: 1 }) > 0.7 * area(p))); + // Not someone the frame has already cut in half: they are background, and the crop leaves them out. + const contest = people.filter((p) => p === main || (!cutOff(p) && h(p) >= 0.6 * h(main) && Math.abs(cx(p) - cx(ball)) <= 0.9 * h(main) && overlap(p, { x0: 0, y0: 0, x1: 1, y1: 1 }) > 0.7 * area(p))); if (!contest.includes(main)) contest.push(main); const box = contest.reduce((u, p) => union(u, p), { x0: ball.x0, y0: ball.y0, x1: ball.x1, y1: ball.y1 }); return { box, reason: "ball", people: contest, ball }; @@ -275,31 +282,53 @@ export function compose(d: Detections, aspect: number | null = null, opts: Compo // for a ball in play. A complete hand or foot is never cut just to fill the frame. const personStory = opts.context === "reportage" || !ball; if (opts.framing === "tight" && !personStory) { - // The ball (and the hands on it) is never cut: the bottom edge stays below it. - const keep = (y: number) => Math.min(s.y1, Math.max(y, ball ? ball.y1 + 0.01 : 0)); - // Feet that are whole in the photo stay in. Mid-shin and mid-thigh are only for a body the frame does - // not finish, and only when the whole body cannot fit. The hip (a joint) is never the cut. + // Feet that are whole in the photo stay in. Cutting the leg is not how a tight frame is made. if (feetIn) { - // Headroom gives way before a complete foot does. Cutting the leg is not how a tight frame is made. + // Headroom gives way before a complete foot does. const whole = frame(s, ratio, sole, headroom, tallest, others, inSubject, reason, minWidth, true) ?? frame(s, ratio, sole, 0, tallest, others, inSubject, reason, minWidth, true) ?? frame(s, ratio, s.y1, 0, tallest, others, inSubject, reason, minWidth, true); if (whole) return { rect: whole.rect, subject: whole.subject, reason: whole.reason, people: whole.people }; } - const tries = [keep(s.y0 + MID_SHIN * tallest), keep(s.y0 + MID_THIGH * tallest)] - .map((bottom) => frame(s, ratio, bottom, headroom, tallest, others, inSubject, reason, minWidth, true)) - .filter((c): c is Composition & { cost: number } => !!c); - const best = tries.reduce<(Composition & { cost: number }) | null>((a, b) => (!a || b.cost < a.cost ? b : a), null); - if (best) return { rect: best.rect, subject: best.subject, reason: best.reason, people: best.people }; - return frame(s, ratio, keep(s.y0 + MID_THIGH * tallest), headroom, tallest, others, inSubject, reason, minWidth, true); + // The foot already leaves the photo: end mid-shin or mid-thigh, not on the sock. + const cut = cutAboveTheAnkle(s, ratio, headroom, tallest, others, inSubject, reason, minWidth, true, false, ball); + if (cut) return { rect: cut.rect, subject: cut.subject, reason: cut.reason, people: cut.people }; + return null; } - // Whole bodies, feet included. A little headroom gives way before a complete foot does. Mid-thigh only when - // the foot already leaves the photo, or the whole body cannot fit at all. + // The foot already leaves the photo. Stopping on the sock is the same as cutting the ankle. + if (!feetIn) { + const cut = cutAboveTheAnkle(s, ratio, headroom, tallest, others, inSubject, reason, minWidth, false, reportage, ball); + if (cut) return { rect: cut.rect, subject: cut.subject, reason: cut.reason, people: cut.people }; + } + // Whole bodies, feet included. A little headroom gives way before a complete foot does. const whole = frame(s, ratio, sole, headroom, tallest, others, inSubject, reason, minWidth, false, false, reportage) ?? frame(s, ratio, sole, 0, tallest, others, inSubject, reason, minWidth, false, false, reportage) ?? frame(s, ratio, s.y1, 0, tallest, others, inSubject, reason, minWidth, false, false, reportage); if (whole || feetIn) return whole; - return frame(s, ratio, s.y0 + MID_THIGH * tallest, headroom, tallest, others, inSubject, reason, minWidth); + return frame(s, ratio, s.y0 + MID_THIGH * tallest, headroom, tallest, others, inSubject, reason, minWidth, false, true, reportage); +} + +/** + * The foot is already out of the photo. The crop ends between the joints (mid-shin or mid-thigh), pinned there + * so it cannot slide down onto the sock. A ball in the hands stays below the cut. The hip, the knee and the + * ankle are never the line. + */ +function cutAboveTheAnkle(s: Box, ratio: number, headroom: number, tallest: number, others: Detection[], inSubject: Detection[], reason: Composition["reason"], minWidth: number, tight: boolean, reportage: boolean, ball?: Box): (Composition & { cost: number }) | null { + // The line is on the person. The subject box can start higher, at a ball held overhead, and that must not + // pull the cut up onto the hip. + const body = inSubject.reduce((a, b) => (h(b) > h(a) ? b : a)); + const keep = (y: number) => Math.min(body.y1, Math.max(y, ball ? ball.y1 + 0.01 : 0)); + const lines = [MID_SHIN, MID_THIGH] + .map((at) => keep(body.y0 + at * h(body))) + .filter((y) => { + const at = (y - body.y0) / Math.max(h(body), 1e-6); + if (at > FOOT_ZONE) return false; + return !JOINTS.some((j) => Math.abs(at - j) < JOINT_BAND); + }); + const tries = lines + .map((bottom) => frame(s, ratio, bottom, headroom, tallest, others, inSubject, reason, minWidth, tight, true, reportage)) + .filter((c): c is Composition & { cost: number } => !!c); + return tries.reduce<(Composition & { cost: number }) | null>((a, b) => (!a || b.cost < a.cost ? b : a), null); } /** diff --git a/apps/api/test/compose.test.ts b/apps/api/test/compose.test.ts index 48414b5..92e15a3 100644 --- a/apps/api/test/compose.test.ts +++ b/apps/api/test/compose.test.ts @@ -102,7 +102,8 @@ describe("tight framing (the agencies' handball look)", () => { const full = compose({ ...frame, people: [p], balls: [ball] })!; const tight = compose({ ...frame, people: [p], balls: [ball] }, null, { framing: "tight" })!; const hT = tight.rect.y1 - tight.rect.y0; - expect(hT).toBeLessThan(full.rect.y1 - full.rect.y0); + // Both stop on the limb once the foot is gone, so the tight frame is not a looser one. + expect(hT).toBeLessThanOrEqual(full.rect.y1 - full.rect.y0); expect(tight.rect.y1).toBeLessThan(p.y1); expect(tight.rect.y0).toBeLessThanOrEqual(p.y0); expect(tight.rect.y1).toBeGreaterThan(ball.y1); @@ -172,6 +173,24 @@ describe("reportage", () => { expect(headCut(beside)).toBe(false); }); + it("leaves out a half person at the frame edge, and does not stop on the sock when the feet are already gone", () => { + // PSR17710. The player fills the frame down to the socks. Another player is already cut in half on the left, + // and a spare ball sits on the floor beside her. The crop is about the player with the ball. + const player = { ...person(0.36, 0.15, 0.66, 0.996), sharpness: 1 }; + const half = { ...person(0, 0.22, 0.14, 0.98), sharpness: 0.7 }; + const inHand = { x0: 0.48, y0: 0.06, x1: 0.55, y1: 0.16, score: 0.9 }; + const onTheFloor = { x0: 0.06, y0: 0.7, x1: 0.12, y1: 0.78, score: 0.8 }; + const c = compose({ width: 6000, height: 4000, people: [player, half], balls: [onTheFloor, inHand] }, null, { context: "reportage", minWidth: 0.4 })!; + expect(c.people).toBe(1); + expect(c.rect.x0).toBeGreaterThan(half.x1 - 0.02); + expect(c.rect.y0).toBeLessThanOrEqual(player.y0); + expect(c.rect.y1).toBeGreaterThan(inHand.y1); + const at = (c.rect.y1 - player.y0) / (player.y1 - player.y0); + expect(at).toBeGreaterThan(0.55); + expect(at).toBeLessThanOrEqual(0.9); + for (const j of [0.53, 0.74, 0.95]) expect(Math.abs(at - j)).toBeGreaterThanOrEqual(0.035); + }); + it("keeps a complete foot when the head is already against the top edge", () => { const p = person(0.4, 0.02, 0.55, 0.984); const c = compose({ ...frame, people: [p], balls: [] }, null, { framing: "tight", context: "reportage" })!; @@ -199,8 +218,11 @@ describe("reportage", () => { ]; const c = compose({ width: 6000, height: 4000, people: [standing, window], balls: [] }, null, { context: "reportage", faces, minWidth: 0.5 })!; expect(c.rect.y0).toBeLessThanOrEqual(standing.y0 + 1e-6); - expect(c.rect.y1).toBeGreaterThan(standing.y1 - 1e-6); expect(c.rect.x0).toBeLessThanOrEqual(standing.x0 + 1e-6); + // Her box runs off the bottom, so the cut is mid-limb, not the sock and not a band through the door. + const at = (c.rect.y1 - standing.y0) / (standing.y1 - standing.y0); + expect(at).toBeGreaterThan(0.55); + expect(at).toBeLessThanOrEqual(0.9); const widthIn = Math.max(0, Math.min(c.rect.x1, window.x1) - Math.max(c.rect.x0, window.x0)) / (window.x1 - window.x0); expect(widthIn < 0.12 || widthIn > 0.9).toBe(true); if (widthIn > 0.9) { diff --git a/docs/photo-editing-rules.md b/docs/photo-editing-rules.md index e06d265..ce11104 100644 --- a/docs/photo-editing-rules.md +++ b/docs/photo-editing-rules.md @@ -33,16 +33,18 @@ Following the AP and NPPA codes: mid-thigh when the foot already leaves the photo. The face is big (a median face of 14% of the frame height) near the top. - **"full"** keeps whole bodies when they fit: Peter's usual crop, about two thirds of the frame. - - **Reportage** always keeps a whole hand and a whole foot. The sports framing on the event is ignored. + - **Reportage** keeps a whole hand and a whole foot when they are in the photo. The sports framing on the event + is ignored. A person the frame has already cut in half is not pulled into the subject: half a body at the + edge is background, and the crop leaves it out whenever it can. - **Never cut:** heads, faces, a hand or a foot that is whole in the photo, hands holding the ball, the ball itself. A person who is in the crop keeps their head, including someone a little less sharp stood in the shade. In reportage the people of a conversation are one subject, so the crop is built around all of them. - **Limbs:** if a body has to be cut, cut mid-limb (mid-thigh, mid-shin, mid-forearm), never at a joint (ankle, knee, hip, wrist, elbow, neck). The engine estimates joints from the body box and avoids crop edges there. - **Feet and hands:** a foot or a hand that is whole in the photo stays in the crop. A foot keeps a little - ground under the sole. Cutting the shoe, the ankle or a hand just to fill the frame is not allowed. A leg is - cut only when the foot already leaves the photo, or the whole body cannot fit: mid-thigh, or (with a ball in - play) mid-shin, never at a joint, and never below the ball. + ground under the sole. Cutting the shoe, the ankle or a hand just to fill the frame is not allowed. When the + foot already leaves the photo, the crop ends mid-shin or mid-thigh. It does not stop on the sock. Never at a + joint, and never through the ball. - **Edges:** no slivers of other in-focus people at the crop edge: either in, or out. Blurred foreground and background people don't count. In reportage this is absolute for anyone still reasonably sharp: the crop keeps their head, and the primary crop keeps their feet when those feet are in the photo. A half or a @@ -51,7 +53,8 @@ Following the AP and NPPA codes: beat a whole figure standing in the same photo. The taller person, closer to the camera, stays in the frame even when someone behind them is a little sharper. - **Stray limbs:** a hand or a foot of someone who is not the subject, and nothing more of them, is cropped out - so the frame stays clean. A whole other person is not that: they stay in or go out, they are not sliced. + so the frame stays clean. So is a person the photo already cuts in half at the frame edge, when they are not + the subject. A whole other person is not that: they stay in or go out, they are not sliced. - **Headroom:** heads a little below the top edge, not touching it, not floating in empty space. - **Vertical placement:** the upper body (chest, faces) a little above the middle of the crop, never the knees. Little or no empty pitch or floor under the feet: when the crop is taller than the subject, the spare height