0

I call model from firebase through firebase_ml_model_downloader it work fine but when I try to work with my model through this flutter_tflite it giving error

enter image description here

init model in first screen like this

  /// Initially get the lcoal model if found, and asynchronously get the latest one in background.
  initWithLocalModel() async {
    final _model = await FirebaseModelDownloader.instance.getModel(
        'logos', FirebaseModelDownloadType.localModelUpdateInBackground);

    setState(() {
      model = _model;
    });
  }

Here's how I am trying to make used of my model

  @override
  void initState() {
    //initS is the first function that is executed by default when this class is called
    super.initState();
    loadModel().then((value) {
      setState(() {});
      Future.delayed(
        const Duration(seconds: 3),
        () {
          log(widget.image.path.toString());
          classifyImage(widget.image);
        },
      );
    });
  }

  loadModel() async {
    //this function loads our model
    log(mode);
    try {
      await Tflite.loadModel(
          model: model.file.toString(),
        
          numThreads: 2, // defaults to 1
          isAsset:
              true, // defaults to true, set to false to load resources outside assets
          useGpuDelegate:
              false // defaults to false, set to true to use GPU delegate
          );
    } on PlatformException {
      debugPrint("Failed to load Model");
    }
  }
  classifyImage(CroppedFile image) async {
    var recognitions = await Tflite.runModelOnImage(
        path: image.path,
        // imageMean: 0.0, // defaults to 117.0
        imageStd: 20.0, // defaults to 1.0
        numResults: 2, // defaults to 5
        threshold: 0.9, // defaults to 0.1
        asynch: true);
    setState(() {
      _output = recognitions!;
      _loading = true;
    });
    debugPrint("output data is ${_output.toString()}");
  }

Error That I am Facing

Exception has occurred.
PlatformException (PlatformException(Failed to run model, Attempt to invoke virtual method 'org.tensorflow.lite.Tensor org.tensorflow.lite.Interpreter.getInputTensor(int)' on a null object reference, java.lang.NullPointerException: Attempt to invoke virtual method 'org.tensorflow.lite.Tensor org.tensorflow.lite.Interpreter.getInputTensor(int)' on a null object reference
    at sq.flutter.flutter_tflite.TflitePlugin.feedInputTensor(TflitePlugin.java:368)
    at sq.flutter.flutter_tflite.TflitePlugin.feedInputTensorImage(TflitePlugin.java:430)
    at sq.flutter.flutter_tflite.TflitePlugin$RunModelOnImage.<init>(TflitePlugin.java:536)
    at sq.flutter.flutter_tflite.TflitePlugin.onMethodCall(TflitePlugin.java:139)
    at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:262)
    at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
    at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319)
    at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:268)
    at android.app.ActivityThread.main(ActivityThread.java:8101)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:627)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:997)
, null))
Shady Aziza
  • 50,824
  • 20
  • 115
  • 113
Anas Ayub
  • 13
  • 1
  • 4

0 Answers0