Tensorflow 向颤振项目发布装载资产
我试图将TFLite模型加载到颤振中,但出现异常“加载模型失败”。我已经通过yaml加载了资产,导入了TFLite插件,并确保文件路径正确,但我一直打印相同的异常。我已经用python测试了这个模型,它可以工作了,所以我现在只是想让它与颤振一起工作 代码:Tensorflow 向颤振项目发布装载资产,tensorflow,flutter,Tensorflow,Flutter,我试图将TFLite模型加载到颤振中,但出现异常“加载模型失败”。我已经通过yaml加载了资产,导入了TFLite插件,并确保文件路径正确,但我一直打印相同的异常。我已经用python测试了这个模型,它可以工作了,所以我现在只是想让它与颤振一起工作 代码: import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:tflite
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,
),
);
}
}
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
文件路径:
安卓
在android/app/build.gradle中,在android块中添加以下设置
aaptOptions {
noCompress 'tflite'
noCompress 'lite'
}
为我工作 Android
在android/app/build.gradle中,在android块中添加以下设置
aaptOptions {
noCompress 'tflite'
noCompress 'lite'
}
为我工作 你把它们添加到pubspec.yaml了吗?包括问题中的资产片段。是的,我已经将资产添加到pubspec.yaml中,并且我已经将这部分代码添加到我的原始帖子中。应该有一行缩进,上面写着
-assets/image\u classifier.tflite
和-assets/image\u labels.txt
。缩进是否存在于pubspec.yaml
中?因为在上面的代码片段中它不存在。我在pubspec.yaml中缩进了资产,但它仍然不起作用。它仍在打印。加载模型失败。您是否能够使用根捆绑自己加载资产?只是为了检查它是否可用。您是否将它们添加到pubspec.yaml?包括问题中的资产片段。是的,我已经将资产添加到pubspec.yaml中,并且我已经将这部分代码添加到我的原始帖子中。应该有一行缩进,上面写着-assets/image\u classifier.tflite
和-assets/image\u labels.txt
。缩进是否存在于pubspec.yaml
中?因为在上面的代码片段中它不存在。我在pubspec.yaml中缩进了资产,但它仍然不起作用。它仍在打印。加载模型失败。您是否能够使用根捆绑自己加载资产?只是为了检查它是否可用。