【问题标题】:Error :The argument type 'XFile' can't be assigned to the parameter type 'File'错误:无法将参数类型 \'XFile\' 分配给参数类型 \'File\'
【发布时间】: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


【解决方案1】:

您正在调用runModelOnImage,它将File 作为参数,并带有XFile

chooseImage() async {
  final image = await ImagePicker().pickImage(source: ImageSource.gallery);
  if (image == null) return null;
  setState(() {
    _isLoading = true;
    _image = File(image.path);
  });
  runModelOnImage(_image);
}

【讨论】:

    【解决方案2】:
    chooseImage() async {
        final image = await ImagePicker().pickImage(source: ImageSource.gallery);
        if (image == null) return null;
        setState(() {
          _isLoading = true;
          _image = File(image.path);
        });
       runModelOnImage(_image);
    }
    

    【讨论】:

    • 还是一样的错误
    • 将 runModelOnImage(image) 更改为 runModelOnImage(_image) 因为您必须通过 runModelOnImage 接受 File 而不是 xFile
    【解决方案3】:

    我面临着同样的问题。我所做的是:

    • 在我的小部件XFile? _image; 中创建了一个 XFile 变量 看看这个功能
    Future getImageFromGallery() async {
        _image = await ImagePicker()
            .pickImage(source: ImageSource.gallery, maxHeight: 300, maxWidth: 300);
        if (_image!.path.isNotEmpty) {
          setState(() {
            pickedImage = true;
          });
        }
      }
    
    • 按一个按钮,调用上述函数。

    • 使用以下功能将文件上传到 Firebase 存储
    
      Future uploadLogo(BuildContext? context, XFile? image) async {
          FirebaseStorage storage = FirebaseStorage.instance;
          Reference ref = storage
              .ref()
              .child('shops/${_name.text}/Logo${DateTime.now().toString()}');
    
    
          final path = image!.path; //Getting the path of XFile
          File file = File(path);// Turning that into File
          UploadTask uploadTask = ref
              .putFile(file); //Getting a proper reference to upload on storage
    
          final TaskSnapshot downloadUrl = (await uploadTask); //Uploading to //storage
    
          imageUrlShop = await downloadUrl.ref.getDownloadURL();
      }
    

    我的答案与前面的答案几乎相同,但格式和参数的传递方式略有变化。这对我有用。也可能对你有用...

    【讨论】:

      猜你喜欢
      • 2021-07-09
      • 2021-08-22
      • 2021-11-03
      • 2022-11-23
      • 2022-12-24
      • 2020-11-20
      • 2021-08-26
      • 2021-08-30
      • 1970-01-01
      相关资源
      最近更新 更多