tflite 1.0.1 tflite: ^1.0.1 copied to clipboard
A Flutter plugin for accessing TensorFlow Lite. Supports both iOS and Android.
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:image/image.dart' as img;
import 'package:tflite/tflite.dart';
import 'package:image_picker/image_picker.dart';
void main() => runApp(new App());
const String mobile = "MobileNet";
const String ssd = "SSD MobileNet";
const String yolo = "Tiny YOLOv2";
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyApp(),
);
}
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
File _image;
List _recognitions;
String _model = "";
double _imageHeight;
double _imageWidth;
Future getImage() async {
var image = await ImagePicker.pickImage(source: ImageSource.gallery);
switch (_model) {
case yolo:
yolov2Tiny(image);
break;
case ssd:
ssdMobileNet(image);
break;
default:
recognizeImage(image);
// recognizeImageBinary(image);
}
new FileImage(image)
.resolve(new ImageConfiguration())
.addListener((ImageInfo info, bool _) {
setState(() {
_imageHeight = info.image.height.toDouble();
_imageWidth = info.image.width.toDouble();
});
});
setState(() {
_image = image;
});
}
@override
void initState() {
super.initState();
}
Future loadModel() async {
try {
String res;
switch (_model) {
case yolo:
res = await Tflite.loadModel(
model: "assets/yolov2_tiny.tflite",
labels: "assets/yolov2_tiny.txt",
);
break;
case ssd:
res = await Tflite.loadModel(
model: "assets/ssd_mobilenet.tflite",
labels: "assets/ssd_mobilenet.txt");
break;
default:
res = await Tflite.loadModel(
model: "assets/mobilenet_v1_1.0_224.tflite",
labels: "assets/mobilenet_v1_1.0_224.txt",
);
}
print(res);
} on PlatformException {
print('Failed to load model.');
}
}
Uint8List imageToByteListFloat32(
img.Image image, int inputSize, double mean, double std) {
var convertedBytes = Float32List(1 * inputSize * inputSize * 3);
var buffer = Float32List.view(convertedBytes.buffer);
int pixelIndex = 0;
for (var i = 0; i < inputSize; i++) {
for (var j = 0; j < inputSize; j++) {
var pixel = image.getPixel(j, i);
buffer[pixelIndex++] = (img.getRed(pixel) - mean) / std;
buffer[pixelIndex++] = (img.getGreen(pixel) - mean) / std;
buffer[pixelIndex++] = (img.getBlue(pixel) - mean) / std;
}
}
return convertedBytes.buffer.asUint8List();
}
Uint8List imageToByteListUint8(img.Image image, int inputSize) {
var convertedBytes = Uint8List(1 * inputSize * inputSize * 3);
var buffer = Uint8List.view(convertedBytes.buffer);
int pixelIndex = 0;
for (var i = 0; i < inputSize; i++) {
for (var j = 0; j < inputSize; j++) {
var pixel = image.getPixel(j, i);
buffer[pixelIndex++] = img.getRed(pixel);
buffer[pixelIndex++] = img.getGreen(pixel);
buffer[pixelIndex++] = img.getBlue(pixel);
}
}
return convertedBytes.buffer.asUint8List();
}
Future recognizeImage(File image) async {
var recognitions = await Tflite.runModelOnImage(
path: image.path,
numResults: 6,
threshold: 0.05,
imageMean: 127.5,
imageStd: 127.5,
);
setState(() {
_recognitions = recognitions;
});
}
Future recognizeImageBinary(File image) async {
var imageBytes = (await rootBundle.load(image.path)).buffer;
img.Image oriImage = img.decodeJpg(imageBytes.asUint8List());
img.Image resizedImage = img.copyResize(oriImage, 224, 224);
var recognitions = await Tflite.runModelOnBinary(
binary: imageToByteListFloat32(resizedImage, 224, 127.5, 127.5),
numResults: 6,
threshold: 0.05,
);
setState(() {
_recognitions = recognitions;
});
}
Future yolov2Tiny(File image) async {
var recognitions = await Tflite.detectObjectOnImage(
path: image.path,
model: "YOLO",
threshold: 0.3,
imageMean: 0.0,
imageStd: 255.0,
numResultsPerClass: 1,
);
// var imageBytes = (await rootBundle.load(image.path)).buffer;
// img.Image oriImage = img.decodeJpg(imageBytes.asUint8List());
// img.Image resizedImage = img.copyResize(oriImage, 416, 416);
// var recognitions = await Tflite.detectObjectOnBinary(
// binary: imageToByteListFloat32(resizedImage, 416, 0.0, 255.0),
// model: "YOLO",
// threshold: 0.3,
// numResultsPerClass: 1,
// );
setState(() {
_recognitions = recognitions;
});
}
Future ssdMobileNet(File image) async {
var recognitions = await Tflite.detectObjectOnImage(
path: image.path,
numResultsPerClass: 1,
);
// var imageBytes = (await rootBundle.load(image.path)).buffer;
// img.Image oriImage = img.decodeJpg(imageBytes.asUint8List());
// img.Image resizedImage = img.copyResize(oriImage, 300, 300);
// var recognitions = await Tflite.detectObjectOnBinary(
// binary: imageToByteListUint8(resizedImage, 300),
// numResultsPerClass: 1,
// );
setState(() {
_recognitions = recognitions;
});
}
onSelect(model) {
setState(() {
_model = model;
});
loadModel();
}
List<Widget> renderBoxes(Size screen) {
if (_recognitions == null) return [];
double factorX = screen.width;
double factorY = _imageHeight / _imageWidth * screen.width;
Color blue = Color.fromRGBO(37, 213, 253, 1.0);
return _recognitions.map((re) {
return Positioned(
left: re["rect"]["x"] * factorX,
top: re["rect"]["y"] * factorY,
width: re["rect"]["w"] * factorX,
height: re["rect"]["h"] * factorY,
child: Container(
decoration: BoxDecoration(
border: Border.all(
color: blue,
width: 2,
),
),
child: Text(
"${re["detectedClass"]} ${(re["confidenceInClass"] * 100).toStringAsFixed(0)}%",
style: TextStyle(
background: Paint()..color = blue,
color: Colors.white,
fontSize: 12.0,
),
),
),
);
}).toList();
}
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Scaffold(
appBar: AppBar(
title: const Text('tflite example app'),
),
body: _model == ""
? Center(
child: Column(
children: <Widget>[
RaisedButton(
child: const Text(mobile),
onPressed: () => onSelect(mobile),
),
RaisedButton(
child: const Text(ssd),
onPressed: () => onSelect(ssd),
),
RaisedButton(
child: const Text(yolo),
onPressed: () => onSelect(yolo),
),
],
),
)
: Stack(
children: <Widget>[
Container(
child: _image == null
? Text('No image selected.')
: Image.file(_image),
),
_model == mobile
? Center(
child: Column(
children: _recognitions != null
? _recognitions.map((res) {
return Text(
"${res["index"]} - ${res["label"]}: ${res["confidence"].toString()}",
style: TextStyle(
color: Colors.black,
fontSize: 20.0,
background: Paint()..color = Colors.white,
),
);
}).toList()
: [],
),
)
: Stack(children: renderBoxes(size)),
],
),
floatingActionButton: FloatingActionButton(
onPressed: getImage,
tooltip: 'Pick Image',
child: Icon(Icons.image),
),
);
}
}