0

why im getting this error, im trying to run a object detection app in flutter while running im getting this error in run time, and the object text also not appear

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(Failed to run model, Interpreter busy, java.lang.RuntimeException: Interpreter busy

My code,

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

String result = "";

class CameraScreen extends StatefulWidget {
  final CameraDescription camera;

  CameraScreen(this.camera);

  @override
  _CameraScreenState createState() => _CameraScreenState();
}

class _CameraScreenState extends State<CameraScreen> {
  loadModel() async {
    await Tflite.loadModel(
        model: "assets/mobilenet_v1_1.0_224.tflite",
        labels: "assets/mobilenet_v1_1.0_224.txt");
  }

  late CameraController _controller;
  late CameraImage imgCam;
  late Future<void> _initializeControllerFuture;

  @override
  void initState() {
    super.initState();
    _controller = CameraController(widget.camera, ResolutionPreset.medium);
    _initializeControllerFuture = _controller.initialize().then((value) => {
          setState((() {
            _controller.startImageStream((imageFromStream) => {
                  imgCam = imageFromStream,
                  runModel(),
                });
          }))
        });
    loadModel();
  }

  runModel() async {
    if (imgCam != null) {
      var recognition = await Tflite.runModelOnFrame(
        bytesList: imgCam.planes.map((plane) {
          return plane.bytes;
        }).toList(),
        imageHeight: imgCam.height,
        imageWidth: imgCam.width,
        imageMean: 127.5,
        imageStd: 127.5,
        rotation: 90,
        numResults: 1,
        threshold: 0.1,
        asynch: true,
      );
      result = "yes";
      recognition?.forEach((response) {
        result += response["label"] +
            " " +
            (response["confidence"] as double).toStringAsFixed(2) +
            "\n\n";
        print(result);
        print("\n\n\n\n\n\n\n\n\n\n");
      });
      setState(() {
        result;
      });
    }
  }

  @override
  void dispose() async {
    _controller.dispose();
    await Tflite.close();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    final deviceRatio = size.width / size.height;
    return Scaffold(
        body: FutureBuilder<void>(
            future: _initializeControllerFuture,
            builder: (context, snapshot) {
              if (snapshot.connectionState == ConnectionState.done) {
                // If the Future is complete, display the preview
                return AspectRatio(
                  aspectRatio: deviceRatio,
                  child: CameraPreview(
                      child: Center(
                        child: Text(result),
                      ),
                      _controller),
                );
              } else {
                // Otherwise, display a loading indicator
                return Center(child: CircularProgressIndicator());
              }
            }));
  }
}
  • 1
    ...I can't believe I'm saying this, but I think its a [duplicate](https://stackoverflow.com/questions/67307566/unhandled-exception-platformexceptionfailed-to-run-model-interpreter-busy-ja) – TekExplorer Jan 13 '23 at 19:32
  • You seem to have posted more code than what would be reasonable for your issue. Please read [ask] and how to make a [mre]; providing a MRE helps users answer your question and future users relate to your issue. – Jim G. Jan 18 '23 at 18:53

0 Answers0