flutter - Flutter TfLite 模型在每次热重载后仅发送一次输出,有时也是随机的
问题描述
我一直在尝试运行一个对不同食品进行分类的模型。该模型运行一次,但当我再次尝试时,它给出了错误。我之前曾在另一个有动画按钮的应用程序上尝试过 tflite,我只为按钮设置了动画控制器,并且该应用程序仍然有效,我无法找到我错的地方。
import 'dart:io';
import 'package:tflite/tflite.dart';
import 'package:flutter/material.dart';
import 'package:flutter_fashion/Constants/text_styles.dart';
import 'package:image_picker/image_picker.dart';
class HomeTab extends StatefulWidget {
const HomeTab({Key? key}) : super(key: key);
@override
_HomeTabState createState() => _HomeTabState();
}
class _HomeTabState extends State<HomeTab> {
bool _loading = true;
late File _image;
late List _output;
final picker = ImagePicker();
@override
void initState() {
super.initState();
loadModel().then((value) {
setState(() {});
});
}
@override
void dispose() {
super.dispose();
Tflite.close();
}
classifyImage(File image) async {
//this function runs the model on the image
var output = await Tflite.runModelOnImage(
path: image.path,
numResults: 36, //the amount of categories our neural network can predict
threshold: 0.5,
imageMean: 127.5,
imageStd: 127.5,
);
setState(() {
_output = output!;
_loading = false;
});
}
loadModel() async {
//this function loads our model
await Tflite.loadModel(
model: 'assets/model.tflite', labels: 'assets/labels.txt');
}
pickImage() async {
//this function to grab the image from camera
var image = await picker.getImage(source: ImageSource.camera);
setState(() {
if (image != null) {
_image = File(image.path);
} else {
print('No image selected.');
}
});
classifyImage(_image);
}
pickGalleryImage() async {
//this function to grab the image from gallery
var image = await picker.getImage(source: ImageSource.gallery);
setState(() {
if (image != null) {
_image = File(image.path);
} else {
print('No image selected.');
}
});
classifyImage(_image);
}
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
child: Container(
alignment: Alignment.center,
decoration: BoxDecoration(
color: Color(0xFFFFFFFF),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
child: Center(
child: _loading == true
? null //show nothing if no picture selected
: Container(
child: Column(
children: [
Container(
height: 250,
width: 250,
child: ClipRRect(
borderRadius: BorderRadius.circular(30),
child: Image.file(
_image,
fit: BoxFit.fill,
),
),
),
SizedBox(
height: 25,
),
if (_output == null)
Container()
else
Text(
'${_output[0]['label']}',
style: Constants.regularDarkText,
),
SizedBox(
height: 25,
),
],
),
),
),
),
Container(
child: Column(
children: [
GestureDetector(
onTap: pickImage,
child: Container(
width: MediaQuery.of(context).size.width - 200,
alignment: Alignment.center,
padding:
EdgeInsets.symmetric(horizontal: 24, vertical: 17),
decoration: BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.circular(15)),
child: Text(
'Take A Photo',
style: TextStyle(
fontFamily: "FiraSansCondensed",
fontSize: 18.0,
color: Colors.white,
fontWeight: FontWeight.w600,
),
),
),
),
SizedBox(
height: 30,
),
GestureDetector(
onTap: pickGalleryImage,
child: Container(
width: MediaQuery.of(context).size.width - 200,
alignment: Alignment.center,
padding:
EdgeInsets.symmetric(horizontal: 24, vertical: 17),
decoration: BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.circular(15)),
child: Text(
'Pick From Gallery',
style: TextStyle(fontFamily: "FiraSansCondensed",
fontSize: 18.0,
color: Colors.white,
fontWeight: FontWeight.w600,),
),
),
),
],
),
),
],
),
),
);
}
}
错误:
======== Exception caught by widgets library =======================================================
The following RangeError was thrown building HomeTab(dirty, dependencies: [MediaQuery], state: _HomeTabState#f9e09):
RangeError (index): Invalid value: Valid value range is empty: 0
The relevant error-causing widget was:
HomeTab file:///D:/Programming/Flutter/Flutter%20Fashion/flutter_fashion/lib/Views/home_page.dart:46:19
解决方案
推荐阅读
- r - 如何从一个长数据框中制作多个子图
- javascript - angular/rxjs- 以字符串形式发送的深度复制数据
- node.js - 如何使用节点根据日期范围查询 mongoDB?
- java - 如何使用 Jwt 验证端点并防止用户在 Spring Boot WebFlux 中不使用他们的数据
- java - XML字符串解析文档有0时它应该有一个更大的值
- java - MongoDB从Java将值插入数据库
- javascript - 用于显示从获取 API 对象生成的数字的 jQuery 中的计数器效果显示 isNaN 错误
- typescript - 从对象文字中查找打字稿
- python - 如何通过在 pandas.DataFrame 中逐行应用函数来生成多个新列?
- sql-server - T-SQL - 如果为真则运行 sp_send_dbmail 否则什么都不做?