Embedding ML Model in Flutter App | by Ameycorporates | Mar, 2024


we train a model but most of the developer don’t know how to use it in the app.

In this i will explain how to use ML model in app using Tensorflow Lite in Flutter.

First,convert your .h5 model file to .tflite file.

To do so use following code in Google Colab.

from tensorflow.contrib import lite
converter = lite.TFLiteConverter.from_keras_model_file( 'model.h5')
tfmodel = converter.convert()
open ("model.tflite" , "wb") .write(tfmodel)

Download and save this “model.tflite” file in to the assets folder

also create “labels.txt” file in assets folder to store the labels of the model.

Flutter part:

Now add this plugins to pubspec.yaml file

flutter_tflite: ^1.0.1
image_picker: ^0.8.5+3

note: check the latest version as per flutter compatibility

main.dart code :

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:flutter_tflite/flutter_tflite.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'TFLite Image Classification',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: ImageClassificationPage(),
);
}
}

class ImageClassificationPage extends StatefulWidget {
@override
_ImageClassificationPageState createState() => _ImageClassificationPageState();
}

class _ImageClassificationPageState extends State<ImageClassificationPage> {
File? _image;
List<dynamic>? _output;
bool _loading = false;

@override
void initState() {
super.initState();
_loading = true;
loadModel().then((value) {
setState(() {
_loading = false;
});
});
}

@override
void dispose() {
Tflite.close();
super.dispose();
}

Future<void> loadModel() async {
await Tflite.loadModel(
model: 'assets/model.tflite',
labels: 'assets/labels.txt',
);
}

Future<void> classifyImage(File image) async {
if (image == null) return;
var output = await Tflite.runModelOnImage(
path: image.path,
numResults: 5,
threshold: 0.5,
imageMean: 127.5,
imageStd: 127.5,
);
setState(() {
_loading = false;
_output = output;
});
}

Future<void> _pickImage(ImageSource source) async {
var image = await ImagePicker().pickImage(source: source);
if (image == null) return;
setState(() {
_loading = true;
_image = File(image.path);
});
classifyImage(_image!);
}

Future<void> _pickImageFromGallery() async {
_pickImage(ImageSource.gallery);
}

Future<void> _pickImageFromCamera() async {
_pickImage(ImageSource.camera);
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('RETINOSCAN'),
),
body: Center(
child: _loading
? CircularProgressIndicator()
: _output != null
? Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.file(
_image!,
width: 300,
height: 300,
),
SizedBox(height: 20),
Text(
'Prediction:',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
SizedBox(height: 10),
Text(
'${_output![0]['label']}',
style: TextStyle(fontSize: 16),
),
],
)
: Text('No image selected'),
),
// floatingActionButton: FloatingActionButton(
// onPressed: () {
// _pickImage(ImageSource.gallery);
// },
// tooltip: 'Pick Image',
// child: Icon(Icons.image),
// ),
floatingActionButton: Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
FloatingActionButton(
onPressed: _pickImageFromGallery,
tooltip: 'Pick Image from Gallery',
child: Icon(Icons.image),
),
SizedBox(height: 16),
FloatingActionButton(
onPressed: _pickImageFromCamera,
tooltip: 'Capture Image from Camera',
child: Icon(Icons.camera_alt),
),
],
),

);
}
}

use Flutter run and build the app.

Error Part : minimum SDK API level should be 26. (Remember)

Output:

I hope this will help you to integrate your model into the flutter app.

Feel Free to connect me on Linkedin :

https://www.linkedin.com/in/amey-kulkarni-746a081a6?utm_source=share&utm_campaign=share_via&utm_content=profile&utm_medium=ios_app



Source link

Be the first to comment

Leave a Reply

Your email address will not be published.


*