Machine Learning in Flutter!

Step 1: Load the model

loadModel() async {
await Tflite.loadModel(
model: "assets/model_unquant.tflite",
labels: "assets/labels.txt",
);
}
@override
void initState() {
super.initState();
loadModel().then((value) {
setState((){});
});
}

Step 2: Create 2 variables, one for image and other for output.

List _outputs;  
File _image;

Step 3: Open Gallery or Camera to get an Image using image_picker

Future openCamera() async {
var image = await ImagePicker.pickImage(
source: ImageSource.camera,
);
setState(() {
_image = image;
});
classifyImage(image);
}
Future openGallery() async {
var image;
image = await ImagePicker.pickImage(
source: ImageSource.gallery,
);
setState(() {
_image = image;
});
classifyImage(image);
}

Step 4: Pass image to our classifier model

classifyImage(File image) async {
var output = await Tflite.runModelOnImage(
path: filepath, // required
imageMean: 0.0, // defaults to 117.0
imageStd: 255.0, // defaults to 1.0
numResults: 2, // defaults to 5
threshold: 0.2, // defaults to 0.1
asynch: true // defaults to true
);
setState(() {
_outputs = output;
});
}

Step 5: Print the class name!

Text('${_outputs[0]["label"]}')

Step 6: Finally, you need to close the TFlite model in dispose() method

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

Don’t forget to connect with me on:

Don’t stop, until you are breathing!💙
- Abhishek Doshi

--

--

--

Google Developer Expert — Dart, Flutter & Firebase 💙💛

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Deploybot, or our long journey towards Continuous Deployment

Shine bridge to Matic is live!

Identify AWS Resources Exposed to the World using Cloud Custodian

On-Premise vs Cloud Software: The Benefits, Risk and Process of Migrating to the Cloud

Running Flask and FastAPI on Google Colab

Microsoft Azure- How does Azure Work ? (Azure-01)

How to change APN settings on an Android Mobile Phone

Drinking the OTP Elixir

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhishek Doshi

Abhishek Doshi

Google Developer Expert — Dart, Flutter & Firebase 💙💛

More from Medium

Face Detection in Mobile App using Python, Flask, and Flutter

The Ultimate Guide to Flutter

The Ultimate Guide to Flutter

5 tips to Learn Flutter Like a Pro [from a Flutter course creator]

Almost Netflix: A Netflix clone built with Flutter + Appwrite