App crashes when try to read the text on a image using firebase ml vision

1,450

I was just having the same issue today. I spent a little while debugging it and found the cause, which i am hoping will also help you out.

I wrapped the three Firebase lines in a try/catch.

try {
  final FirebaseVisionImage visionImage =
  final TextRecognizer _recognizer = FirebaseVision.instance.textRecognizer();
  final VisionText readText = await _recognizer.processImage(visionImage);
} catch(error) {
  print('processText() - Error: $error');
}

This was then printing the following error

processText() - Error: PlatformException(MLVisionDetectorIOError, path_to_my_image (No such file or directory), null)

I then used the Android Studios device file explorer and looked at my device and sure enough, the file was not there!

I ended up creating a helper function to capture the image which then creates a copy of the image (which fixes the issue).

import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart' as syspaths;
import 'package:path/path.dart' as path;

Future<String> takePicture() async {
  final imageFile = await ImagePicker.pickImage(
    source: ImageSource.camera,
  );

  if (imageFile == null) {
    return null;
  }

  final appDir = await syspaths.getApplicationDocumentsDirectory();
  final fileName = path.basename(imageFile.path);

  final savedImage = await imageFile.copy('${appDir.path}/$fileName'); //This is the line that fixed it for me
  return '${appDir.path}/$fileName';
}

If you go down this route, you'll need to update your pubspec.yaml to include the following dependencies..

image_picker: ^0.6.5+2
path:
path_provider: ^1.6.7

Image Picker Library

Path Library

Path Provider Library

Share:
1,450
Udith Shalinda
Author by

Udith Shalinda

Web developer | mobile developer | reactjs | angular | flutter

Updated on December 14, 2022

Comments

  • Udith Shalinda
    Udith Shalinda over 1 year

    I'm tring to read text from the image using firebase ml vision and I use image_picker to get the image from gallery.

    image_picker: ^0.6.1+4

    firebase_ml_vision: ^0.9.2+1

    Get the image

    File imageFile = await ImagePicker.pickImage(source: ImageSource.gallery);
    

    the app crashes only when executing the last line of the below code

    final FirebaseVisionImage visionImage = FirebaseVisionImage.fromFile(imageFile);
    
    final TextRecognizer textRecognizer = FirebaseVision.instance.textRecognizer();
    
    final VisionText visionText = await textRecognizer.processImage(visionImage);
    //crash the app when execution above line
    

    error message

    W/ExifInterface(15273): Skip the tag entry since data format (ULONG) is unexpected for tag: LightSource
    W/ExifInterface(15273): Stop reading file since a wrong offset may cause an infinite loop: 0
    W/ExifInterface(15273): Stop reading file since a wrong offset may cause an infinite loop: 0
    W/ExifInterface(15273): Skip the tag entry since data format (ULONG) is unexpected for tag: LightSource
    W/ExifInterface(15273): Stop reading file since a wrong offset may cause an infinite loop: 0
    W/ExifInterface(15273): Stop reading file since a wrong offset may cause an infinite loop: 0
    E/MethodChannel#plugins.flutter.io/firebase_ml_vision(15273): Failed to handle method call
    E/MethodChannel#plugins.flutter.io/firebase_ml_vision(15273): java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
    E/MethodChannel#plugins.flutter.io/firebase_ml_vision(15273):   at io.flutter.plugins.firebasemlvision.FirebaseMlVisionPlugin.getDetector(FirebaseMlVisionPlugin.java:205)
    E/MethodChannel#plugins.flutter.io/firebase_ml_vision(15273):   at io.flutter.plugins.firebasemlvision.FirebaseMlVisionPlugin.handleDetection(FirebaseMlVisionPlugin.java:74)
    E/MethodChannel#plugins.flutter.io/firebase_ml_vision(15273):   at io.flutter.plugins.firebasemlvision.FirebaseMlVisionPlugin.onMethodCall(FirebaseMlVisionPlugin.java:49)
    E/MethodChannel#plugins.flutter.io/firebase_ml_vision(15273):   at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:222)
    E/MethodChannel#plugins.flutter.io/firebase_ml_vision(15273):   at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:96)
    E/MethodChannel#plugins.flutter.io/firebase_ml_vision(15273):   at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:656)
    E/MethodChannel#plugins.flutter.io/firebase_ml_vision(15273):   at android.os.MessageQueue.nativePollOnce(Native Method)
    E/MethodChannel#plugins.flutter.io/firebase_ml_vision(15273):   at android.os.MessageQueue.next(MessageQueue.java:326)
    E/MethodChannel#plugins.flutter.io/firebase_ml_vision(15273):   at android.os.Looper.loop(Looper.java:160)
    E/MethodChannel#plugins.flutter.io/firebase_ml_vision(15273):   at android.app.ActivityThread.main(ActivityThread.java:6669)
    E/MethodChannel#plugins.flutter.io/firebase_ml_vision(15273):   at java.lang.reflect.Method.invoke(Native Method)
    E/MethodChannel#plugins.flutter.io/firebase_ml_vision(15273):   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    E/MethodChannel#plugins.flutter.io/firebase_ml_vision(15273):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
    E/flutter (15273): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(error, Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference, null)
    E/flutter (15273): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
    E/flutter (15273): #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:316:33)
    E/flutter (15273): <asynchronous suspension>
    E/flutter (15273): #2      TextRecognizer.processImage (package:firebase_ml_vision/src/text_recognizer.dart:22:38)
    E/flutter (15273): <asynchronous suspension>
    E/flutter (15273): #3      _ScanState._initializeVision (package:scanner/ui/scan.dart:65:56)
    E/flutter (15273): <asynchronous suspension>
    E/flutter (15273): #4      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:654:14)
    E/flutter (15273): #5      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:729:32)
    E/flutter (15273): #6      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
    E/flutter (15273): #7      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:365:11)
    E/flutter (15273): #8      TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:275:7)
    E/flutter (15273): #9      PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:455:9)
    E/flutter (15273): #10     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:75:13)
    E/flutter (15273): #11     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:102:11)
    E/flutter (15273): #12     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:218:19)
    E/flutter (15273): #13     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
    E/flutter (15273): #14     GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
    E/flutter (15273): #15     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
    E/flutter (15273): #16     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
    E/flutter (15273): #17     _rootRunUnary (dart:async/zone.dart:1136:13)
    E/flutter (15273): #18     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
    E/flutter (15273): #19     _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
    E/flutter (15273): #20     _invoke1 (dart:ui/hooks.dart:263:10)
    E/flutter (15273): #21     _dispatchPointerDataPacket (dart:ui/hooks.dart:172:5)
    

    how do I fix this?