computeEmbeddingAlignment function

AlignedRoi computeEmbeddingAlignment({
  1. required Point leftEye,
  2. required Point rightEye,
})

Computes alignment parameters for extracting a face crop suitable for embedding.

This function calculates the optimal crop region for face recognition based on eye positions. The alignment ensures:

  • Eyes are horizontally aligned
  • Face is centered in the crop
  • Adequate padding around the face

The leftEye and rightEye parameters are the eye center positions in absolute pixel coordinates.

Returns AlignedRoi with center, size, and rotation parameters.

Implementation

AlignedRoi computeEmbeddingAlignment({
  required Point leftEye,
  required Point rightEye,
}) {
  final double dx = rightEye.x - leftEye.x;
  final double dy = rightEye.y - leftEye.y;
  final double theta = math.atan2(dy, dx);

  final double eyeDist = math.sqrt(dx * dx + dy * dy);

  final double size = eyeDist * 2.5;

  final double eyeCx = (leftEye.x + rightEye.x) * 0.5;
  final double eyeCy = (leftEye.y + rightEye.y) * 0.5;

  final double ct = math.cos(theta);
  final double st = math.sin(theta);
  final double offsetY = size * 0.15;
  final double cx = eyeCx - offsetY * st;
  final double cy = eyeCy + offsetY * ct;

  return AlignedRoi(cx, cy, size, theta);
}