繁体   English   中英

使用 ML Kit 的计算机视觉 - Flutter In Focus

[英]Computer Vision with ML Kit - Flutter In Focus

我正在尝试遵循带有 ML Kit计算机视觉 - Flutter In Focus教程,在那里我一步一步地遵循了教程,但仍然没有设法让它发挥作用。

我的代码如下:

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})'),
    );
  }
}

我得到以下异常堆栈:

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)
.........

有谁知道问题是什么? 我尝试了任何我能想到的方法,包括尝试在创建实例时捕获 'ImageAndFaces' 类构造函数,但没有成功。 我是扑扑和飞镖的新手,所以也许这是一个愚蠢的错误。

非常感谢!

您遇到问题的原因是 imageFile 以 null 开头。 由于它被传递给Image.file(imageFile, fit: BoxFit.cover)你看到失败是因为断言传递给 Image.file 的文件不为空。

您需要添加一些逻辑来检查 imageFile 是否为空,如果为空则做一些不同的事情。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM