Cotton code

Face crop and alignment: the core's contract and test vectors

The face crop and alignment move from the phones into the Rust core (coordinator, 27 Nov; DECISIONS §5: "the face crop and alignment are done in the Rust core"), so his iPhone and an Android phone hand the face model the same 112×112 pixels. Today it is AlbumsAlign in apps/cotton/Sources/CottonUI/Albums/AlbumsAnalysis.swift; it is deleted once the core 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:

pointxy
eye (left)38.294651.6963
eye (right)73.531851.5014
nose tip56.025271.7366
mouth (left)41.549392.3655
mouth (right)70.729992.2041

Transform. The similarity transform (scale, rotation, translation; no reflection, no shear) that best maps the five points onto the template, by least squares (Umeyama 1991 without reflection), with ms and md the means of the source 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):

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. This stays 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.

casetests
identitypoints already on the template: the crop is the picture, byte for byte
largera face twice the template's size
turnedturned 20°
small-turneda small face (about 16 px between the eyes), turned −35°
jitteredpoints off the template by noise: least squares, not exact
edgea face at the corner: the crop reaches outside the picture, which must be black
no-spreadall five points the same: an error

Each case gives:

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 dot is 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 match of 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 the others it averages 1.1–2.5 per byte off, at most 84 at a dot's edge: the geometry agrees, but CoreGraphics' resampling is 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.