0

我已经为此苦苦挣扎了大约 24 小时,并且已经结束了关于如何解决它的想法。

我编写了从 Firebase 加载图像并在其中检测到人脸的代码 - 完全没有问题。但是,当我使用 Flutter 中的标准 imagePicker 类拍照时,尽管我实际上在屏幕上显示图像以检查它是否是有效文件,但 faceDetector 无法在图像中找到任何人脸。

代码的编辑版本如下。不会编译,因为我的项目中缺少一些东西,但希望你能明白。测试平台是 iOS(设备,不是模拟器)。插件是firebase_ml_vision 0.9.6+2,flutter的版本是前几天最新的。

有人有想法么?


import 'package:flutter/material.dart';
import 'package:firebase_ml_vision/firebase_ml_vision.dart';
import 'package:image_picker/image_picker.dart';
import 'package:modal_progress_hud/modal_progress_hud.dart';
import 'dart:io';
import 'dart:async';
import 'package:camera/camera.dart';

class imageComparison extends StatefulWidget {
  @override
  _imageComparison createState() => _imageComparison();
}

class _imageComparison extends State<imageComparison> {
  CameraController controller;
  List<CameraDescription> cameras;
  File chosenImage;
  bool gotImage = false;
  File grabbedImage;
  FaceDetector faceDetector;
  File tempImg;

  @override
  void initState() {
    super.initState();

    faceDetector = FirebaseVision.instance.faceDetector(FaceDetectorOptions(enableLandmarks: true, enableContours: true, enableClassification: true));

    availableCameras().then((value) {
      cameras = value;
      controller = CameraController(cameras[1], ResolutionPreset.medium);
      controller.initialize().then((_) {
        if (!mounted) {
          return;
        }
        setState(() {});
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    if (controller == null) {
      return Scaffold(
        backgroundColor: Colors.white,
        body: Container(),
      );
    }

    if (!gotImage) {
      File img;
      downloadImage(imageURLs[0], 'verificationImage.jpg').then((value) {
        img = tempImg = value;
        extractFace(img).then((value2) {
          print('got image');
          gotImage = true;
          setState(() {});
        });
      });
    }

    return Scaffold(
      backgroundColor: Colors.white,
      body: ModalProgressHUD(
        inAsyncCall: globals.showSpinner,
        child: Container(
          width: globals.SizeConfig.blockSizeHorizontal * 100,
          height: globals.SizeConfig.blockSizeVertical * 100,
          decoration: BoxDecoration(
            gradient: gradient,
          ),
          child: Stack(
            children: <Widget>[
              Positioned(
                left: globals.SizeConfig.blockSizeHorizontal * 10,
                top: globals.SizeConfig.blockSizeVertical * 5,
                child: Column(
                  children: [
                    FlatButton(
                      child: Text(
                        'check',
                        textAlign: TextAlign.center,
                        style: TextStyle(color: Colors.white, fontSize: globals.SizeConfig.blockSizeHorizontal * 4),
                      ),
                      color: Colors.red,
                      onPressed: () async {
                        bool res = await takeAndAnalyseImage();
                        //other stuff
                      },
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Future<bool> takeAndAnalyseImage() async {
    //String filepath = await takePicture();

    grabbedImage = File((await ImagePicker().getImage(source: ImageSource.camera)).path);

    //grabbedImage = File(filepath);
    setState(() {
      print('setting state');
    });
    while (grabbedImage == null) {}

    if (mounted) {
      extractFace(grabbedImage).then((value) {
        //irrelevant stuff
      });
    }
    return null;
  }

  Future<Map<String, dynamic>> extractFace(File img) async {
    FirebaseVisionImage visionImage;

    visionImage = FirebaseVisionImage.fromFile(img);

    Map<String, dynamic> facevals = Map<String, dynamic>();
    List<Face> faces;
    try {
      faces = await faceDetector.processImage(visionImage);
    } catch (e) {
      print('can\'t extract face from image. e = ' + e.toString());
    }
    for (Face face in faces) {
      //do irrelevant stuff to question.
    }
    return facevals;
  }
}

编辑:将图像转换为'FirebaseVisionImage'似乎工作正常 - 这FaceDetector.processImage是失败的......

4

0 回答 0