entryPoint static method

void entryPoint(
  1. SendPort sendPort
)

Implementation

static void entryPoint(SendPort sendPort) async {
  final port = ReceivePort();
  sendPort.send(port.sendPort);

  await for (final InferenceModel isolateModel in port) {
    image_lib.Image? img;
    img = isolateModel.image;

    // resize original image to match model shape.
    image_lib.Image imageInput = image_lib.copyResize(
      img,
      width: isolateModel.inputShape[1],
      height: isolateModel.inputShape[2],
    );

    final imageMatrix = List.generate(
      imageInput.height,
      (y) => List.generate(
        imageInput.width,
        (x) {
          final pixel = imageInput.getPixel(x, y);
          return [pixel.r, pixel.g, pixel.b];
        },
      ),
    );

    // Set tensor input [1, 224, 224, 3]
    final input = [imageMatrix];
    // Set tensor output [1, 1001]
    final output = [List<int>.filled(isolateModel.outputShape[1], 0)];
    // // Run inference
    Interpreter interpreter =
        Interpreter.fromAddress(isolateModel.interpreterAddress);
    interpreter.run(input, output);
    // Get first output tensor
    final result = output.first;
    int maxScore = result.reduce((a, b) => a + b);
    // Set classification map {label: points}
    var classification = <String, double>{};
    for (var i = 0; i < result.length; i++) {
      if (result[i] != 0) {
        // Set label: points
        classification[isolateModel.labels[i]] =
            result[i].toDouble() / maxScore.toDouble();
      }
    }
    isolateModel.responsePort.send(classification);
  }
}