Home > Blockchain >  Error :The argument type 'XFile' can't be assigned to the parameter type 'File&#
Error :The argument type 'XFile' can't be assigned to the parameter type 'File&#

Time:04-01

I am working on a Flutter app to take image from gallery and predict the appropriate output via detection using the model I trained using Machine learning but, I am getting an error for this following code:

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 error is:

The argument type 'XFile' can't be assigned to the parameter type 'File'.

I have tried all the other alternatives given out there for imagepicker issues faced by other people. Any help to solve this would be great! Thank you in advance!!

CodePudding user response:

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

CodePudding user response:

You are calling runModelOnImage which takes a File as an argument, with an 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);
}
  • Related