Shut eyes and backs no longer win a burst when an open-eyed face exists. Develop/crop runs only on the picked frames (not the whole sequence). Bursts without EXIF group by ingest time. Level falls back to LightCraft when stadium lines disagree.
190 lines
7.7 KiB
TypeScript
190 lines
7.7 KiB
TypeScript
import { MIN_SHARPNESS } from "../faces/math.js";
|
|
import type { Db } from "../db.js";
|
|
import { eyeOpennessAt } from "../composition/eyes.js";
|
|
import { groupBursts } from "./bursts.js";
|
|
|
|
/**
|
|
* Which frame of a sequence to keep (docs/photo-editing-rules.md §3). A player from behind loses to a frame
|
|
* where the face is visible, and shut eyes lose to open ones, when the difference is clear. If every frame is
|
|
* a back, or the eyes cannot be told apart, nothing changes.
|
|
*
|
|
* Open eyes always beat shut ones when both are measured in the same burst — even on a shot or a grimace.
|
|
* A clear face always beats a back when the burst has both.
|
|
*/
|
|
export interface SequenceLook {
|
|
/** 1: a face on the subject. 0: the subject was seen and has no face (a back). null: not analysed yet. */
|
|
facing: 0 | 1 | null;
|
|
/** Higher means the eyes look more open. null: not measured. */
|
|
eyes: number | null;
|
|
}
|
|
|
|
/** Below this, eyes count as shut when another frame in the burst is clearly more open. */
|
|
const EYES_SHUT = 0.45;
|
|
/** At or above this, eyes count as open enough to prefer over a blink. */
|
|
const EYES_OPEN = 0.55;
|
|
/** Minimum gap between best and this frame before a blink penalty applies. */
|
|
const EYES_GAP = 0.08;
|
|
|
|
export function sequenceWeight(frames: readonly SequenceLook[], reportage = false): number[] {
|
|
const front = frames.some((f) => f.facing === 1);
|
|
const known = frames.map((f) => f.eyes).filter((v): v is number => v !== null);
|
|
const best = known.length ? Math.max(...known) : 0;
|
|
const hasOpen = known.some((v) => v >= EYES_OPEN);
|
|
// A blink loses hard whenever the burst has a clearer open-eye frame. Match and reportage both refuse it.
|
|
const shut = reportage ? 0.05 : 0.08;
|
|
return frames.map((f) => {
|
|
let w = 1;
|
|
if (front && f.facing === 0) w *= 0.05;
|
|
if (f.eyes === null || !known.length) return w;
|
|
const gap = best - f.eyes;
|
|
const clearlyShut = hasOpen && f.eyes < EYES_SHUT && gap >= EYES_GAP;
|
|
const relativelyShut = gap >= EYES_GAP && f.eyes < best * 0.85;
|
|
if (clearlyShut || relativelyShut) w *= shut;
|
|
return w;
|
|
});
|
|
}
|
|
|
|
interface Box {
|
|
x0: number;
|
|
y0: number;
|
|
x1: number;
|
|
y1: number;
|
|
}
|
|
|
|
export interface LookPhoto {
|
|
id: string;
|
|
capturedAt: Date | null;
|
|
createdAt?: Date | null;
|
|
cameraSerial: string | null;
|
|
cameraModel: string | null;
|
|
uploaderId: string | null;
|
|
facesStatus: string;
|
|
organizationId: string;
|
|
sha256: string;
|
|
autopilot: unknown;
|
|
flag: string;
|
|
}
|
|
|
|
interface FaceBox extends Box {
|
|
photoId: string;
|
|
sharpness: number | null;
|
|
}
|
|
|
|
const area = (b: Box) => Math.max(0, b.x1 - b.x0) * Math.max(0, b.y1 - b.y0);
|
|
|
|
function overlap(a: Box, b: Box): number {
|
|
const w = Math.max(0, Math.min(a.x1, b.x1) - Math.max(a.x0, b.x0));
|
|
const h = Math.max(0, Math.min(a.y1, b.y1) - Math.max(a.y0, b.y0));
|
|
return w * h;
|
|
}
|
|
|
|
function subjectOf(autopilot: unknown): Box | null {
|
|
const s = (autopilot as { subject?: Box } | null)?.subject;
|
|
if (!s || typeof s.x0 !== "number" || typeof s.y0 !== "number" || typeof s.x1 !== "number" || typeof s.y1 !== "number") return null;
|
|
return s;
|
|
}
|
|
|
|
function facingOf(subject: Box | null, faces: FaceBox[], done: boolean): 0 | 1 | null {
|
|
if (!done) return null;
|
|
const clear = faces.filter((f) => f.sharpness == null || f.sharpness >= MIN_SHARPNESS);
|
|
const head = subject ? { x0: subject.x0, y0: subject.y0, x1: subject.x1, y1: subject.y0 + 0.5 * (subject.y1 - subject.y0) } : null;
|
|
const onSubject = head ? clear.filter((f) => overlap(f, head) > 0.25 * area(f)) : [];
|
|
if (onSubject.length) return 1;
|
|
if (subject) return 0;
|
|
if (clear.some((f) => f.y1 - f.y0 > 0.03)) return 1;
|
|
return 0;
|
|
}
|
|
|
|
/** Every clear face on the subject. Reportage uses all of them: one blink in the group is enough. */
|
|
function facesOnSubject(subject: Box | null, faces: FaceBox[]): FaceBox[] {
|
|
const clear = faces.filter((f) => f.sharpness == null || f.sharpness >= MIN_SHARPNESS);
|
|
const head = subject ? { x0: subject.x0, y0: subject.y0, x1: subject.x1, y1: subject.y0 + 0.5 * (subject.y1 - subject.y0) } : null;
|
|
if (!head) return [];
|
|
return clear.filter((f) => overlap(f, head) > 0.25 * area(f));
|
|
}
|
|
|
|
function faceForEyes(subject: Box | null, faces: FaceBox[]): FaceBox | null {
|
|
const clear = faces.filter((f) => f.sharpness == null || f.sharpness >= MIN_SHARPNESS);
|
|
if (!clear.length) return null;
|
|
const head = subject ? { x0: subject.x0, y0: subject.y0, x1: subject.x1, y1: subject.y0 + 0.5 * (subject.y1 - subject.y0) } : null;
|
|
const on = head ? clear.filter((f) => overlap(f, head) > 0.25 * area(f)) : clear;
|
|
const pool = on.length ? on : clear;
|
|
return pool.reduce((a, b) => ((b.sharpness ?? 0) > (a.sharpness ?? 0) ? b : a));
|
|
}
|
|
|
|
async function mapPool<T>(items: T[], n: number, fn: (item: T) => Promise<void>): Promise<void> {
|
|
let i = 0;
|
|
const workers = Array.from({ length: Math.min(n, items.length) }, async () => {
|
|
while (i < items.length) {
|
|
const item = items[i]!;
|
|
i += 1;
|
|
await fn(item);
|
|
}
|
|
});
|
|
await Promise.all(workers);
|
|
}
|
|
|
|
/**
|
|
* A multiplier per photo. Backs and shut eyes lose inside their own sequence only. `preview` reads the upright
|
|
* preview so the eyes can be compared; without it only the facing (from the stored faces) counts.
|
|
*/
|
|
export async function sequenceWeights(db: Db, photos: readonly LookPhoto[], gapMs: number, preview: ((photo: LookPhoto) => string | null) | null, reportage = false): Promise<Map<string, number>> {
|
|
const out = new Map<string, number>();
|
|
if (!photos.length) return out;
|
|
const faces = await db.face.findMany({
|
|
where: { photoId: { in: photos.map((p) => p.id) } },
|
|
select: { photoId: true, x0: true, y0: true, x1: true, y1: true, sharpness: true },
|
|
});
|
|
const byPhoto = new Map<string, FaceBox[]>();
|
|
for (const f of faces) {
|
|
const list = byPhoto.get(f.photoId) ?? [];
|
|
list.push(f);
|
|
byPhoto.set(f.photoId, list);
|
|
}
|
|
const frames = photos.map((p) => ({
|
|
...p,
|
|
camera: p.cameraSerial ?? p.cameraModel ?? p.uploaderId ?? "?",
|
|
score: 0,
|
|
// USB / FTP without EXIF: still group by ingest time so a burst is not ten singleton picks.
|
|
capturedAt: p.capturedAt ?? p.createdAt ?? null,
|
|
}));
|
|
const jobs: { photo: LookPhoto; face: FaceBox }[] = [];
|
|
const looks = new Map<string, SequenceLook>();
|
|
for (const burst of groupBursts(frames, gapMs)) {
|
|
if (burst.length < 2) continue;
|
|
for (const frame of burst) {
|
|
const own = byPhoto.get(frame.id) ?? [];
|
|
const subject = subjectOf(frame.autopilot);
|
|
const facing = facingOf(subject, own, frame.facesStatus === "DONE");
|
|
looks.set(frame.id, { facing, eyes: null });
|
|
if (!preview || facing !== 1) continue;
|
|
// Reportage: the most closed face in the group. A match: the sharpest face on the subject.
|
|
const chosen = reportage ? facesOnSubject(subject, own) : [faceForEyes(subject, own)].filter((f): f is FaceBox => !!f);
|
|
for (const face of chosen) jobs.push({ photo: frame, face });
|
|
}
|
|
}
|
|
const eyes = new Map<string, number | null>();
|
|
if (preview && jobs.length) {
|
|
const measured = new Map<string, number[]>();
|
|
await mapPool(jobs, 4, async (job) => {
|
|
const file = preview(job.photo);
|
|
const value = file ? await eyeOpennessAt(file, job.face) : null;
|
|
if (value === null) return;
|
|
const list = measured.get(job.photo.id) ?? [];
|
|
list.push(value);
|
|
measured.set(job.photo.id, list);
|
|
});
|
|
for (const [id, values] of measured) eyes.set(id, Math.min(...values));
|
|
}
|
|
for (const burst of groupBursts(frames, gapMs)) {
|
|
if (burst.length < 2) continue;
|
|
const rows = burst.map((frame) => {
|
|
const look = looks.get(frame.id) ?? { facing: null, eyes: null };
|
|
return { facing: look.facing, eyes: eyes.get(frame.id) ?? null };
|
|
});
|
|
const weights = sequenceWeight(rows, reportage);
|
|
burst.forEach((frame, i) => out.set(frame.id, weights[i] ?? 1));
|
|
}
|
|
return out;
|
|
}
|