Search code examples
firebasedartflutterfirebase-mlkitmlvision

Attributes of Face object is always null in firebase ml vision flutter


I followed all the instructions from this README for Face detection and When I try to access the attributes of face like face.leftEyeOpenProbability, face.rightEyeOpenProbability or face.smilingProbability it is throwing me null, though the faces are detected.

I tried to approach this in two ways,
1. Using the old firebase_ml_vision plugin for flutter (not compatible with AndroidX)
firebase_ml_vision: ^0.2.1 where the function detectInImage() is used.

2. Using the new firebase_ml_vision plugin (compatible with AndroidX)
firebase_ml_vision: ^0.6.0+2 where the funciton processImage() is used.

In both the approaches the attributes values are null.

This is the snippet which uses the latest plugin to detect faces.

void _getImageAndDetectFace() async {
  final imageFile = await ImagePicker.pickImage(source: ImageSource.camera);
  final image = FirebaseVisionImage.fromFile(imageFile);
  final faceDetector = FirebaseVision.instance.faceDetector(FaceDetectorOptions(
    mode: FaceDetectorMode.accurate,
    enableLandmarks: true,
  ));
  final faces = await faceDetector.detectInImage(image); //this is not null
  setState(() {
    if (mounted) {
      _imageFile = imageFile;
      _faces = faces;
      for (Face face in faces) { //face is detected here, but...
        print('left eye : ${face.leftEyeOpenProbability}'); //prints null
        print('right eye : ${face.rightEyeOpenProbability}'); //prints null
        print('smiling : ${face.smilingProbability}'); //prints null
      }
    }
  });
}

Please help me solve this.


Solution

  • I have exactly the same error.

    After read the documentation, I found in the class FaceDetector the variable enableClassification. This variable has default value False. For use detation of eyes and smiling have to enable this variable.

    In your case will be something like this:

         ... 
         final faceDetector = FirebaseVision.instance.faceDetector(FaceDetectorOptions(
            mode: FaceDetectorMode.accurate,
            enableLandmarks: true,
            enableClassification: true
            ));
        ...
    

    Hope this helps.