Run packages in isolate - Flutter

369 Views Asked by At

I'm trying to run some image processing and MLmodel prediction inside isolate for all images in the gallery device. But some packages like FaceDetector(from google_ml_kit), and PhotoGallery, don't let me run inside isolate. The question is, exist any possibility to run everything inside an isolate, like an entire package with these imports?

Without using isolate all the code is working and is kind of fast but the screen freezes when I start to load and process the images. I also could process one by one image whitout freezing, but takes too much time.

So I m trying to run with isolate for example when a run this code:

void process(String path){
  final options = FaceDetectorOptions(
        performanceMode: FaceDetectorMode.accurate,
        minFaceSize: .4,
      );
      final faceDetector = FaceDetector(options: options);

      var file = File(path);
      var image = InputImage.fromFile(file);
      var bytes = file.readAsBytesSync();
      var faces = (await faceDetector.processImage(image))
          .map(
            (e) => FaceRect(
              e.boundingBox.left,
              e.boundingBox.top,
              e.boundingBox.width,
              e.boundingBox.height,
            ),
          )
          .toList();
      var faceImage = FacesImage(path, faces, bytes);
}

compute(process, "/storage/emulated/0/WhatsApp/Media/WhatsApp Images/IMG-20221022-WA0006.jpeg");

I get this:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Binding has not yet been initialized.
E/flutter (32478): The "instance" getter on the ServicesBinding binding mixin is only available once that binding has been initialized.
E/flutter (32478): Typically, this is done by calling "WidgetsFlutterBinding.ensureInitialized()" or "runApp()" (the latter calls the former). Typically this call is done in the "void main()" method. The "ensureInitialized" method is idempotent; calling it multiple times is not harmful. After calling that method, the "instance" getter will return the binding

But I can't call WidgetsFlutterBinding.ensureInitialized() in isolate and my app already have.

There is any alternative to find all photos and albuns that can be called in isolate? and faceDetector? Run an entire package in isolate is a possibility?

1

There are 1 best solutions below

0
On

You can't access packages from main isolate to another isolates due to the nature of dart isolate, but you can use flutter_isolate to achieve the same. The usage are same as we have in Dart Isolate with the package accessibility in other isolates.