dart 有没有办法在Flutter with Tflite中实现批次分类?

zhte4eai  于 2023-03-15  发布在  Flutter
关注(0)|答案(1)|浏览(160)

我想迭代地对多幅图像进行分类,我用

Future pickImages() async {
final List<XFile>? images = await ImagePicker().pickMultiImage();
if (images == null) return;

images.forEach((image) async {
  print('- Name: ${image.path}');
  await runModel(image.path);
});

}

Future runModel(String imagePath) async {
var recognitions = await Tflite.runModelOnImage(
    path: imagePath,
    numResults: 1,
    threshold: 0.5,
    imageMean: 127.5,
    imageStd: 127.5,
    asynch: true // defaults to true
    );

print(recognitions);
await Future.delayed(Duration(seconds: 1));
setState(() {});

}

如果我只选择一个图像,一切运行正常,只要我继续选择多个图像,我得到以下错误:

PlatformException (PlatformException(Failed to run model, Interpreter busy, java.lang.RuntimeException: Interpreter busy

我已经尽力了,但还是没能解决这个问题。
如果您能建议如何对多个选定的图像(ImagePicker)进行推断,我将非常感激。
谢谢!

qyyhg6bp

qyyhg6bp1#

这对我很有效:
对于pickImage:

async {
final ImagePicker _picker = ImagePicker();
final pickedFile = await _picker.pickMultiImage();
List<XFile> xfilePick = pickedFile;
setState(
      () {
    if (xfilePick.isNotEmpty) {
      for (var i = 0; i < xfilePick.length; i++) {
        selectedImages.add(File(xfilePick[i].path));
      }
    }
  },
);

for (var i = 0; i < selectedImages.length; i++){
    await Future.delayed(const Duration(seconds: 1));{
      imageClassification(selectedImages[i]);
      await Future.delayed(const Duration(seconds: 1));
    }
  }}

相关问题