firebase ml视觉检测人脸,而flutter应用程序中的相机前没有人脸

问题描述

我的应用程序检测到人脸,然后我停止从相机流式传输图像,但是当我再次调用 startimagestream 时,它会在相机前面没有人的情况下检测到人脸。我无法弄清楚问题所在。

_camera = CameraController(description,ResolutionPreset.high,enableAudio: false);

await _camera.initialize();
await Future.delayed(Duration(seconds: 3));
if (mounted) {
  setState(() {
    _isCameraLoading = false;
  });
  if (_camera != null)
    _camera.startimagestream((Cameraimage image) {
      if (_isDetecting) {
       return;
      }
      _isDetecting = true;
      if (image == null) {
        return;
      }
      var _image = image;
      image = null;

      detect(_image,_getDetectionMethod(),rotation).then(
        (dynamic result) async {
          if (result.length == 0) {
           _isDetecting = false;
            return;
          }
          _camera.stopimagestream();
          setState(() {
            _camera = null;
          });

          Face _face;
          imglib.Image convertedImage =
              _convertCameraimage(_image,_direction);
          _image = null;
          for (_face in result) {
            double x,y,w,h;
            x = (_face.boundingBox.left - 10);
            y = (_face.boundingBox.top - 10);
            w = (_face.boundingBox.width + 10);
            h = (_face.boundingBox.height + 10);
            imglib.Image croppedImage = imglib.copyCrop(
                convertedImage,x.round(),y.round(),w.round(),h.round());
            croppedImage = imglib.copyResizeCropSquare(croppedImage,112);
            faceData = _recog(croppedImage);
          }

          if (_registering) {
            await _insertData(
                fullName: widget.name,mobileNo: widget.mobileNo,faceData: faceData);
            _registering = false;
            _isDetecting = false;
            _initializeCamera();
            return;
          }

          await getEmployeeData(faceData);
          if (_message != 'success') {
            _isDetecting = false;
            await showAlert(context: context,isInvalid: true);
            return;
          } else if (userDetails.isNotEmpty || userDetails != null) {
            faceData = null;
            _isDetecting = false;
            _name = userDetails.elementAt(0).fullName ;
            _empCode = userDetails.elementAt(0).empCode ;
            _date = userDetails.elementAt(0).time;
            await showAlert(
                context: context,name: _name,empCode: _empCode,date: _date);
            return;
          }
          _isDetecting = false;
          log('detecting $_isDetecting');
        },

这里是detect函数

Future<dynamic> detect(
Cameraimage image,HandleDetection handleDetection,ImageRotation rotation,) async {
 return await handleDetection(
  FirebaseVisionImage.fromBytes(
  image.planes[0].bytes,buildMetaData(image,rotation),),);
}

这是_getDetectionMethod

  HandleDetection _getDetectionMethod() {
final faceDetector = FirebaseVision.instance.faceDetector(
  FaceDetectorOptions(
    mode: FaceDetectorMode.accurate,);
return faceDetector.processImage;
}

HandleDetection

typedef HandleDetection = Future<dynamic> Function(FirebaseVisionImage image);

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)