tflite_flutter_helper_plus 0.0.2
tflite_flutter_helper_plus: ^0.0.2 copied to clipboard
Easy, fast processing and manipulation input and output of TensorFlow Lite Models.
example/lib/main.dart
import 'dart:io';
import 'package:flutter/services.dart';
import 'package:image/image.dart' as img;
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:imageclassification/classifier.dart';
import 'package:imageclassification/classifier_quant.dart';
import 'package:logger/logger.dart';
import 'package:tflite_flutter_helper_plus/tflite_flutter_helper_plus.dart';
import 'package:permission_handler/permission_handler.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Image Classification',
theme: ThemeData(
primarySwatch: Colors.orange,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, this.title}) : super(key: key);
final String? title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late Classifier _classifier;
var logger = Logger();
File? _image;
final picker = ImagePicker();
Image? _imageWidget;
img.Image? fox;
Category? category;
@override
void initState() {
super.initState();
_classifier = ClassifierQuant();
}
Future getImage() async {
var status = await Permission.photos.status;
if (!status.isGranted) {
// Ask for permission to access photos
status = await Permission.photos.request();
}
if (!status.isGranted) {
// Permission not granted, handle error
throw PlatformException(code: 'photo_access_denied', message: 'The user did not allow photo access.');
}
print("status.isGranted");
print(status.isGranted);
if (status.isGranted) {
final pickedFile = await picker.getImage(source: ImageSource.gallery);
setState(() {
_image = File(pickedFile!.path);
_imageWidget = Image.file(_image!);
_predict();
});
}
}
void _predict() async {
img.Image imageInput = img.decodeImage(_image!.readAsBytesSync())!;
var pred = _classifier.predict(imageInput);
setState(() {
this.category = pred;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('TfLite Flutter Helper',
style: TextStyle(color: Colors.white)),
),
body: Column(
children: <Widget>[
Center(
child: _image == null
? Text('No image selected.')
: Container(
constraints: BoxConstraints(
maxHeight: MediaQuery.of(context).size.height / 2),
decoration: BoxDecoration(
border: Border.all(),
),
child: _imageWidget,
),
),
SizedBox(
height: 36,
),
Text(
category != null ? category!.label : '',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.w600),
),
SizedBox(
height: 8,
),
Text(
category != null
? 'Confidence: ${category!.score.toStringAsFixed(3)}'
: '',
style: TextStyle(fontSize: 16),
),
],
),
floatingActionButton: FloatingActionButton(
onPressed: getImage,
tooltip: 'Pick Image',
child: Icon(Icons.add_a_photo),
),
);
}
}