【问题标题】:Flutter can't detect 'model.tflite' file from assets folder (FileNotFoundException)Flutter 无法从资产文件夹中检测到“model.tflite”文件(FileNotFoundException)
【发布时间】:2020-11-11 06:09:05
【问题描述】:

背景

我正在 android studio 上使用颤振制作水果分类应用程序。该应用程序将按以下方式运行:

  1. 用相机拍照。
  2. 预测图片中的水果类型。

要执行第 (2) 步,我使用的是从 teachable machine 导出的 'model.tflite' 文件。我也在使用 tflite 插件进行颤振。

问题

当我的程序尝试预测图像时,我在控制台中收到以下消息:


    E/MethodChannel#tflite( 7296): Failed to handle method call
    E/MethodChannel#tflite( 7296): java.lang.IllegalArgumentException: Unsupported value: java.io.FileNotFoundException: flutter_assets/assets/model.tflite
    E/MethodChannel#tflite( 7296):  at io.flutter.plugin.common.StandardMessageCodec.writeValue(StandardMessageCodec.java:278)
    E/MethodChannel#tflite( 7296):  at io.flutter.plugin.common.StandardMethodCodec.encodeErrorEnvelope(StandardMethodCodec.java:69)
    E/MethodChannel#tflite( 7296):  at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler$1.error(MethodChannel.java:236)
    E/MethodChannel#tflite( 7296):  at sq.flutter.tflite.TflitePlugin.onMethodCall(TflitePlugin.java:98)
    E/MethodChannel#tflite( 7296):  at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:226)
    E/MethodChannel#tflite( 7296):  at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
    E/MethodChannel#tflite( 7296):  at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:631)
    E/MethodChannel#tflite( 7296):  at android.os.MessageQueue.nativePollOnce(Native Method)
    E/MethodChannel#tflite( 7296):  at android.os.MessageQueue.next(MessageQueue.java:336)
    E/MethodChannel#tflite( 7296):  at android.os.Looper.loop(Looper.java:197)
    E/MethodChannel#tflite( 7296):  at android.app.ActivityThread.main(ActivityThread.java:7948)
    E/MethodChannel#tflite( 7296):  at java.lang.reflect.Method.invoke(Native Method)
    E/MethodChannel#tflite( 7296):  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    E/MethodChannel#tflite( 7296):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
    I/flutter ( 7296):  
    I/flutter ( 7296): ERROR: PlatformException(error, Unsupported value: java.io.FileNotFoundException: flutter_assets/assets/model.tflite, null)

Flutter 似乎无法从我的资产文件夹中找到 model.tflite 文件!

我尝试过的

  • 我已经三次检查了pubspec.yaml(下)中的缩进。文件路径和目录名称也正确。

    flutter:
      assets:
        - assets/labels.txt
        - assets/model.tflite

  • 我已阅读tflite docs
  • 我已将 model.tflite 下载为浮点类型和量化类型。
  • 我已尝试重命名 model.tflite 文件(您可以看出我快绝望了)。
  • 我还尝试在我的代码中更改某些参数。下面是似乎发生错误的函数 - 每当用户拍照时都会调用它。

      Future classifyImage() async {
        try{
          String res = await Tflite.loadModel(
              model: 'assets/model.tflite',
              labels: 'assets/labels.txt',
              numThreads: 1,
              isAsset: true,
              useGpuDelegate: true
          );
          print(res);
          var recognitions = await Tflite.runModelOnImage(
            path: _image.path,
            imageMean: 117.0,
            imageStd: 1.0,
            numResults: 1,
            threshold: 0.5,
            asynch: true,
          );
          _recognitions = recognitions;
          await Tflite.close();
        } catch (error){
          print(' ');
          print('ERROR: $error');
        }

  • 这是我的项目目录的screenshot
  • 我也试过flutter clean,退出android studio,重启项目。

请帮忙!我目前正在等待有人从天上飘来神奇地解决我的问题。我也是一个初学者,所以对我的代码的任何一般性建议将不胜感激:-)。

谢谢。

【问题讨论】:

  • (作为健全性检查,您是否停止了应用并重新启动/重建?)
  • 愚蠢的问题,但您实际上在项目的资产文件夹中有一个名为 model.tflite 的文件?
  • 感谢您的回复。我的资产文件夹中确实有一个 model.tflite 文件。我还重新启动了应用程序并使用了flutter clean。

标签: android flutter flutter-dependencies flutter-packages


【解决方案1】:

继续打开 android/app/build.gradle 文件并在 android 块中添加以下代码。

aaptOptions {
    noCompress 'tflite'
    noCompress 'lite'
}

【讨论】:

    【解决方案2】:

    我第二天继续我的项目,错误似乎没有出现。不太清楚发生了什么... 也许这与我的笔记本电脑有关?

    【讨论】:

      猜你喜欢
      • 2016-01-25
      • 2020-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多