getImagePrediction method

Future<String> getImagePrediction(
  1. Uint8List imageAsBytes, {
  2. List<double> mean = TORCHVISION_NORM_MEAN_RGB,
  3. List<double> std = TORCHVISION_NORM_STD_RGB,
})

Runs image classification and returns the predicted label.

Processes the image through the model and returns the label with the highest confidence score.

@param imageAsBytes The raw image bytes @param mean Optional normalization mean values (default: ImageNet means) @param std Optional normalization std values (default: ImageNet stds) @return A Future that completes with the predicted label string

Implementation

Future<String> getImagePrediction(Uint8List imageAsBytes,
    {List<double> mean = TORCHVISION_NORM_MEAN_RGB,
    List<double> std = TORCHVISION_NORM_STD_RGB}) async {
  // Assert mean std
  assert(mean.length == 3, "mean should have size of 3");
  assert(std.length == 3, "std should have size of 3");

  final List<double?> prediction = await ModelApi().getImagePredictionList(
      _index, imageAsBytes, null, null, null, mean, std);

  double maxScore = double.negativeInfinity;
  int maxScoreIndex = -1;
  for (int i = 0; i < prediction.length; i++) {
    if (prediction[i]! > maxScore) {
      maxScore = prediction[i]!;
      maxScoreIndex = i;
    }
  }

  return labels[maxScoreIndex];
}