【问题标题】:type 'String' is not a subtype of type 'int' of 'index' while using tflite - flutter使用 tflite 时,类型“String”不是“index”类型“int”的子类型 - 颤振
【发布时间】:2021-05-07 07:15:07
【问题描述】:

我在我的应用程序中使用tflite 包,在程序的一部分中,我收到type 'String' is not a subtype of type 'int' of 'index' 的错误。我将单独提及该行,并将在代码中提供行号。

现在,在那个特定的行,当我写_output[0]['label'] 时,会显示这个错误。但是如果我写了_output[0][0],错误就会消失,但会显示一个随机文本。现在,我该如何纠正它?

首先,代码-

import 'dart:io';

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

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {

  bool _isLoading=true;
  File _image = File("");
  List _output = [];
  final picker = ImagePicker();

  @override
  void initState() {
    super.initState();
    loadModel().then((value) {
      setState(() {

      });
    });
  }

  detectImage(File image) async {
    var output = await Tflite.runModelOnImage(
      path: image.path,
      numResults: 2,
      threshold: 0.6,
      imageMean: 127.5,
      imageStd: 127.5,
    );
    setState(() {
      _output.add(output);
      _isLoading = false;
    });
    output!=null ? output.clear() : null;
  }

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

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();

  }

  pickImage() async {
    var image = await picker.getImage(source: ImageSource.camera);
    if(image == null)
      return null;

    setState(() {
      _image = File(image.path);
    });

    detectImage(_image);
  }

  pickGalleryImage() async {
    var image = await picker.getImage(source: ImageSource.gallery);
    if(image == null)
      return null;

    setState(() {
      _image = File(image.path);
    });

    detectImage(_image);
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        backgroundColor: Colors.grey[400],
        body: Center(
          child: Container(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                SizedBox(height: 20.0,),
                Center(
                  child: Text(
                    'Cats and Dogs Detector app',
                    style: TextStyle(
                      fontSize: 25.0,
                      fontWeight: FontWeight.bold,
                      color: Colors.white70,
                    ),
                  ),
                ),
                SizedBox(height: 200.0),
                Center(
                  child: _isLoading ? Container(
                    width: MediaQuery.of(context).size.width*0.9,
                    child: Column(
                      children: [
                        Image.asset("assets/cats n dogs 2.jpg", fit: BoxFit.cover,)
                      ],
                    ),
                  ) : Container(
                    child: Column(
                      children: [
                        Container(
                          height: 250,
                          child: Image.file(_image),
                        ),
                        SizedBox(height: 20.0,),
                        _output!=null ? Text('${_output[0]['label']}', style: TextStyle(color: Colors.white, fontSize: 15.0),) : Container(),
                        SizedBox(height: 10.0,),
                      ],
                    ),
                  ),
                ),
                SizedBox(
                  height: 20.0,
                ),
                Center(
                  child: Container(
                    width: MediaQuery.of(context).size.width*0.6,
                    alignment: Alignment.center,
                    child: Column(
                      children: [
                        ElevatedButton(
                          onPressed: () {
                            pickImage();
                          },
                          style: ButtonStyle(
                            backgroundColor: MaterialStateProperty.all(Colors.grey),
                          ),
                          child: Padding(
                            padding: const EdgeInsets.symmetric(horizontal: 30, vertical: 15.0),
                            child: Text(
                              'Capture a pic',
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 17.5,
                              ),
                            ),
                          ),
                        ),
                        SizedBox(
                          height: MediaQuery.of(context).size.height*0.01,
                        ),
                        ElevatedButton(
                          onPressed: () {
                            pickGalleryImage();
                          },
                          style: ButtonStyle(
                            backgroundColor: MaterialStateProperty.all(Colors.grey),
                          ),
                          child: Padding(
                            padding: const EdgeInsets.symmetric(horizontal: 12.0, vertical: 15.0),
                            child: Text(
                              'Select from gallery',
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 17.50,
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

然后是错误/异常-

The following _TypeError was thrown building Home(dirty, dependencies: [MediaQuery], state: _HomeState#17a68):
type 'String' is not a subtype of type 'int' of 'index'

The relevant error-causing widget was: 
  Home file:///C:/Users/Hp/AndroidStudioProjects/dog_cat_classification/lib/main.dart:15:13
When the exception was thrown, this was the stack: 
#0      _HomeState.build (package:dog_cat_classification/home.dart:120:59)
#1      StatefulElement.build (package:flutter/src/widgets/framework.dart:4691:27)
#2      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4574:15)
#3      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4746:11)
#4      Element.rebuild (package:flutter/src/widgets/framework.dart:4267:5)
...
====================================================================================================
Reloaded 1 of 569 libraries in 1,022ms.
Lost connection to device.

行号 120 - _output!=null ? Text('${_output[0]['label']}', style: TextStyle(color: Colors.white, fontSize: 15.0),) : Container(),

【问题讨论】:

    标签: android flutter tensorflow dart machine-learning


    【解决方案1】:

    请帮助我了解您在此处为 _output 分配了值,但您尝试访问第 122 行中的“标签”,但没有为列表分配标签属性。

    这是正确的还是我误解了什么。

      detectImage(File image) async {
        var output = await Tflite.runModelOnImage(
          path: image.path,
          numResults: 2,
          threshold: 0.6,
          imageMean: 127.5,
          imageStd: 127.5,
        );
        setState(() {
          _output.add(output);
          _isLoading = false;
        });
        output!=null ? output.clear() : null;
      }
    

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 2020-02-29
      • 2021-02-25
      • 1970-01-01
      • 1970-01-01
      • 2019-04-24
      • 1970-01-01
      • 1970-01-01
      • 2021-03-29
      相关资源
      最近更新 更多