I am using tflite_flutter library V^0.9.0 (https://pub.dev/packages/tflite_flutter) in in my code base for image classification with custom tfLite model.
The tfLite uses RGB channels, same for image classification in Python script.
I am getting the accuracy problem, when we run same image on Python script and same image in mobile.
My Code:
Step 1: To get image from Library
String imageFile;
_getFromGallery() async {
try {
PickedFile? pickedFile = await ImagePicker().getImage(
source: ImageSource.gallery,
maxWidth: 1800,
maxHeight: 1800,
);
if (pickedFile != null) {
imageFile = pickedFile.path;
} else {
print("File is not available------");
openPhoneSetting();
throw Exception('File is not available');
}
} on PlatformException catch (e) {
openPhoneSetting();
print("File is not available+++++++");
print("Unsupported operation" + e.toString());
}
}
Step 2: Decode image
File imageFile = File(imageFile);
Uint8List imageRaw = await imageFile.readAsBytes();
img.Image? imageInput = img.decodeImage(imageRaw!);
var prediction = _classifier.predict(imageInput!);
var string = prediction.label;
Step 3: Predict image
TensorImage? _inputImage;
Category predict(Image image) {
print("Image Dimension::::${image.height} ${image.width}");
final pres = DateTime.now().millisecondsSinceEpoch;
_inputImage = TensorImage(_inputType!);
_inputImage?.loadImage(image);
_inputImage = _preProcess();
final pre = DateTime.now().millisecondsSinceEpoch - pres;
print('Time to load image: $pre ms');
final runs = DateTime.now().millisecondsSinceEpoch;
interpreter!.run(_inputImage!.buffer, _outputBuffer!.getBuffer());
final run = DateTime.now().millisecondsSinceEpoch - runs;
print('Time to run inference: $run ms');
Map<String, double> labeledProb = TensorLabel.fromList(
labels!, _probabilityProcessor.process(_outputBuffer))
.getMapWithFloatValue();
final pred = getTopProbability(labeledProb);
return Category(pred.key, pred.value);
}
Step 4: Processed image
TensorImage _preProcess() {
return ImageProcessorBuilder()
.add(ResizeOp(
_inputShape![1], _inputShape![2], ResizeMethod.BILINEAR))
.build()
.process(_inputImage!);
}
I want to showcase this _inputImage
Results are in given image Links:
- https://ibb.co/Tvmt9R0
- https://ibb.co/f4b4LKk
- https://ibb.co/0Qw26Xd
- https://ibb.co/1mg1pv4
- https://ibb.co/zVz2d2v
- https://ibb.co/6DLK4Dw
"I read several documents but could not find solutions. Is it possible to process image in Flutter similar way as done by matplotlib in Python e.g. "import matplotlib.pyplot as plt".