so I'm new to flutter and tensorflow and I have been trying to create a custom plant image classification model for a flutter app. To integrate the model I was using the 'google_ml_image_labelling' package in flutter and when I tried to run there was the following error: 'PlatformException (PlatfromException(ImageLabelDetectorError, com.google,mlkit.common.MlKtException: Failed to initialize detector. Input tensor has type kTfLiteFloat32: it requires specifying NormalizationOptions metadata to preprocess input images., null, null)). This is the code I used in 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;
});
}
After this error I looked into adding normalisation options with metadata and found the only tutorial I could on it on google colab but after installing tflite_support; when I tried to do the next step of importing the stated packages from it:
from tflite_support import metadata_schema_py_generated as _metadata_fb
from tflite_support import metadata as _metadata
I received an error stating: 'ImportError: generic_type: cannot initialize type "StatusCode": an object with that name is already defined'.
I tried testing this to see if it was just my tflite code causing this error by trying to import the packages after installing tflite_support in a new google colab session and the error remained. I then looked to see if the tflite_support package has been discontinued etc. and couldn't find anything. If you can see any problems in my flutter code or know how to fix that error I was getting in it or know how to fix my tensorflow problem or even know how to add metadata with normalised options (assuming that's the way to fix the Flutter error ) without using that previous method it would be very helpful. Any help would be appreciated. Thanks.
As far as I know, tflite models require metadata in order to run. Regarding the "cannot initialize type StatusCode" error, I recently resolved the same situation with tflite_support imports simply by downgrading the TensorFlow version in Google Colab like: