【问题标题】:Flutter can't get the output image nor labels to display? What am I missing?Flutter 无法获取输出图像或标签显示?我错过了什么?
【发布时间】:2021-05-16 19:31:17
【问题描述】:

正如标题所说,我无法显示输出?我不确定我错过了什么?是否会弹出任何错误并正常运行?无论我选择画廊图像还是相机图像时,它都不会只显示输出的主要机器人面,即 jpg 文件?我已经检查并尝试了所有我能想到的改变,但我觉得我错过了一些明显的东西。 我家.dart:

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


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

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

class _HomeState extends State<Home> {
  bool _loading = true;
  File _image;
  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: 128,
        imageStd: 128
    );
    setState(() {
      _output = output;
      _loading = false;

    });
  }
  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 Scaffold(
      backgroundColor: Colors.lightBlue,
      body: Container(
        padding: EdgeInsets.symmetric(horizontal: 24),
        child: Column(crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          SizedBox(height: 50),
          Text('Coding bliss',
          style: TextStyle(color: Colors.yellowAccent,
              fontSize: 26 ),
          ),
          SizedBox(height: 5),
          Text('Machine eyes learn the difference!',
            style: TextStyle(color: Colors.white,
              fontWeight: FontWeight.w400,
          fontSize: 28),
          ),
          SizedBox(height: 50),
          Center(child: _loading ?
          Container(
            width: 350,
            child: Column(children: <Widget>[
              Image.asset('assets/bot.jpg'),
              SizedBox(height: 50),
            ],),
          ) : Container(
            child: Column(children: <Widget>[
              Container(
                height: 250,
                  child: Image.file(_image),
              ),
              SizedBox(height: 20),
              _output != null
                  ? Text("${_output[0]['label']}",
                style: TextStyle(
                    color: Colors.white,
                    fontSize: 15
                ),
              )
                  : Container(),
              SizedBox(height: 10),
            ],),
          ),
          ),
          Container(
            width: MediaQuery.of(context).size.width,
          child: Column(
            children: <Widget>[
            GestureDetector(
              onTap: (){
                pickImage();
              },
            child: Container(
              width: MediaQuery.of(context).size.width - 250,
              alignment: Alignment.center,
              padding: EdgeInsets.symmetric(horizontal: 10, vertical: 18),
              decoration: BoxDecoration(
                color: Colors.amberAccent,
              borderRadius: BorderRadius.circular(6),
              ),
            child: Text("Capture photo",
              style: TextStyle(color: Colors.black),),
            ),
            ),
              SizedBox(height: 15),
              GestureDetector(
                onTap: (){

                  pickGalleryImage();

                },
                child: Container(
                  width: MediaQuery.of(context).size.width - 250,
                  alignment: Alignment.center,
                  padding: EdgeInsets.symmetric(horizontal: 10, vertical: 18),
                  decoration: BoxDecoration(color: Colors.amberAccent,
                    borderRadius: BorderRadius.circular(6),
                  ),
                  child: Text("Select a photo",
                    style: TextStyle(color: Colors.black), ),
                ),
              ),
          ],)
            ,)
        ],),
      ),
    );
  }
}

我的 build.gradle:

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new FileNotFoundException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 30

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
    aaptOptions {
        noCompress 'tflite'
        noCompress 'lite'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.vonte.dogcatlabel.dog_cat_labels"
        minSdkVersion 19
        targetSdkVersion 30
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

我的 main.dart

import 'package:flutter/material.dart';
import 'package:dog_cat_labels/splashscreen.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Cat or Dog Classifier',
      home: MySplash(),
      debugShowCheckedModeBanner: false,
    );
  }
}

Info.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>$(DEVELOPMENT_LANGUAGE)</string>
    <key>CFBundleExecutable</key>
    <string>$(EXECUTABLE_NAME)</string>
    <key>CFBundleIdentifier</key>
    <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>dog_cat_labels</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>$(FLUTTER_BUILD_NAME)</string>
    <key>CFBundleSignature</key>
    <string>????</string>
    <key>CFBundleVersion</key>
    <string>$(FLUTTER_BUILD_NUMBER)</string>
    <key>LSRequiresIPhoneOS</key>
    <true/>
    <key>UILaunchStoryboardName</key>
    <string>LaunchScreen</string>
    <key>UIMainStoryboardFile</key>
    <string>Main</string>
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>UISupportedInterfaceOrientations~ipad</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationPortraitUpsideDown</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>UIViewControllerBasedStatusBarAppearance</key>
    <false/>
    <key>NSPhotoLibraryUsageDescription</key>
        <string>Photo Required</string>
    <key>NSCameraUsageDescription</key>
        <string>Photo Required</string>
    <key>NSMicrophoneUsageDescription</key>
        <string>Photo Required</string>
</dict>
</plist>

有什么推荐吗?

【问题讨论】:

  • 我强烈怀疑这与 android studio 有什么关系,因为这只是您用来构建应用程序的 IDE,所以我认为您不需要使用标签或参考你的头衔
  • 改变了!不知道我做错了什么?一切顺利,直到显示输出
  • 不幸的是,我对颤振知之甚少,无法真正提供帮助,只是想确保您使用正确的标签,以便最终得到答案

标签: java android flutter


【解决方案1】:

所以我通过一些激烈的挖掘发现了它。它位于 build.gradle 文件的第 38 行。minSdkVersion 设置为 19。它需要是 21 版。现在效果惊人!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-13
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多