Flutter- Android - App crash When Using faceDetector on imageStream

668 views Asked by At

whenever I use the app with face detector in debug, profile, or release mode it works fine but when i build the app and install it then start it the app automatically close after the camera stream starts

im using google_ml_kit: ^0.7.3 and camera: ^0.9.4+14

this is the code i use to initialize the camera and start detecting every time

  void initCamera() async {
    final description = await availableCameras().then(
      (cameras) => cameras.firstWhere(
        (camera) => camera.lensDirection == CameraLensDirection.front,
      ),
    );
    cameraControllerNotifier.value = CameraController(
      description,
      ResolutionPreset.low,
      enableAudio: false,
    );
    await cameraControllerNotifier.value!.initialize();
    await Future.delayed(const Duration(milliseconds: 500));
    isDetecting = false;
    cameraControllerNotifier.value!.startImageStream((img) async {
      if (isDetecting) return;
      if (cameraControllerNotifier.value != null) {
        isDetecting = true;
        final image = InputImage.fromBytes(
          bytes: img.planes[0].bytes,
          inputImageData: InputImageData(
            inputImageFormat:
                InputImageFormatMethods.fromRawValue(img.format.raw)!,
            size: Size(img.width.toDouble(), img.height.toDouble()),
            imageRotation: MlHelper.rotationIntToImageRotation(
              description.sensorOrientation,
            ),
            planeData: null,
          ),
        );

        try {
          final faceDetector = GoogleMlKit.vision.faceDetector(
            const FaceDetectorOptions(
              mode: FaceDetectorMode.accurate,
              enableLandmarks: true,
            ),
          );

          List<Face> _faces = await faceDetector.processImage(image);
          if (_faces.isNotEmpty) {
           //..........
          } else {
            isClose.value = false;
          }
          isDetecting = false;
          // facesNotifier.value = _faces;
        } catch (e) {
          isClose.value = false;
          isDetecting = false;
          log("FaceKIt Error : $e");
        }
      }
    });
    if (mounted) {
      setState(() {});
    }
  }
0

There are 0 answers