Search code examples
flutterdartfirebase-mlkitgoogle-mlkit

How do I make autocapture when face is detected in flutter


I am trying to capture the image automatically rather than button tap method when face is detected. For this I'm using these plugins google_ml_kit: ^0.13.0 for face detection and camera: ^0.9.4+19.

want to trigger this method automatically rather than callBack Function.

Future<void> takePicture() async {
    if (_faceDetectorService.faceDetected) {
      await _cameraService.takePicture();
      setState(() => _isPictureTaken = true);
    } else {
      showDialog(
        context: context,
        builder: (context) => AlertDialog(
          content: Text('No face detected!'),
        ),
      );
    }
  }

Solution

  • I'd implement it using camera controller with provider or any other state management tool to await for face detection to capture without any button being pressed. Or you could use streams of the google_ml_kit data if its a future you could turn it to stream then use streambuilder to listen for face detection then capture in the same state or after a timed response is received.