extractEmbedding method

Future<List<double>> extractEmbedding(
  1. Image image,
  2. Face face
)

Implementation

Future<List<double>> extractEmbedding(img.Image image, Face face) async {
  // Cropping, resizing and preprocessing image for model
  int x = face.boundingBox.left.toInt().clamp(0, image.width - 1);
  int y = face.boundingBox.top.toInt().clamp(0, image.height - 1);
  int w = face.boundingBox.width.toInt().clamp(1, image.width - x);
  int h = face.boundingBox.height.toInt().clamp(1, image.height - y);

  final img.Image croppedFace = img.copyCrop(image, x: x, y: y, width: w, height: h);
  final img.Image resizedFace = img.copyResize(croppedFace, width: faceNetImageSize, height: faceNetImageSize);

  final Float32List input = Float32List(faceNetImageSize * faceNetImageSize * 3);
  int index = 0;

  for (int y = 0; y < faceNetImageSize; y++) {
    for (int x = 0; x < faceNetImageSize; x++) {
      final pixel = resizedFace.getPixel(x, y);
      input[index++] = ((pixel.r - 127.5) / 127.5);
      input[index++] = ((pixel.g - 127.5) / 127.5);
      input[index++] = ((pixel.b - 127.5) / 127.5);
    }
  }

  final List<List<double>> output = List.generate(1, (_) => List.filled(faceNetEmbeddingSize, 0.0));
  interpreter.run(input.reshape([1, faceNetImageSize, faceNetImageSize, 3]), output);

  return output[0];
}