Cotton code
83 lines · 3.9 KB · MarkdownRaw
# Face crop and alignment: the core's contract and test vectorsThe face crop and alignment move from the phones into the Rust core (coordinator, 27 Nov; DECISIONS §5: "the face cropand alignment are done in the Rust core"), so his iPhone and an Android phone hand the face model the same 112×112pixels. Today it is `AlbumsAlign` in `apps/cotton/Sources/CottonUI/Albums/AlbumsAnalysis.swift`; it is deleted once thecore answers.## The contract**Input.** An RGB picture (8 bits a channel, row by row from the top) and five points in **pixel-index coordinates**:the centre of the top-left pixel is (0, 0), x to the right, y down. The order is:1. the eye on the picture's left;2. the other eye;3. the nose tip;4. the mouth corner on the picture's left;5. the other corner.Vision gives continuous coordinates (pixel centres at i + 0.5), so the iPhone subtracts 0.5 before calling.**Template.** ArcFace's five points for a 112×112 face, in the same order:| point | x | y ||---|---|---|| eye (left) | 38.2946 | 51.6963 || eye (right) | 73.5318 | 51.5014 || nose tip | 56.0252 | 71.7366 || mouth (left) | 41.5493 | 92.3655 || mouth (right) | 70.7299 | 92.2041 |**Transform.** The similarity transform (scale, rotation, translation; no reflection, no shear) that best maps the fivepoints onto the template, by least squares (Umeyama 1991 without reflection), with `ms` and `md` the means of thesource points and the template points:```dst = [[a, -b], [b, a]] · src + [tx, ty]a  = Σ(sx·dx + sy·dy) / Σ(sx² + sy²)      (s = src − mean(src), d = template − mean(template))b  = Σ(sx·dy − sy·dx) / Σ(sx² + sy²)tx = md.x − (a·ms.x − b·ms.y)ty = md.y − (b·ms.x + a·ms.y)```**Output.** 112×112 RGB. Output pixel (u, v), integers, takes the source at M⁻¹·(u, v):- bilinear interpolation over the four nearest pixel centres;- a neighbour outside the picture counts as black (0, 0, 0);- each channel is rounded to the nearest integer (halves away from zero) and clamped to 0–255.**Refusal.** When the five points have no spread (all the same point), answer an error, never a crop.**Model input.** Then the model's own scaling. For HyperFace-50k that is (x − 127.5) / 127.5, blue channel first. Thisstays with the phone's model call, or the core's if the model moves too.## The vectors`vectors.json` holds six cases and one refusal. `scripts/albums-align-vectors.py --out assets/design/albums/align`regenerates them, with the same contract written in Python.| case | tests ||---|---|| identity | points already on the template: the crop is the picture, byte for byte || larger | a face twice the template's size || turned | turned 20° || small-turned | a small face (about 16 px between the eyes), turned −35° || jittered | points off the template by noise: least squares, not exact || edge | a face at the corner: the crop reaches outside the picture, which must be black || no-spread | all five points the same: an error |Each case gives:- the picture's size and its source PNG, with the SHA-256 of its raw RGB bytes;- the five points;- the expected `a, b, tx, ty`;- the expected crop PNG, with the SHA-256 of its raw RGB bytes.Each source has a white dot at every point, so in a right crop the dots sit on the template. The edge case's first dotis off the picture, so its crop is black there.**Tolerance:** `a`, `b`, `tx` and `ty` within 1e-4, and every crop byte within 1 of the expected crop. A bit-exact matchof the SHA-256 is not required, since floating point may differ by one in a rounding.**The phone today, against the vectors:** `AlbumsAlign` (CoreGraphics) matches identity byte for byte. On theothers it averages 1.1–2.5 per byte off, at most 84 at a dot's edge: the geometry agrees, but CoreGraphics' resamplingis not plain bilinear. An Android phone would differ again, which is why the core does it.Nothing in these pictures is a person. They are drawn from numbers, so the vectors carry no licence.