You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`Future<img.Image> resizeImage(File imageFile) async {
// Load the image from the file
final originalImage = img.decodeImage(await imageFile.readAsBytes());
if (originalImage == null) {
throw Exception('Failed to load the image');
}
// Resize the image to the expected input size (640x640)
final resizedImage = img.copyResize(originalImage, width: 640, height: 640);
return resizedImage;
}
Future imageClassification(File imageFile) async {
try {
// Load the TFLite model
final interpreter = await Interpreter.fromAsset('assets/best_float32.tflite');
// Get the input and output tensor details
final inputTensor = interpreter.getInputTensor(0);
final outputTensor = interpreter.getOutputTensor(0);
print(inputTensor);
print(outputTensor);
// Preprocess the image
final resizedImage = await resizeImage(imageFile);
if (resizedImage == null) {
throw Exception('Failed to resize the image');
}
// Normalize the input image
final input = Uint8List.fromList(resizedImage.getBytes().map((pixel) => (pixel / 255.0).round()).toList());
// Create the output tensor
final output = List<double>.filled(outputTensor.shape[1], 0.0);
// Run the model
interpreter.run(input, output);
// Print the output
print('-----------------------------------------------------------');
print('Output tensor shape: ${outputTensor.shape}');
print('Output: $output');
print('-----------------------------------------------------------');
// Clean up resources (important!)
interpreter.close();
} catch (e, stackTrace) {
print('Error during image classification: $e');
print('Stack trace: $stackTrace');
}
}
Future<File?> pickImage() async {
final ImagePicker picker = ImagePicker();
final XFile? pickedFile =
await picker.pickImage(source: ImageSource.gallery);
if (pickedFile != null) {
imageClassification(File(pickedFile.path));
}
return null;
}`
but when I pick an image i get the following error. please help me i solve this problem
i have the following code
`Future<img.Image> resizeImage(File imageFile) async {
// Load the image from the file
final originalImage = img.decodeImage(await imageFile.readAsBytes());
}
Future imageClassification(File imageFile) async {
try {
// Load the TFLite model
final interpreter = await Interpreter.fromAsset('assets/best_float32.tflite');
}
Future<File?> pickImage() async {
final ImagePicker picker = ImagePicker();
final XFile? pickedFile =
await picker.pickImage(source: ImageSource.gallery);
if (pickedFile != null) {
imageClassification(File(pickedFile.path));
}
return null;
}`
but when I pick an image i get the following error. please help me i solve this problem
I/flutter (17481): Tensor{_tensor: Pointer: address=0xb400007206d41540, name: inputs_0, type: float32, shape: [1, 640, 640, 3], data: 4915200} I/flutter (17481): Tensor{_tensor: Pointer: address=0xb400007206d4c750, name: Identity, type: float32, shape: [1, 5, 8400], data: 168000} I/flutter (17481): Error during image classification: Bad state: failed precondition I/flutter (17481): Stack trace: #0 checkState (package:quiver/check.dart:74:5) I/flutter (17481): #1 Tensor.setTo (package:tflite_flutter/src/tensor.dart:167:7) I/flutter (17481): #2 Interpreter.runInference (package:tflite_flutter/src/interpreter.dart:210:33) I/flutter (17481): #3 Interpreter.runForMultipleInputs (package:tflite_flutter/src/interpreter.dart:180:5) I/flutter (17481): #4 Interpreter.run (package:tflite_flutter/src/interpreter.dart:172:5) I/flutter (17481): #5 HomeController.imageClassification (package:fmd_vet/pages/home/controller.dart:100:19) I/flutter (17481): <asynchronous suspension> I/ViewRootImpl@9a894d1[MainActivity](17481): onDisplayChanged oldDisplayState=2 newDisplayState=2
The text was updated successfully, but these errors were encountered: