rushio-consulting / flutter_camera_ml_vision

A flutter widget that show the camera stream and allow ML vision recognition on it, it allow you to detect barcodes, labels, text, faces...
MIT License
278 stars 107 forks source link

Uninitialized CameraController after closing and opening the application #133

Open petodavid opened 3 years ago

petodavid commented 3 years ago

E/flutter (14891): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: CameraException(Uninitialized CameraController, startImageStream was called on uninitialized CameraController.) E/flutter (14891): #0 CameraController.startImageStream (package:camera/camera.dart:438:7) E/flutter (14891): #1 CameraMlVisionState._start (package:flutter_camera_ml_vision/flutter_camera_ml_vision.dart:148:23) E/flutter (14891): #2 CameraMlVisionState.build. (package:flutter_camera_ml_vision/flutter_camera_ml_vision.dart:308:11) E/flutter (14891): #3 VisibilityDetectorLayer._fireCallback (package:flutter_widgets/src/visibility_detector/src/visibility_detector_layer.dart:268:24) E/flutter (14891): #4 VisibilityDetectorLayer._processCallbacks (package:flutter_widgets/src/visibility_detector/src/visibility_detector_layer.dart:239:13) E/flutter (14891): #5 _TaskEntry.run. (package:flutter/src/scheduler/binding.dart:80:34) E/flutter (14891): #6 Timeline.timeSync (dart:developer/timeline.dart:163:22) E/flutter (14891): #7 _TaskEntry.run (package:flutter/src/scheduler/binding.dart:77:16) E/flutter (14891): #8 SchedulerBinding.handleEventLoopCallback (package:flutter/src/scheduler/binding.dart:460:15) E/flutter (14891): #9 SchedulerBinding._runTasks (package:flutter/src/scheduler/binding.dart:438:9) E/flutter (14891): #10 _rootRun (dart:async/zone.dart:1182:47) E/flutter (14891): #11 _CustomZone.run (dart:async/zone.dart:1093:19) E/flutter (14891): #12 _CustomZone.runGuarded (dart:async/zone.dart:997:7) E/flutter (14891): #13 _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:1037:23) E/flutter (14891): #14 _rootRun (dart:async/zone.dart:1190:13) E/flutter (14891): #15 _CustomZone.run (dart:async/zone.dart:1093:19) E/flutter (14891): #16 _CustomZone.bindCallback. (dart:async/zone.dart:1021:23) E/flutter (14891): #17 Timer._createTimer. (dart:async-patch/timer_patch.dart:18:15) E/flutter (14891): #18 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:397:19) E/flutter (14891): #19 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:428:5) E/flutter (14891): #20 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12) E/flutter (14891):

class FaceVision extends StatefulWidget {
  @override
  _FaceVisionState createState() => _FaceVisionState();
}

class _FaceVisionState extends State<FaceVision> {
  List<Face> _faces = [];
  final _scanKey = GlobalKey<CameraMlVisionState>();
  CameraLensDirection cameraLensDirection = CameraLensDirection.front;
  FaceDetector detector = FirebaseVision.instance.faceDetector(
    FaceDetectorOptions(
      enableTracking: true,
      mode: FaceDetectorMode.fast,
    ),
  );

  @override
  Widget build(BuildContext context) {
    return CameraMlVision<List<Face>>(
      key: _scanKey,
      resolution: ResolutionPreset.max,
      cameraLensDirection: cameraLensDirection,
      detector: detector.processImage,
      overlayBuilder: (c) {
        return CustomPaint(
          painter: FaceDetectorPainter(
              _scanKey.currentState.cameraValue.previewSize.flipped, _faces,
              reflection: cameraLensDirection == CameraLensDirection.front),
        );
      },
      onResult: (faces) {
        if (faces == null || !mounted) {
          return;
        }
        setState(() {
          _faces = faces;
        });
        sl<RecognitionBloc>()..add(NewFaces(faces: _faces));
      },
      onDispose: () {
        detector.close();
      },
    );
  }

  @override
  void dispose() {
    super.dispose();
  }
}
petodavid commented 3 years ago

@Kleak

nirbar89 commented 3 years ago

Hey @petodavid did you find a solution?