【问题标题】:Passing image to tflite model将图像传递给 tflite 模型
【发布时间】:2020-03-13 12:24:02
【问题描述】:

我在 Flutter 中加载了 tensor-flow Lite 模型,但在将图像传递给模型进行预测时遇到了一些问题。预测方法允许任何对象通过,但 Flutter 正在将图像作为文件读取,因为我正在使用 Image Picker 类,我找不到将文件类型转换为图像的方法,以便我可以将其转换为 (28,28,1)这是模型所要求的。

谢谢,我们将不胜感激。

Uint8List imageToByteListFloat32(
       img.Image image, int inputSize, double mean, double std) {
    var convertedBytes = Float32List(1 * inputSize * inputSize * 3);
    var buffer = Float32List.view(convertedBytes.buffer);
    int pixelIndex = 0;
    for (var i = 0; i < inputSize; i++) {
      for (var j = 0; j < inputSize; j++) {
        var pixel = image.getPixel(j, i);
        buffer[pixelIndex++] = (img.getRed(pixel) - mean) / std;
        buffer[pixelIndex++] = (img.getGreen(pixel) - mean) / std;
        buffer[pixelIndex++] = (img.getBlue(pixel) - mean) / std;
      }
    }
    return convertedBytes.buffer.asUint8List();
  }

  classifyImage(File image) async {
    var output = await Tflite.runModelOnBinary(
        binary: imageToByteListFloat32(image, 224, 127.5, 127.5),// required
        numResults: 10,    // defaults to 5
        threshold: 0.05,  // defaults to 0.1
        asynch: true      // defaults to true
    );
    setState(() {
      _loading = false;
      _outputs = output;
    });
  }`

【问题讨论】:

  • 请正确格式化您的代码
  • 你好 Rizwan,欢迎来到 Stack Overflow。我尝试修复您的格式,但该过程可能会打乱缩进。一旦获得批准,请确保我做对了。

标签: tensorflow flutter deep-learning tensorflow-lite


【解决方案1】:

您可以使用颤振图像库从文件中获取图像。

import 'package:image/image.dart' as imageLib;
var image = imageLib.decodeImage(await imageFile.readAsBytes());

这个来自 shaquian 的 github repo 可能对你有帮助!

https://github.com/shaqian/flutter_tflite/blob/master/example/lib/main.dart

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 2020-06-13
    • 1970-01-01
    • 2011-12-21
    • 2011-08-10
    • 1970-01-01
    相关资源
    最近更新 更多