Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dart 带ML套件的计算机视觉-聚焦颤振_Dart_Flutter_Firebase Mlkit - Fatal编程技术网

Dart 带ML套件的计算机视觉-聚焦颤振

Dart 带ML套件的计算机视觉-聚焦颤振,dart,flutter,firebase-mlkit,Dart,Flutter,Firebase Mlkit,我正在努力学习教程,在那里我一步一步地学习教程,但仍然没有成功 我的代码如下: import 'package:flutter/material.dart'; import 'dart:io'; import 'dart:async'; import 'package:image_picker/image_picker.dart'; import 'package:firebase_ml_vision/firebase_ml_vision.dart'; void main() => ru

我正在努力学习教程,在那里我一步一步地学习教程,但仍然没有成功

我的代码如下:

import 'package:flutter/material.dart';
import 'dart:io';
import 'dart:async';
import 'package:image_picker/image_picker.dart';
import 'package:firebase_ml_vision/firebase_ml_vision.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: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: FacePage(),
    );
  }
}


class FacePage extends StatefulWidget{
  @override
  createState() => _FacePageState();
}

class _FacePageState extends State<FacePage>{
  File _imageFile;
  List<Face> _faces;


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Face Detector'),
      ),

      body: ImageAndFaces(),

      floatingActionButton: FloatingActionButton(
        onPressed: _getImageAndDetectFace,
        tooltip: 'Pick an Image',
        child: Icon(Icons.add_a_photo),
      ),
    );
  }

  void _getImageAndDetectFace() async {
    final imageFile = await ImagePicker.pickImage(
        source: ImageSource.gallery,
    );

    final image = FirebaseVisionImage.fromFile(imageFile);

    final faceDetector = FirebaseVision.instance.faceDetector(
      FaceDetectorOptions(
        mode: FaceDetectorMode.accurate,
        enableLandmarks: true,
      ),
    );
    List<Face> faces = await faceDetector.detectInImage(image);

    if(mounted) {
      setState(() {
        _imageFile = imageFile;
        _faces = faces;
      });
    }
  }
}

class ImageAndFaces extends StatelessWidget {
  ImageAndFaces({this.imageFile, this.faces});
  final File imageFile;
  final List<Face> faces;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Flexible(
          flex: 2 ,
          child: Container(
            constraints: BoxConstraints.expand(),
            child: Image.file(imageFile, fit: BoxFit.cover),
            ),
        ),
        Flexible(flex: 1 ,
            child: ListView(
              children: faces.map<Widget>((f) => FaceCoordinates(f)).toList(),
            ),
        ),
      ],
    );
  }
}

class FaceCoordinates extends StatelessWidget {
  FaceCoordinates(this.face);
  final Face face;

  @override
  Widget build(BuildContext context) {
    final pos = face.boundingBox;
    return ListTile(
      title: Text('(${pos.top}, ${pos.left}, ${pos.bottom}, ${pos.right})'),
    );
  }
}
有人知道问题出在哪里吗? 我尝试了我能想到的任何方法,包括尝试在“ImageAndFaces”类构造函数创建实例时捕获它,但没有成功。 我对飞镖和飞镖还不熟悉,所以也许这是个愚蠢的错误


非常感谢

出现问题的原因是imageFile以null开头。由于正在将其传递到
Image.file(imageFile,fit:BoxFit.cover)
中,您会看到由于断言传递到Image.file的文件不是null而导致的失败


您需要添加一些逻辑来检查imageFile是否为null,如果为null,则执行不同的操作。

好的,现在我知道异常是因为imageFile为null,但是如果我处理这个问题,例如使用空容器,即使在加载图像时,它也始终为null。。。你能帮我吗?嗯,不运行它很难判断,但是你确定图像已经设置好了吗?查看您的代码,
if(mounted)
可能是一个问题。。。可能值得调试或加入一些print语句,以查看何时/是否正在进行设置
I/flutter ( 5077): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 5077): The following assertion was thrown building ImageAndFaces(dirty):
I/flutter ( 5077): 'package:flutter/src/painting/image_provider.dart': Failed assertion: line 532 pos 14: 'file !=
I/flutter ( 5077): null': is not true.
I/flutter ( 5077): 
I/flutter ( 5077): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter ( 5077): more information in this error message to help you determine and fix the underlying cause.
I/flutter ( 5077): In either case, please report this assertion by filing a bug on GitHub:
I/flutter ( 5077):   https://github.com/flutter/flutter/issues/new?template=BUG.md
I/flutter ( 5077): 
I/flutter ( 5077): When the exception was thrown, this was the stack:
I/flutter ( 5077): #2      new FileImage (package:flutter/src/painting/image_provider.dart:532:14)
I/flutter ( 5077): #3      new Image.file (package:flutter/src/widgets/image.dart:254:16)
I/flutter ( 5077): #4      ImageAndFaces.build (package:visionappwork/main.dart:94:28)
I/flutter ( 5077): #5      StatelessElement.build (package:flutter/src/widgets/framework.dart:3789:28)
I/flutter ( 5077): #6      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3736:15)
.........