微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。