在隔离中运行包- Flutter

8yoxcaq7  于 2023-10-22  发布在  Flutter
关注(0)|答案(1)|浏览(113)

我正在尝试运行一些图像处理和MLmodel预测内部隔离的所有图像在画廊设备。但是一些软件包,比如FaceDetector(来自google_ml_kit)和PhotoGallery,不允许我在isolate内部运行。问题是,是否有可能在隔离中运行所有内容,比如带有这些导入的整个包?
没有使用isolate,所有的代码都在工作,而且速度很快,但是当我开始加载和处理图像时,屏幕冻结了。我也可以处理一个接一个的图像,而不冻结,但需要太多的时间。
因此,我尝试使用isolate运行,例如当运行以下代码时:

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");

我得到了这个:

[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

但是我不能单独调用WidgetsFlutterBinding.ensureInitialized(),而我的应用已经有了。
有没有其他办法找到所有的照片和albun,可以单独调用?和面部检测器单独检测整个包裹有可能吗?

h6my8fg2

h6my8fg21#

由于dart isolate的性质,您无法访问从main isolate到另一个分离株的软件包,但您可以使用flutter_isolate来实现相同的功能。用法与我们在Dart Isolate中的用法相同,其他隔离中的包可访问性也相同。

相关问题