flutter pub add image_picker
flutter pub add tflite
flutter pub add tflite_flutter
pubspec.yaml
name: ml_app
description: A new Flutter project.
publish_to: "none"
version: 0.1.0
environment:
sdk: ">=3.0.5 <4.0.0"
dependencies:
flutter:
sdk: flutter
image_picker: ^1.0.2
tflite: ^1.1.2
tflite_flutter: ^0.10.1
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^2.0.0
flutter:
uses-material-design: true
assets:
- assets/
android/app/build.gradle
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
localPropertiesFile.withReader('UTF-8') { reader ->
localProperties.load(reader)
}
}
def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
flutterVersionCode = '1'
}
def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
flutterVersionName = '1.0'
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
android {
aaptOptions {
noCompress 'tflite'
noCompress 'lite'
}
namespace "com.example.ml_app"
compileSdkVersion flutter.compileSdkVersion
ndkVersion flutter.ndkVersion
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.example.ml_app"
// You can update the following values to match your application needs.
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
minSdkVersion 19
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
buildTypes {
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.debug
}
}
}
flutter {
source '../..'
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
main.dart
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:tflite/tflite.dart';
import 'package:image_picker/image_picker.dart';
void main() => runApp(MaterialApp(
home: DetectMain(),
debugShowCheckedModeBanner: false,
));
class DetectMain extends StatefulWidget {
@override
_DetectMainState createState() => new _DetectMainState();
}
class _DetectMainState extends State<DetectMain> {
File? _image;
//double? _imageWidth;
//double? _imageHeight;
var _recognitions;
loadModel() async {
Tflite.close();
try {
String res;
res = (await Tflite.loadModel(
model: "assets/model_unquant.tflite",
labels: "assets/labels.txt",
))!;
print(res);
} on PlatformException {
print("Failed to load the model");
}
}
// run prediction using TFLite on given image
Future predict(File image) async {
var recognitions = await Tflite.runModelOnImage(
path: image.path, // 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
);
print(recognitions);
setState(() {
_recognitions = recognitions;
});
}
// send image to predict method selected from gallery or camera
sendImage(File image) async {
if (image == null) return;
await predict(image);
// get the width and height of selected image
FileImage(image)
.resolve(ImageConfiguration())
.addListener((ImageStreamListener((ImageInfo info, bool _) {
setState(() {
//_imageWidth = info.image.width.toDouble();
//_imageHeight = info.image.height.toDouble();
_image = image;
});
})));
}
// select image from gallery
selectFromGallery() async {
var image = await ImagePicker().pickImage(source: ImageSource.gallery);
if (image == null) return;
setState(() {});
sendImage(image as File);
}
// select image from camera
selectFromCamera() async {
var image = await ImagePicker().pickImage(source: ImageSource.camera);
if (image == null) return;
setState(() {});
sendImage(File(image.path));
}
// select image from camera
selectFromAsset() async {
var image = await ImagePicker().pickImage(source: ImageSource.camera);
if (image == null) return;
setState(() {});
sendImage(File(image.path));
}
@override
void initState() {
super.initState();
loadModel().then((val) {
setState(() {});
});
}
Widget printValue(rcg) {
if (rcg == null) {
return Text('',
style: TextStyle(fontSize: 30, fontWeight: FontWeight.w700));
} else if (rcg.isEmpty) {
return Center(
child: Text("Could not recognize",
style: TextStyle(fontSize: 25, fontWeight: FontWeight.w700)),
);
}
return Padding(
padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
child: Center(
child: Text(
"Prediction: " + _recognitions[0]['label'].toString().toUpperCase(),
style: TextStyle(fontSize: 20, fontWeight: FontWeight.w700),
),
),
);
}
// gets called every time the widget need to re-render or build
@override
Widget build(BuildContext context) {
// get the width and height of current screen the app is running on
return Scaffold(
appBar: AppBar(
iconTheme: IconThemeData(
color: Colors.black, //change your color here
),
title: Text(
"Flutter and TFLite",
style: TextStyle(color: Colors.white),
),
backgroundColor: Colors.blueGrey,
centerTitle: true,
),
body: ListView(
children: <Widget>[
Padding(
padding: EdgeInsets.fromLTRB(0, 30, 0, 30),
child: printValue(_recognitions),
),
Padding(
padding: EdgeInsets.fromLTRB(0, 0, 0, 10),
child: _image == null
? Center(
child: Text("Select image from camera or gallery"),
)
: Center(
child: Image.file(
_image!,
)),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.fromLTRB(0, 0, 20, 0),
child: Container(
height: 50,
width: 150,
color: Colors.redAccent,
child: TextButton(
onPressed: selectFromCamera,
child: Text(
"Camera",
style: TextStyle(color: Colors.white, fontSize: 20),
),
),
margin: EdgeInsets.fromLTRB(0, 20, 0, 10),
),
),
Container(
height: 50,
width: 150,
color: Colors.tealAccent,
child: TextButton.icon(
onPressed: selectFromGallery,
icon: Icon(
Icons.file_upload,
color: Colors.white,
size: 30,
),
label: Text(
"Gallery",
style: TextStyle(color: Colors.white, fontSize: 20),
),
),
margin: EdgeInsets.fromLTRB(0, 20, 0, 10),
),
],
),
],
));
}
}