0

我的应用程序检测到脸部,然后我停止从相机流式传输图像,但是当我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);
4

0 回答 0