getImagePredictionFromBytesList method

Future<String> getImagePredictionFromBytesList(
  1. List<Uint8List> imageAsBytesList,
  2. int imageWidth,
  3. int imageHeight, {
  4. List<double> mean = TORCHVISION_NORM_MEAN_RGB,
  5. List<double> std = TORCHVISION_NORM_STD_RGB,
})

Runs batch image classification and returns predicted labels.

Processes multiple images in a single batch for improved performance. Returns the label with the highest confidence for each image.

@param imageAsBytesList List of raw image bytes @param imageWidth The width to resize images to @param imageHeight The height to resize images to @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> getImagePredictionFromBytesList(
    List<Uint8List> imageAsBytesList, int imageWidth, int imageHeight,
    {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, null, imageAsBytesList, imageWidth, imageHeight, 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];
}