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

Error in model prediction #238

Open
DarshanDayal123 opened this issue Jul 15, 2023 · 0 comments
Open

Error in model prediction #238

DarshanDayal123 opened this issue Jul 15, 2023 · 0 comments

Comments

@DarshanDayal123
Copy link

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart';
import 'package:tflite_flutter/tflite_flutter.dart';

void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);

@OverRide
Widget build(BuildContext context) {
return MaterialApp(
title: 'Cat or Dog',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: SplashPage(),
);
}
}

class SplashPage extends StatefulWidget {
@OverRide
_SplashPageState createState() => _SplashPageState();
}

class _SplashPageState extends State {
File? _imageFile;
late Interpreter _interpreter;
late List _labels;
bool _isLoaded = false;
bool _isImagePickerActive = false; // Flag to track image picker activity

@OverRide
void initState() {
super.initState();
loadModel().then((_) {
setState(() {
_isLoaded = true;
});
});
}

Future loadModel() async {
_interpreter = await Interpreter.fromAsset('assets/model.tflite');
_labels = await loadLabels('assets/labels.txt');
}

Future<List> loadLabels(String path) async {
String labelData = await rootBundle.loadString(path);
return labelData.trim().split('\n');
}

Future classifyImage(File imageFile) async {
if (!_isLoaded) return;

var inputImage = await imageFile.readAsBytes();
var output = List.filled(_labels.length, 0).reshape([1, _labels.length]);

_interpreter.run(inputImage, output);

var predictionIndex = output[0].indexOf(output[0].reduce((a, b) => a > b ? a : b));
var predictionLabel = predictionIndex == 0 ? 'Dog' : 'Cat';

showDialog(
  context: context,
  builder: (context) => AlertDialog(
    title: Text('Prediction'),
    content: Text('The image is classified as $predictionLabel.'),
    actions: [
      TextButton(
        onPressed: () => Navigator.pop(context),
        child: Text('OK'),
      ),
    ],
  ),
);

}

Future selectImage() async {
if (_isImagePickerActive) return; // Check if image picker is already active
_isImagePickerActive = true; // Set the flag to true

var pickedImage = await ImagePicker().pickImage(source: ImageSource.gallery);
_isImagePickerActive = false; // Reset the flag

if (pickedImage == null) return;

setState(() {
  _imageFile = File(pickedImage.path);
});

classifyImage(_imageFile!);

}

@OverRide
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Cat or Dog'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (_imageFile != null)
Image.file(
_imageFile!,
height: 200,
),
SizedBox(height: 20),
ElevatedButton(
onPressed: selectImage,
child: Text('Select Image'),
),
],
),
),
);
}
}

output its take image but don't predict that this is cat or dog error got on console Unable to match the desired swap behavior.
E/flutter (29351): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Bad state: failed precondition

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