0

enter image description hereI am working on a flatter app to classify plant diseases using deep learning. When I take a picture to classify the disease, it appears with an error because the model I trained deals with 4D image, and the image I took is 3D. Is there a suggestion to solve this problem?

type here
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart';
import 'package:tflite/tflite.dart';


class UI extends StatefulWidget {
  const UI({Key? key}) : super(key: key);

  @override
  _UIState createState() => _UIState();
}

class _UIState extends State<UI> {
  List? _outputs;
  XFile? _image;
  bool _loading = false;

  final ImagePicker _picker = ImagePicker();
  @override
  void initState() {
    super.initState();
    _loading = true;

    loadModel().then((value) {
      setState(() {
        _loading = false;
      });
    });
  }

  loadModel() async {
    await Tflite.loadModel(
      model: "assets/model_unquant.tflite",
      labels: "assets/labels.txt",
      numThreads: 1,
    );
  }

  classifyImage(File image) async {
    var output = await Tflite.runModelOnImage(
        path: image.path,
        imageMean: 0.0,
        imageStd: 255.0,
        numResults: 10,
        threshold: 0.2,
        asynch: true);
    setState(() {
      _loading = false;
      _outputs = output;
    });
  }
  @override
  void dispose() {
    Tflite.close();
    super.dispose();
  }
  Future getImageCamera() async {
    var image =
        await _picker.pickImage(source: ImageSource.camera, imageQuality: 50);

    if (image == null) return null;
    setState(() {
      _loading = true;

      _image = image;
      
    });
    classifyImage(File(_image!.path));
   
  }

  Future getImageGallery() async {
    var image =
        await _picker.pickImage(source: ImageSource.gallery, imageQuality: 50);
    if (image == null) return null;
    setState(() {
      _loading = true;
      _image = image;
    });
    classifyImage(File(_image!.path));
  }

  @override
  Widget build(BuildContext context) {
    SystemChrome.setEnabledSystemUIOverlays([]);
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text('Crop diseases'),
        actions: [
                            IconButton(
                                onPressed: () {
                                  getImageCamera();
                                  
                                },
                                icon: const Icon(
                                  Icons.camera_alt,
                                  color: Colors.white,
                                )),
                            IconButton(
                                onPressed: () {
                                  getImageGallery();
                                },
                                icon: const Icon(
                                  Icons.image,
                                  color: Colors.white,
                                ))
                          ],
        

      ),
      body: SafeArea(
        child: SingleChildScrollView(
          child: SizedBox(
            height: MediaQuery.of(context).size.height,
            width: MediaQuery.of(context).size.width,
            child: Column(
              children: [

                Expanded(
                  flex: 9,
                  child: _image == null
                      ? Container(
                          margin: const EdgeInsets.all(10),
                          decoration: const BoxDecoration(
                            color:Colors.white,
                            borderRadius:
                                BorderRadius.all(Radius.circular(25.0)),
                          ),
                          child: Align(
                            alignment: Alignment.bottomCenter,
                            child: Container(
                              margin: const EdgeInsets.all(20),
                              decoration: BoxDecoration(
                                  color: Colors.black26,
                                  borderRadius: BorderRadius.circular(15)),
                              padding: const EdgeInsets.all(20),
                              child: const Text(
                                "Upload an image",
                                style: TextStyle(
                                    fontSize: 20,
                                    fontWeight: FontWeight.w600,
                                    color: Colors.white),
                              ),
                            ),
                          ),
                        )
                      : Container(
                          margin: const EdgeInsets.all(10),
                          decoration: BoxDecoration(
                            image: DecorationImage(
                                image: FileImage(File(_image!.path)),
                                fit: BoxFit.cover),
                            color: Colors.transparent,
                            borderRadius:
                                const BorderRadius.all(Radius.circular(25.0)),
                          ),
                          child: Align(
                            alignment: Alignment.bottomCenter,
                            child: Container(
                              margin: const EdgeInsets.all(20),
                              decoration: BoxDecoration(
                                  color: Colors.black26,
                                  borderRadius: BorderRadius.circular(15)),
                              padding: const EdgeInsets.all(20),
                              child: Text(
                                _outputs?[0]["label"] ?? "",
                                style: const TextStyle(
                                    fontSize: 20,
                                    fontWeight: FontWeight.w600,
                                    color: Colors.white),
                              ),
                            ),
                          ),
                        ),
                ),
                Expanded(
                  flex: 2,
                  child: Card(
                    margin: const EdgeInsets.all(0),
                    //color: const Color(0xFFD4DCFF),
                    shape: const RoundedRectangleBorder(
                      borderRadius: BorderRadius.only(
                        topRight: Radius.circular(15.0),
                        topLeft: Radius.circular(15.0),
                      ),
                    ),
                    child: Container(
                        width: double.infinity,
                        decoration: const BoxDecoration(
                          borderRadius: BorderRadius.only(
                            topRight: Radius.circular(15.0),
                            topLeft: Radius.circular(15.0),
                          ),
                        ),
                        //  child: Row(
                        //   mainAxisAlignment: MainAxisAlignment.center,

                        //   children: [
                        //     Container(
                        //       margin: const EdgeInsets.all(10),
                        //       decoration: const BoxDecoration(
                        //         color: Color(0xFF65708F),
                        //         shape: BoxShape.circle,
                        //       ),
                        //       child: IconButton(
                        //           onPressed: () {
                        //             getImageCamera();
                        //           },
                        //           icon: const Icon(
                        //             Icons.camera_alt,
                        //             color: Colors.white,
                        //           )),
                        //     ),
                        //     Container(
                        //       margin: const EdgeInsets.all(10),
                        //       decoration: const BoxDecoration(
                        //         color: Color(0xFF65708F),
                        //         shape: BoxShape.circle,
                        //       ),
                        //       child: IconButton(
                        //           onPressed: () {
                        //             getImageGallery();
                        //           },
                        //           icon: const Icon(
                        //             Icons.image,
                        //             color: Colors.white,
                        //           )),
                        //     )
                        //   ],
                        // )
                        ),
                  ),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}
[![[![[![enter image description here](https://i.stack.imgur.com/9bYdu.jpg)](https://i.stack.imgur.com/9bYdu.jpg)](https://i.stack.imgur.com/gzexM.jpg)](https://i.stack.imgur.com/gzexM.jpg)](https://i.stack.imgur.com/BnjYt.jpg)]

how can i reshape this image?

  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Nov 08 '22 at 20:19

0 Answers0