所以我是颤振和张量流的新手,我一直在尝试为颤振应用程序创建自定义植物图像分类模型。为了集成模型,我在 flutter 中使用了“google_ml_image_labelling”包,当我尝试运行时出现以下错误: 'PlatformException(PlatfromException(ImageLabelDetectorError,com.google,mlkit.common.MlKtException:无法初始化检测器。输入张量的类型为 kTfLiteFloat32:它需要指定 NormalizationOptions 元数据来预处理输入图像。,null,null))。 这是我在flutter中使用的代码:
late ImagePicker imagePicker;
File? _image;
String idResult = '';
dynamic imageLabeler;
@override
void initState() {
super.initState();
imagePicker = ImagePicker();
createLabeler();
}
@override
void dispose() {
super.dispose();
}
_chooseImage() async {
XFile? image = await imagePicker.pickImage(
source: ImageSource
.gallery);
if (image != null) {
setState(() {
_image = File(image
.path);
imageLabelling();
});
}
}
_takePicture() async {
final XFile? image = await imagePicker.pickImage(
source: ImageSource
.camera);
if (image != null) {
setState(() {
_image = File(image
.path);
imageLabelling();
});
}
}
createLabeler() async {
final modelPath = await getModelPath('assets/ml/model.tflite');
final options = LocalLabelerOptions(
modelPath: modelPath,
);
imageLabeler = ImageLabeler(options: options);
}
Future<String> getModelPath(String asset) async {
final path = '${(await getApplicationSupportDirectory()).path}/$asset';
await Directory(dirname(path)).create(recursive: true);
final file = File(path);
if (!await file.exists()) {
final byteData = await rootBundle.load(asset);
await file.writeAsBytes(byteData.buffer
.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));
}
return file.path;
}
imageLabelling() async {
InputImage theInputImage = InputImage.fromFile(
_image!); //In order to use the image labelling package, the image needs to be
//converted into the InputImage type. This is converting the image into the type InputImage.
final List<ImageLabel> labels =
await imageLabeler.processImage(theInputImage);
for (ImageLabel label in labels) {
final String text = label.label;
final int index = label.index;
final double confidence = label.confidence;
idResult += text + " " + confidence.toStringAsFixed(2);
}
setState(() {
idResult;
});
}
出现此错误后,我研究了使用元数据添加标准化选项,并在 google colab 上找到了唯一的教程,但在安装 tflite_support 之后;当我尝试执行下一步从其中导入指定的包时:
from tflite_support import metadata_schema_py_generated as _metadata_fb
from tflite_support import metadata as _metadata
我收到一条错误消息:“ImportError:generic_type:无法初始化类型“StatusCode”:已定义具有该名称的对象”。
我尝试测试此问题,看看是否只是我的 tflite 代码导致了此错误,在新的 google colab 会话中安装 tflite_support 后尝试导入软件包,但错误仍然存在。然后我查看 tflite_support 包是否已停止使用等,但找不到任何东西。如果您可以看到我的 flutter 代码中的任何问题,或者知道如何修复我遇到的错误,或者知道如何修复我的张量流问题,甚至知道如何使用标准化选项添加元数据(假设这是修复 Flutter 错误的方法) )如果不使用以前的方法,这将非常有帮助。任何帮助,将不胜感激。谢谢。
据我所知,tflite 模型需要元数据才能运行。关于“无法初始化类型 StatusCode”错误,我最近通过降级 Google Colab 中的 TensorFlow 版本,通过 tflite_support 导入解决了相同的情况,如下所示:
!pip uninstall tensorflow
!pip install tensorflow=="2.13.0"
希望对你有帮助