Files
photoreport/apps/api/test/jersey-crop.test.ts
spark 467df72a59
ci / rust (push) Successful in 8m24s
ci / typescript (push) Successful in 11m44s
Faces: face, shirt number and kit colour together
A weak or ambiguous face match is settled by the number read on the shirt; a sure face whose shirt clearly
reads another number is suggested as less sure; when both teams have a player with the number, the kit colour
(median of the chest, per face) against each team's confirmed players decides, or nothing is guessed.

Claude-Session: https://claude.ai/code/session_01HT6iXLYUUp9pJz5Kza9xCW
2026-10-13 21:53:42 +01:00

52 lines
2.3 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { numberBoxes, shortsBox, torsoBox } from "../src/jersey/crop.js";
// A player standing in a 3:2 frame: face 4% wide and 6% tall, high in the picture.
const face = { x0: 0.48, y0: 0.1, x1: 0.52, y1: 0.16 };
const W = 6000;
const H = 4000;
describe("shirt-number crops", () => {
it("takes the chest for handball and the shorts for football, the shorts lower and tighter", () => {
const chest = torsoBox(face, W, H)!;
const shorts = shortsBox(face, W, H)!;
expect(chest.y0).toBeGreaterThan(face.y1);
// The shorts start around the waist, under most of the chest crop, and reach mid-thigh.
expect(shorts.y0).toBeGreaterThan(chest.y0 + (chest.y1 - chest.y0) * 0.6);
expect(shorts.y1).toBeCloseTo(face.y1 + 6.8 * (face.y1 - face.y0), 5);
expect(shorts.y1 - shorts.y0).toBeLessThan(chest.y1 - chest.y0);
expect(numberBoxes(face, W, H).map((b) => b.region)).toEqual(["CHEST", "SHORTS"]);
});
it("leaves out the shorts when the frame cuts them (a close-up from the waist up)", () => {
const low = { x0: 0.45, y0: 0.4, x1: 0.55, y1: 0.55 };
expect(shortsBox(low, W, H)).toBeNull();
expect(numberBoxes(low, W, H).map((b) => b.region)).toEqual(["CHEST"]);
});
});
describe("labels", () => {
it("reads shirt numbers and handball officials' cards the same way", async () => {
const { normLabel } = await import("../src/jersey/service.js");
expect(normLabel("07")).toBe("7");
expect(normLabel(" 10 ")).toBe("10");
expect(normLabel("b")).toBe("B");
expect(normLabel("Oficial B")).toBe("B");
expect(normLabel("G")).toBeNull();
expect(normLabel("1234")).toBeNull();
expect(normLabel(null)).toBeNull();
});
});
describe("kit colour", () => {
it("takes the median colour in the middle of the chest, so a logo or stripe pulls it little", async () => {
const { kitColour } = await import("../src/jersey/crop.js");
const width = 100;
const height = 100;
const data = Buffer.alloc(width * height * 3);
for (let i = 0; i < width * height; i++) data.set([200, 20, 20], i * 3); // a red shirt
for (let y = 40; y < 46; y++) for (let x = 45; x < 55; x++) data.set([255, 255, 255], (y * width + x) * 3); // a white logo
expect(kitColour({ data, width, height, channels: 3 }, { x0: 0.2, y0: 0.2, x1: 0.8, y1: 0.9 })).toEqual([200, 20, 20]);
});
});