Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I am trying to do image classification #244

Open
blezzed opened this issue Aug 22, 2024 · 0 comments
Open

I am trying to do image classification #244

blezzed opened this issue Aug 22, 2024 · 0 comments

Comments

@blezzed
Copy link

blezzed commented Aug 22, 2024

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());

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/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

@blezzed blezzed changed the title i am tiring to do image classification I am trying to do image classification Aug 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant