androidflutterflutter-dependenciestflitejava-runtime-compiler

How to resolve this "Unhandled Exception: PlatformException(Failed to run model, Interpreter busy,java.lang.RuntimeException: Interpreter busy" issue?


I am trying to create an android application utilizing the yolov5 model from TFlite. My custom detector will detect one class in real-time. As well as this app was built into my device, but after 2-3 seconds, later app closed, and an error occurred (Given Below in Error Message Block). I found some previous questions answers but did not work for me. What can be the permanent solution for this? Cause I am struggling to solve this issue for 5 hours.

My dependencies are (pubspec.yaml):

name: bat_app
description: A new Flutter project.

publish_to: 'none' # Remove this line if you wish to publish to pub.dev


version: 1.0.0+1

environment:
  sdk: '>=2.16.1 <3.0.0'


dependencies:
  flutter:
    sdk: flutter
  
  tflite: ^1.1.2
  camera: ^0.10.3+2



  cupertino_icons: ^1.0.2

dev_dependencies:
  flutter_test:
    sdk: flutter

  flutter_lints: ^1.0.0

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

# The following section is specific to Flutter packages.
flutter:
  uses-material-design: true

  
  assets:
    - assets/
  

  # An image asset can refer to one or more resolution-specific "variants", see
  # https://flutter.dev/assets-and-images/#resolution-aware

  # For details regarding adding assets from package dependencies, see
  # https://flutter.dev/assets-and-images/#from-packages

  # To add custom fonts to your application, add a fonts section here,
  # in this "flutter" section. Each entry in this list should have a
  # "family" key with the font family name, and a "fonts" key with a
  # list giving the asset and other descriptors for the font. For
  # example:
  # fonts:
  #   - family: Schyler
  #     fonts:
  #       - asset: fonts/Schyler-Regular.ttf
  #       - asset: fonts/Schyler-Italic.ttf
  #         style: italic
  #   - family: Trajan Pro
  #     fonts:
  #       - asset: fonts/TrajanPro.ttf
  #       - asset: fonts/TrajanPro_Bold.ttf
  #         weight: 700
  #
  # For details regarding fonts from package dependencies,
  # see https://flutter.dev/custom-fonts/#from-packages

My home.dart page code:

import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:tflite/tflite.dart';
import 'main.dart';

class Home extends StatefulWidget {
  const Home({Key? key}) : super(key: key);

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  CameraImage? cameraImage;
  CameraController? cameraController;
  String output = '';

  @override
  void initState() {
    super.initState();
    
    loadModel();
    loadCamera();
  }

  loadCamera() {
    cameraController = CameraController(cameras![0], ResolutionPreset.medium);
    cameraController!.initialize().then((value) {
      if (!mounted) {
        return;
      } else {
        setState(() {
          cameraController!.startImageStream((imageStream) {
            cameraImage = imageStream;
            runModel();
          });
        });
      }
    });
  }

  runModel() async {
    if (cameraImage != null) {
      var predictions = await Tflite.runModelOnFrame(
          bytesList: cameraImage!.planes.map((plane) {
            return plane.bytes;
          }).toList(),
          imageHeight: cameraImage!.height,
          imageWidth: cameraImage!.width,
          imageMean: 127.5,
          imageStd: 127.5,
          rotation: 90,
          numResults: 2,
          threshold: 0.6,
          asynch: true);

      predictions!.forEach((element) {
        setState(() {
          output = element['label'];
        });
      });
    }
  }

  loadModel() async {
    await Tflite.loadModel(
        model: "assets/model.tflite", labels: "assets/labels.txt");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Racket Bat Scoring'),
      ),
      body: Column(
        children: [
          Padding(
            padding: const EdgeInsets.all(20),
            child: Container(
              height: MediaQuery.of(context).size.height * 0.7,
              width: MediaQuery.of(context).size.width,
              child: !cameraController!.value.isInitialized
                  ? Container()
                  : AspectRatio(
                      aspectRatio: cameraController!.value.aspectRatio,
                      child: CameraPreview(cameraController!),
                    ),
            ),
          ),
          Text(output,
              style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 20))
        ],
      ),
    );
  }
}

Error Message

E/flutter (16282): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(Failed to run model, Interpreter busy, java.lang.RuntimeException: Interpreter busy
E/flutter (16282):      at sq.flutter.tflite.TflitePlugin$TfliteTask.<init>(TflitePlugin.java:450)
E/flutter (16282):      at sq.flutter.tflite.TflitePlugin$RunModelOnFrame.<init>(TflitePlugin.java:545)
E/flutter (16282):      at sq.flutter.tflite.TflitePlugin.onMethodCall(TflitePlugin.java:117)
E/flutter (16282):      at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:258)
E/flutter (16282):      at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
E/flutter (16282):      at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:322)
E/flutter (16282):      at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
E/flutter (16282):      at android.os.Handler.handleCallback(Handler.java:938)
E/flutter (16282):      at android.os.Handler.dispatchMessage(Handler.java:99)
E/flutter (16282):      at android.os.Looper.loop(Looper.java:236)
E/flutter (16282):      at android.app.ActivityThread.main(ActivityThread.java:7876)
E/flutter (16282):      at java.lang.reflect.Method.invoke(Native Method)
E/flutter (16282):      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
E/flutter (16282):      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
E/flutter (16282): , null)
E/flutter (16282): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:668:7)
E/flutter (16282): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:315:18)
E/flutter (16282): <asynchronous suspension>
E/flutter (16282): #2      Tflite.runModelOnFrame (package:tflite/tflite.dart:73:12)
E/flutter (16282): <asynchronous suspension>
E/flutter (16282): #3      _HomeState.runModel (package:bat_app/Home.dart:43:25)
E/flutter (16282): <asynchronous suspension>
E/flutter (16282):
D/OOMEventManagerFK(16282): checkEventAndDumpForJE: 0
I/Process (16282): Sending signal. PID: 16282 SIG: 9
Lost connection to device.

Solution

  • In which part of the code we should call await Tflite.close();.

    /// example
    Future loadModel() async {
        await Tflite.close(); // This line add
        try {
         String res;
          switch (_model) {
            case yolo:
              res = await Tflite.loadModel(
                model: "assets/yolov2_tiny.tflite",
                labels: "assets/yolov2_tiny.txt",
                // useGpuDelegate: true,
              );
              break;
         /// ...
        } on PlatformException {
          print('Failed to load model.');
        }
      }
    

    Source: example

    See link to discussion of this case https://github.com/shaqian/flutter_tflite/issues/47#issuecomment-1061423668