getImagePrediction method

Future<String> getImagePrediction(
  1. File image,
  2. int width,
  3. int height,
  4. String labelPath, {
  5. List<double> mean = TORCHVISION_NORM_MEAN_RGB,
  6. List<double> std = TORCHVISION_NORM_STD_RGB,
})

predicts image and returns the supposed label belonging to it

Implementation

Future<String> getImagePrediction(
    File image, int width, int height, String labelPath,
    {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");

  List<String> labels = await _getLabels(labelPath);
  List byteArray = image.readAsBytesSync();
  final List? prediction = await _channel.invokeListMethod("predictImage", {
    "index": _index,
    "image": byteArray,
    "width": width,
    "height": height,
    "mean": mean,
    "std": 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];
}