在 flutter 中使用 GoogleMlKit (firebase) 进行眼睛检测

问题描述 投票:0回答:1

下面的代码,我用于检测颤动中的面部和眼睛,我使用 firebase google ml 套件成功检测面部,但问题是在检测眼睛(leftEyeOpenProbability 和 rightEyeOpenProbability)时总是为空。

下面是我的代码:

 `Future<void> detectFacesInImage(String imagePath) async
  {
    final File imageFile = File(imagePath);
    final InputImage inputImage = InputImage.fromFile(imageFile);
    final FaceDetector faceDetector = GoogleMlKit.vision.faceDetector();
   
    try {
      final List<Face> faces = await faceDetector.processImage(inputImage);
      if (faces.isEmpty)
      {
              /// No faces detected in the image
              print('No faces detected in the image');
              showCustomSnackbar(title: 'error', message: 'No faces detected in the image', backgroundColor: Colors.red);
              return;
            }

            for (Face face in faces)
            {
              print('enter in face');
              /// Access the right eye open probability
              final double? leftEyeOpenProbability = face.leftEyeOpenProbability;
              if (leftEyeOpenProbability != null) {
                print('Left eye open probability: $leftEyeOpenProbability');
              } else {
                print('Left eye open probability: $leftEyeOpenProbability');
                showCustomSnackbar(title: 'error', message: 'Left eye open probability not available', backgroundColor: Colors.red);
                return;
              }
              /// Access the right eye open probability
              final double? rightEyeOpenProbability = face.rightEyeOpenProbability;
              if (rightEyeOpenProbability != null) {
                print('Right eye open probability: $rightEyeOpenProbability');
              } else {
                print('Right eye open probability: $rightEyeOpenProbability');
                showCustomSnackbar(title: 'error', message: 'Right eye open probability not available', backgroundColor: Colors.red);
                return;
              }

            }
    } catch (e) {
      showCustomSnackbar(title: 'error', message: 'Error detecting faces', backgroundColor: Colors.red);
      print('Error detecting faces: $e');
    } finally {
      /// showCustomSnackbar(title: 'error', message: 'succes', backgroundColor: Colors.red);
      faceDetector.close();
    }
  }`

I am expecting when eyes open, it should return some probability value of left and right open.
flutter dart face-detection firebase-mlkit eye-detection
1个回答
1
投票

我使用 google_mlkit_face_detection 而不是 google_ml_kit 成功获得右眼、左眼和所有面部标志的概率。问题是,之前,我在启用此功能后没有启用enableLandmarks,它可以工作。以下是我的新代码:

    Future<void> detectFacesInImage(String imagePath) async {
    final options = FaceDetectorOptions(
        enableLandmarks: true,
        enableContours: true,
        enableTracking: true,
        enableClassification: true);
    final faceDetector = FaceDetector(options: options);
    final File imageFile = File(imagePath);
    final InputImage inputImage = InputImage.fromFile(imageFile);

    try {
      final List<Face> faces = await faceDetector.processImage(inputImage);
      if (faces.isEmpty) {
        /// No faces detected in the image
        print('No faces detected in the image');
        showCustomSnackbar(
            title: 'error',
            message: 'No faces detected in the image',
            backgroundColor: Colors.red);
        return;
      }

      for (Face face in faces) {
        print('enter in face');

        /// Access the right eye open probability
        final double? leftEyeOpenProbability = face.leftEyeOpenProbability;
        if (leftEyeOpenProbability != null) {
          print('Left eye open probability: $leftEyeOpenProbability');
        } else {
          print('Left eye open probability: $leftEyeOpenProbability');
          showCustomSnackbar(
              title: 'error',
              message: 'Left eye open probability not available',
              backgroundColor: Colors.red);
          return;
        }

        /// Access the right eye open probability
        final double? rightEyeOpenProbability = face.rightEyeOpenProbability;
        if (rightEyeOpenProbability != null) {
          print('Right eye open probability: $rightEyeOpenProbability');
        } else {
          print('Right eye open probability: $rightEyeOpenProbability');
          showCustomSnackbar(
              title: 'error',
              message: 'Right eye open probability not available',
              backgroundColor: Colors.red);
          return;
        }
      }
    } catch (e) {
      showCustomSnackbar(
          title: 'error',
          message: 'Error detecting faces',
          backgroundColor: Colors.red);
      print('Error detecting faces: $e');
    } finally {
      /// showCustomSnackbar(title: 'error', message: 'succes', backgroundColor: Colors.red);
      faceDetector.close();
    }
  }                
© www.soinside.com 2019 - 2024. All rights reserved.