Firebase云文本识别

时间:2019-01-31 20:04:01

标签: android firebase firebase-mlkit

当我尝试使用基于云的API进行文本识别时,出现以下错误:

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

我将检测器创建为

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

并使用以下命令调用检测器:

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

我已启用在火力地堡控制台云API。此外,该设备上的textdetector工作得很好。我的代码和示例代码here之间也没有任何区别。有人遇到过吗?

1 个答案:

答案 0 :(得分:1)

很明显,与deviceDetector相比,Firebase在cloudDetector中处理不含文本的图像的方式有所不同。

deviceDetctor进入onSuccess,但是cloudDetector抛出NullPtrException并进入onFailure。由于我在onSuccess中切换了布尔值,但没有在onFailure中切换布尔值,因此从未再次调用cloudDetector。现在,我也可以在onFailure中切换布尔值,并且可以正常工作。

话虽如此,对没有文本的图像具有不同的行为有点愚蠢。如果未检测到文本,则表示没有任何问题,因此抛出异常似乎不合适。