|
|
|
@@ -7,10 +7,11 @@
|
|
|
|
|
* - With a ball near the subject, the ball and the sharp players contesting it come along.
|
|
|
|
|
* - The crop is usually two thirds of the frame (the same shape), the subject in the middle across, the upper
|
|
|
|
|
* 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). Legs may be cut; heads and the ball may not.
|
|
|
|
|
* 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.
|
|
|
|
|
* - "tight" framing (the agencies' handball look: kolektiff images, measured on 600 photos of 2025-26): the
|
|
|
|
|
* subject fills the height, cut mid-shin or mid-thigh, the face big (about a seventh of the frame) near the top.
|
|
|
|
|
* subject fills the height and the face is big (about a seventh of the frame) near the top. A foot that is
|
|
|
|
|
* whole in the photo stays in; mid-shin or mid-thigh only when the frame does not finish the body.
|
|
|
|
|
* "full" keeps whole bodies when they fit (Peter's usual two thirds of the frame).
|
|
|
|
|
*
|
|
|
|
|
* Boxes are fractions of the upright photo (0..1, y down).
|
|
|
|
@@ -81,12 +82,22 @@ export const TUNING = {
|
|
|
|
|
*/
|
|
|
|
|
const JOINTS = [0.17, 0.53, 0.74, 0.95];
|
|
|
|
|
const JOINT_BAND = 0.035;
|
|
|
|
|
/** Below this, the foot is whole in the photo (the box does not run off the bottom edge). */
|
|
|
|
|
const FEET_IN_FRAME = 0.985;
|
|
|
|
|
/** A bottom edge past this fraction of the body is in the foot, not mid-shin. */
|
|
|
|
|
const FOOT_ZONE = 0.9;
|
|
|
|
|
/** Mid-shin and mid-thigh: between the joints, never on the ankle, the knee or the hip. */
|
|
|
|
|
const MID_SHIN = 0.86;
|
|
|
|
|
const MID_THIGH = 0.64;
|
|
|
|
|
/** Ground kept under a sole that stays in the crop, as a fraction of the body height. */
|
|
|
|
|
const SOLE_PAD = 0.03;
|
|
|
|
|
const SOLE_LIMIT = 0.995;
|
|
|
|
|
|
|
|
|
|
/** Tight framing: the subject's tallest body about this many crop heights (a little cut at the bottom). */
|
|
|
|
|
export const TIGHT = { fillAt: 1.05, wFill: 2.2, chestAt: 0.34, headMax: 0.2 };
|
|
|
|
|
|
|
|
|
|
export interface ComposeOptions {
|
|
|
|
|
/** "full": whole bodies when they fit (default); "tight": the agencies' close framing, legs cut mid-limb. */
|
|
|
|
|
/** "full": whole bodies when they fit (default); "tight": the agencies' close framing. A complete foot stays in. */
|
|
|
|
|
framing?: "full" | "tight";
|
|
|
|
|
/** Narrowest crop allowed, as a fraction of the photo's width (from the delivery's minimum resolution). */
|
|
|
|
|
minWidth?: number;
|
|
|
|
@@ -176,19 +187,29 @@ export function compose(d: Detections, aspect: number | null = null, opts: Compo
|
|
|
|
|
const others = d.people.filter((p) => p.score >= MIN_PERSON && !inSubject.includes(p));
|
|
|
|
|
const headroom = 0.04 * tallest;
|
|
|
|
|
const minWidth = opts.minWidth ?? 0;
|
|
|
|
|
const feetIn = s.y1 < FEET_IN_FRAME;
|
|
|
|
|
// A little ground under a sole that is whole in the photo, so the crop line is not the shoe.
|
|
|
|
|
const sole = feetIn ? Math.min(SOLE_LIMIT, s.y1 + Math.min(SOLE_PAD * tallest, Math.max(0, SOLE_LIMIT - s.y1))) : s.y1;
|
|
|
|
|
if (opts.framing === "tight") {
|
|
|
|
|
// 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));
|
|
|
|
|
// Mid-shin, mid-thigh, or whole: the cost (fill, head, joints) chooses; the waist only when nothing else fits.
|
|
|
|
|
const tries = [keep(s.y0 + 0.86 * tallest), keep(s.y0 + 0.64 * tallest), s.y1]
|
|
|
|
|
// 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.
|
|
|
|
|
if (feetIn) {
|
|
|
|
|
const whole = frame(s, ratio, sole, headroom, 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 + 0.55 * tallest), headroom, tallest, others, inSubject, reason, minWidth, true);
|
|
|
|
|
return frame(s, ratio, keep(s.y0 + MID_THIGH * tallest), headroom, tallest, others, inSubject, reason, minWidth, true);
|
|
|
|
|
}
|
|
|
|
|
// Whole bodies, feet included; only when that can't fit, down to the waist.
|
|
|
|
|
return frame(s, ratio, s.y1, headroom, tallest, others, inSubject, reason, minWidth) ?? frame(s, ratio, s.y0 + 0.55 * tallest, headroom, tallest, others, inSubject, reason, minWidth);
|
|
|
|
|
// Whole bodies, feet included; only when that can't fit, mid-thigh (between hip and knee), never the foot.
|
|
|
|
|
return frame(s, ratio, sole, headroom, tallest, others, inSubject, reason, minWidth)
|
|
|
|
|
?? frame(s, ratio, s.y1, headroom, tallest, others, inSubject, reason, minWidth)
|
|
|
|
|
?? frame(s, ratio, s.y0 + MID_THIGH * tallest, headroom, tallest, others, inSubject, reason, minWidth);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Recommended narrowest crop for a photo `width` px wide when deliveries need `minLongEdge` px (and never below a third). */
|
|
|
|
@@ -219,6 +240,7 @@ function frame(s: Box, ratio: number, mustBottom: number, headroom: number, tall
|
|
|
|
|
const y0 = Math.min(Math.max(0, top - (j / 20) * spare), 1 - ch);
|
|
|
|
|
const r = { x0, y0, x1: x0 + cw, y1: y0 + ch };
|
|
|
|
|
if (r.x0 > s.x0 + 1e-6 || r.x1 < s.x1 - 1e-6 || r.y0 > Math.max(0, s.y0 - headroom) + 1e-6 || r.y1 < mustBottom - 1e-6) continue;
|
|
|
|
|
if (cutsFoot(r, inSubject)) continue;
|
|
|
|
|
const c = cost(r, s, others, ch, tallest, mainSharp, ratio, inSubject, tight) + jointCost(r, inSubject, others, mainSharp);
|
|
|
|
|
if (!best || c < best.cost) best = { rect: r, cost: c };
|
|
|
|
|
}
|
|
|
|
@@ -265,6 +287,19 @@ function cost(r: Box, s: Box, others: Detection[], ch: number, tallest: number,
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A bottom edge through the foot (below mid-shin, above the sole). The sole itself, or anything below it,
|
|
|
|
|
* keeps the whole foot. A body that already runs off the photo can still be cut mid-shin, never through the shoe.
|
|
|
|
|
*/
|
|
|
|
|
function cutsFoot(r: Box, people: Detection[]): boolean {
|
|
|
|
|
for (const p of people) {
|
|
|
|
|
if (r.x1 <= p.x0 || r.x0 >= p.x1) continue;
|
|
|
|
|
if (r.y1 >= p.y1 - 1e-6) continue;
|
|
|
|
|
if ((r.y1 - p.y0) / h(p) > FOOT_ZONE) return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Crop edges that cross a body at a joint (ankle, knee, hip, neck): the "amputation" look. */
|
|
|
|
|
function jointCost(r: Box, subject: Detection[], others: Detection[], mainSharp: number): number {
|
|
|
|
|
let c = 0;
|
|
|
|
|