【发布时间】:2022-08-14 04:58:53
【问题描述】:
我正在开发一个 Flutter 应用程序以从图库中获取图像,并使用我使用机器学习训练的模型通过检测来预测适当的输出,但是,我收到以下代码的错误:
import \'dart:io\';
import \'package:flutter/material.dart\';
import \'package:image_picker/image_picker.dart\';
import \'package:tflite/tflite.dart\';
void main() {
runApp(MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData.dark(),
home: HomePage(),
));
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
late bool _isLoading;
late File _image;
late List _output;
@override
void initState() {
// TODO: implement initState
super.initState();
_isLoading = true;
loadMLModel().then((value){
setState(() {
_isLoading = false;
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(\"Brain Tumor Detection\"),
),
body: _isLoading ? Container(
alignment: Alignment.center,
child: CircularProgressIndicator(),
) : SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
_image == null ? Container() : Image.file(File(_image.path)),
SizedBox(height: 16,),
_output == null ? Text(\"\"): Text(
\"${_output[0][\"label\"]}\"
)
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
chooseImage();
},
child: Icon(
Icons.image
),
)
,
);
}
chooseImage() async {
final image = await ImagePicker().pickImage(source: ImageSource.gallery);
if (image == null) return null;
setState(() {
_isLoading = true;
_image = image as File;
});
runModelOnImage(image);
}
runModelOnImage(File image) async{
var output = await Tflite.runModelOnImage(
path: image.path,
numResults: 2,
imageMean: 127.5,
imageStd: 127.5,
threshold: 0.5
);
setState(() {
_isLoading = false;
_output = output!;
});
}
loadMLModel() async {
await Tflite.loadModel(
model: \"assets/btc.tflite\",
labels: \"assets/labels.txt\"
);
}
}
错误是:
The argument type \'XFile\' can\'t be assigned to the parameter type \'File\'.
对于其他人面临的图像选择器问题,我已经尝试了所有其他替代方案。解决这个问题的任何帮助都会很棒! 先感谢您!!
标签: flutter tensorflow dart imagepicker