Flutter-Android-在 imageStream 上使用 FaceDetector 时应用程序崩溃

发布于 2025-01-15 13:14:12 字数 2107 浏览 1 评论 0原文

每当我在调试、配置文件或发布模式下使用带有面部检测器的应用程序时,它都可以正常工作,但是当我构建应用程序并安装它然后启动它时,应用程序会在相机流启动后自动关闭,

我使用 google_ml_kit: ^0.7。 3camera: ^0.9.4+14

这是我用来初始化相机并每次开始检测的代码

  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(() {});
    }
  }

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(() {});
    }
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文