【发布时间】:2020-04-26 07:31:18
【问题描述】:
我正在尝试将 TFLite 模型加载到颤振中,但出现“加载模型失败”异常。我已经通过 yaml 加载了资产,导入了 TFLite 插件并确保文件路径是正确的,但我一直打印相同的异常。我已经用 python 测试了模型并且它可以工作,所以我现在只是想让它与颤振一起工作。
代码:
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';
import 'package:image/image.dart' as img;
void main() {
runApp(MyApp());
}
const String TF = "image_classifier";
//const String yolo = "Tiny YOLOv2";
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: TfliteHome(),
);
}
}
class TfliteHome extends StatefulWidget {
@override
_TfliteHomeState createState() => _TfliteHomeState();
}
class _TfliteHomeState extends State<TfliteHome> {
String _model = TF;
File _image;
double _imageWidth;
double _imageHeight;
bool _busy = false;
List _recognitions;
@override
void initState() {
super.initState();
_busy = true;
loadModel().then((val) {
setState(() {
_busy = false;
});
});
}
loadModel() async {
Tflite.close();
try {
String res;
res = await Tflite.loadModel(
model: "assets/image_classifier.tflite",
labels: "assets/image_labels.txt",
);
print(res);
} on PlatformException {
print("Failed to load the model");
}
}
selectFromImagePicker() async {
var image = await ImagePicker.pickImage(source: ImageSource.gallery);
if (image == null) return;
setState(() {
_busy = true;
});
predictImage(image);
}
predictImage(File image) async {
if (image == null) return;
if (_model == TF) {
await TFModel(image);
}
FileImage(image)
.resolve(ImageConfiguration())
.addListener((ImageStreamListener((ImageInfo info, bool _) {
setState(() {
_imageWidth = info.image.width.toDouble();
_imageHeight = info.image.height.toDouble();
});
})));
setState(() {
_image = image;
_busy = false;
});
}
TFModel(File image) async {
var recognitions = await Tflite.detectObjectOnImage(
path: image.path,
model: "image_classifier",
threshold: 0.3,
imageMean: 0.0,
imageStd: 255.0,
numResultsPerClass: 3);
setState(() {
_recognitions = recognitions;
});
}
List<Widget> renderBoxes(Size screen) {
if (_recognitions == null) return [];
if (_imageWidth == null || _imageHeight == null) return [];
double factorX = screen.width;
double factorY = _imageHeight / _imageHeight * screen.width;
Color blue = Colors.red;
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: 3,
)),
child: Text(
"${re["detectedClass"]} ${(re["confidenceInClass"] * 100).toStringAsFixed(0)}%",
style: TextStyle(
background: Paint()..color = blue,
color: Colors.white,
fontSize: 15,
),
),
),
);
}).toList();
}
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
List<Widget> stackChildren = [];
stackChildren.add(Positioned(
top: 0.0,
left: 0.0,
width: size.width,
child: _image == null ? Text("No Image Selected") : Image.file(_image),
));
stackChildren.addAll(renderBoxes(size));
if (_busy) {
stackChildren.add(Center(
child: CircularProgressIndicator(),
));
}
return Scaffold(
appBar: AppBar(
title: Text("TFLite Demo"),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.image),
tooltip: "Pick Image from gallery",
onPressed: selectFromImagePicker,
),
body: Stack(
children: stackChildren,
),
);
}
}
Pubspec.yaml:
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
assets:
- assets/image_classifier.tflite
- assets/image_labels.txt
【问题讨论】:
-
您是否将它们添加到 pubspec.yaml 中?在问题中包含资产 sn-p。
-
是的,我已将资产添加到 pubspec.yaml 中,并将该部分代码添加到我的原始帖子中
-
一行应该有一个缩进,上面写着
- assets/image_classifier.tflite和- assets/image_labels.txt。pubspec.yaml中是否存在缩进?因为在上面的代码中 sn -p 是不存在的。 -
我在 pubspec.yaml 中缩进了资产,但它仍然无法正常工作。它仍在打印加载模型失败。
-
您是否可以使用根包自己加载资产?只是为了检查它是否可用。
标签: tensorflow flutter