Dev.to WebDev 🛠 Dev 👁 0 📖 5 min read

Roll-Leveling 478 Face Landmarks Before Measuring Canthal Tilt and fWHR

A small sideways head tilt changes more than how a portrait looks. In raw image coordinates, each eye-corner line acquires some of the head’s roll, so its measured angle is no longer just the eye’s own slope. A width-to-

A small sideways head tilt changes more than how a portrait looks. In raw image coordinates, each eye-corner line acquires some of the head’s roll, so its measured angle is no longer just the eye’s own slope. A width-to-height ratio also depends on the image axes: after the face rotates, horizontal and vertical projections of facial spans shift. This is particularly visible when the measured cheek and upper-face landmarks sit at different positions and the photo contains a few degrees of roll.

The practical fix is modest: estimate the eye-line angle, rotate the landmark set back around a pivot, and measure on the leveled coordinates. That corrects in-plane rotation. It does not turn a selfie into a 3D scan, and it does not make the underlying landmark definitions universal.

1. Put normalized landmarks in pixel space

MediaPipe FaceMesh reports normalized coordinates: x and y are fractions of image width and height. Use the actual analysis image dimensions to put both axes into pixels before calculating angles. On a 1200 × 800 canvas, for example, a normalized displacement of 0.01 means 12 px horizontally but 8 px vertically. Taking atan2 on the unscaled normalized deltas would treat those unequal pixel distances as equal and distort the angle.

function toPixels(rawLandmarks, width, height) {
  return rawLandmarks.map(function (p) {
    return {
      x: p.x * width,
      y: p.y * height,
      z: (p.z || 0) * width
    };
  });
}

Use the dimensions of the canvas actually used for analysis, not a CSS display size. If you resize the source first, the resized canvas dimensions are the coordinate frame. Consistency matters more than whether those pixels match the original file’s dimensions.

2. Estimate roll from eye centers

With the refined iris landmarks available, the production implementation uses pt[468] and pt[473] as eye centers. If the refined iris points are unavailable, it estimates each center as the midpoint of that eye’s inner and outer corners: 33/133 on the image-left eye and 362/263 on the image-right eye. The angle is the center-to-center line’s atan2(dy, dx). The engine rotates by its negative.

The pivot is not simply the eye midpoint. Its x coordinate blends 60% of the two-eye midpoint with 40% of bridge landmark 168; its y coordinate is the eye-midpoint y. Keeping the pivot explicit makes the code reproducible and keeps the rotation near the face’s central axis.

function levelLandmarks(pts) {
  var hasIris = pts.length >= 478;
  var leftEyeC = hasIris
    ? pts[468]
    : { x: (pts[33].x + pts[133].x) / 2,
        y: (pts[33].y + pts[133].y) / 2 };
  var rightEyeC = hasIris
    ? pts[473]
    : { x: (pts[362].x + pts[263].x) / 2,
        y: (pts[362].y + pts[263].y) / 2 };

  var rollRad = Math.atan2(
    rightEyeC.y - leftEyeC.y,
    rightEyeC.x - leftEyeC.x
  );
  var eyeMid = {
    x: (leftEyeC.x + rightEyeC.x) / 2,
    y: (leftEyeC.y + rightEyeC.y) / 2
  };
  var cx = eyeMid.x * 0.6 + pts[168].x * 0.4;
  var cy = eyeMid.y;
  var cosA = Math.cos(-rollRad);
  var sinA = Math.sin(-rollRad);

  var rot = pts.map(function (p) {
    var dx = p.x - cx;
    var dy = p.y - cy;
    return {
      x: dx * cosA - dy * sinA,
      y: dx * sinA + dy * cosA
    };
  });
  return { rot: rot, rollRad: rollRad, cx: cx, cy: cy };
}

All 478 points are transformed by the same 2D rotation. The origin is subtracted before rotation, so rot holds coordinates relative to (cx, cy). Angles and differences do not depend on that translation, but using the same transformed set prevents mixing coordinate frames.

