Search code examples
androidfirebasefirebase-mlkit

Firebase Cloud Text Recognition


I get the following error when I try to use the cloud based API to do text recognition:

W/System.err: com.google.firebase.ml.common.FirebaseMLException: Internal error has occurred when executing Firebase ML tasks
        at com.google.android.gms.internal.firebase_ml.zzmy.zza(Unknown Source:35)
        at com.google.android.gms.internal.firebase_ml.zzmz.run(Unknown Source:2)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at com.google.android.gms.internal.firebase_ml.zze.dispatchMessage(Unknown Source:6)
        at android.os.Looper.loop(Looper.java:280)
        at android.os.HandlerThread.run(HandlerThread.java:65)
    Caused by: java.lang.NullPointerException: The input TextAnnotation can not be null
        at com.google.android.gms.common.internal.Preconditions.checkNotNull(Unknown Source:11)
        at com.google.android.gms.internal.firebase_ml.zzpj.zzb(Unknown Source:1)
        at com.google.android.gms.internal.firebase_ml.zzpf.zza(Unknown Source:38)
        at com.google.android.gms.internal.firebase_ml.zzoo.zza(Unknown Source:23)
        at com.google.android.gms.internal.firebase_ml.zznd.call(Unknown Source:4)
        at com.google.android.gms.internal.firebase_ml.zzmy.zza(Unknown Source:29)
        ... 6 more

I create the detector as

    private FirebaseVisionTextRecognizer cloudDetector = FirebaseVision.getInstance().getCloudTextRecognizer();

and call the detector using:

FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(bm);
        Task<FirebaseVisionText> result =
                cloudDetector.processImage(image)
                        .addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
                            @Override
                            public void onSuccess(FirebaseVisionText firebaseVisionText) {
                                String text = firebaseVisionText.getText();
                                mFragment.setDetectedText(text);

                                // toggle isDetecting after 0.5 s. Do not want the textview to flickr to much
                                final Handler handler = new Handler();
                                handler.postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        isDetecting = false;
                                    }
                                }, delayTime);
                            }
                        })
                        .addOnFailureListener(
                                new OnFailureListener() {
                                    @Override
                                    public void onFailure(@NonNull Exception e) {
                                        // Task failed with an exception
                                        // ...
                                        Log.d("DETECTING", e.getMessage());
                                        e.printStackTrace();
                                    }
                                });

I have enabled the cloud API in the Firebase console. Moreover, the textdetector on the device works perfectly well. I do also not see any difference between my code and the example code here. Has anyone experienced this?


Solution

  • Apparently, Firebase handles images without text differently in the cloudDetector compared to deviceDetector.

    The deviceDetctor goes into onSuccess but the cloudDetector throws a NullPtrException and goes into onFailure. Since I toggled a boolean in onSuccess but not in onFailure, the cloudDetector was never called again. I now toggle the boolean in onFailure as well and that works.

    With that said, it is kind of stupid to have different behaviour for images without text. If no text is detected, nothing has gone wrong so throwing an exception does not seem appropriate.