3. Measure canthal tilt and fWHR

Image y increases downward. For the image-left eye, production measures from outer corner 33 to inner corner 133; the y delta is inner minus outer and the x delta is inner minus outer. For the image-right eye, it measures from outer corner 263 to inner corner 362, again reversing x direction so both eye spans use the same sign convention. The resulting degrees are averaged before rounding the displayed value.

function measureCanthalTilt(rot) {
  var toDegrees = 180 / Math.PI;
  var leftTilt = Math.atan2(
    rot[133].y - rot[33].y,
    rot[133].x - rot[33].x
  ) * toDegrees;
  var rightTilt = Math.atan2(
    rot[362].y - rot[263].y,
    rot[263].x - rot[362].x
  ) * toDegrees;
  var averageUnrounded = (leftTilt + rightTilt) / 2;
  return { left: leftTilt, right: rightTilt, average: averageUnrounded };
}

For a leveled eye with 30 px horizontal span and an outer corner 3 px higher, the downward-positive y convention gives an inner-minus-outer delta of +3 px. atan2(3, 30) = +5.7 deg (rounded to one decimal). In a two-eye result, keep full precision for both angles, average those values, and round only for display.

The fWHR formula here is specific and simple: horizontal cheek span between landmarks 234 and 454 divided by vertical distance from glabella landmark 9 to upper-lip landmark 0. Each measured dimension has a 1 px floor. The numerator uses an absolute x difference; the denominator follows the production y order.

function measureFwhr(rot) {
  var cheekW = Math.abs(rot[454].x - rot[234].x);
  var safeW = Math.max(cheekW, 1);
  var upperFaceH = Math.max(rot[0].y - rot[9].y, 1);
  return safeW / upperFaceH;
}

If the leveled cheek x coordinates are −142 and 143 px, their span is 285 px. With landmark 9 at y = −18 and landmark 0 at y = 132, the height is 150 px, so 285 / 150 = 1.90. If the same width is divided by a 160 px height, 285 / 160 = 1.78 after rounding to two decimals. Endpoint definitions matter: changing the vertical landmark changes the ratio, even when the photo and cheek width stay fixed.

4. Know what a 2D correction cannot fix

Roll leveling fixes in-plane rotation only. Yaw turns one cheek away and changes its projected width; pitch changes which vertical surfaces and landmarks are visible; neither is undone by rotating coordinates around the image plane. A close, wide-angle selfie adds perspective distortion because nearer facial features occupy more of the view than farther ones. Those effects can remain even when the eye line is perfectly level.

For a more repeatable capture, put the camera at eye level, face it directly, and step back to about 1.2–1.5 m. Use a timer or stand if needed, then crop the image rather than holding a wide-angle phone close to the face. Match distance, framing, and expression when comparing results. A second turned-head image is useful as a demonstration of the boundary: the roll correction can make the eye line horizontal, but the near/far cheek projection still differs.

The numbers also depend on the landmark model’s placement and the chosen endpoints. They describe this geometry in a particular image; they are not direct measurements of a 3D skull or a substitute for consistent capture conditions.

5. Audit the browser’s network activity

A client-side image workflow is easier to trust when someone can inspect it. Open DevTools, select Network, enable Preserve log and Disable cache, choose All, reload, then process a test image and export the result. Inspect request URLs, query strings, and payloads. The log may show page assets, model files fetched from a CDN, fonts, and anonymous telemetry events. That is different from sending the image itself: for this workflow, image pixels, landmark coordinates, and calculated scores should remain in the browser.

Do not infer privacy from a “local processing” label alone. Repeat the check with a real test image, inspect requests during scan and export, and distinguish ordinary asset downloads or telemetry from photo or measurement data. The browser’s Network panel gives a practical, self-auditable view of what leaves the tab.

For a hands-on reference, the Canthal Tilt Test and fWHR Calculator demonstrate these measurements in the browser.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.