Firebase ML kit give FirebaseMLException: Waiting

2020-04-02 07:51发布

问题:

I'm use firebase ml kit for text recognition but give this exception on emulator and real device.

W/System.err: com.google.firebase.ml.common.FirebaseMLException: Waiting for the text recognition model to be downloaded. Please wait.
    at com.google.android.gms.internal.firebase_ml.zzjz.zzc(Unknown Source)
    at com.google.android.gms.internal.firebase_ml.zzjz.zza(Unknown Source)
    at com.google.android.gms.internal.firebase_ml.zzic.call(Unknown Source)
    at com.google.android.gms.internal.firebase_ml.zzhx.zza(Unknown Source)
    at com.google.android.gms.internal.firebase_ml.zzhy.run(Unknown Source)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at com.google.android.gms.internal.firebase_ml.zze.dispatchMessage(Unknown Source)
    at android.os.Looper.loop(Looper.java:136)
    at android.os.HandlerThread.run(HandlerThread.java:61)

Here my code

private fun MlProcessText(imageUri:Uri) {
    val bitmap = MediaStore.Images.Media.getBitmap(contentResolver, imageUri)
    val textVision = FirebaseVisionImage.fromBitmap(bitmap)
    val detector = FirebaseVision.getInstance().onDeviceTextRecognizer

    detector.processImage(textVision).addOnSuccessListener { it ->
        val blocks = it.textBlocks
        if (blocks.size == 0 ){

            tvVision.text = "NO TEXT"
        }else{
            blocks.forEach {
                tvVision.append(" ${it.text}")
            }
        }

    }.addOnFailureListener {
        it.printStackTrace() // this is the exception log
        tvVision.text = it.message
    }
}

Also i tried :

1- Settings->Apps->Google Play Services->Storage->Manage Space->Clear All Data

2- Low storage check (At least 1Gig free)

And add meta-data

 <meta-data
        android:name="com.google.firebase.ml.vision.DEPENDENCIES"
        android:value="ocr,text" />

But still the same error!

UPDATE

After getting stuck for several days i try to use Google Mobile Vision

So i add this to my dependencies

implementation 'com.google.android.gms:play-services-vision:17.0.2'

And use this article for OCR and in this code

    //Create the TextRecognizer
    final TextRecognizer textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();

    if (!textRecognizer.isOperational()) {
        Log.w(TAG, "Detector dependencies not loaded yet");
    } else {

        //Initialize camerasource to use high resolution and set Autofocus on.
        mCameraSource = new CameraSource.Builder(getApplicationContext(), textRecognizer)
                .setFacing(CameraSource.CAMERA_FACING_BACK)
                .setRequestedPreviewSize(1280, 1024)
                .setAutoFocusEnabled(true)
                .setRequestedFps(2.0f)
                .build();
     }

textRecognizer.isOperational() return always false. that mean it does not work too . I think there is something common with this two problem.

So i am steel stuck on text recognizer for android !

Test on : Nox emulator , google Nexus 5X API 26 emulator and on Huawei p10 and Samsung Galaxy S7 real device.

is there any idea to solve this problem?

回答1:

TextRecognizer textRecognizer = new TextRecognizer.Builder(this).build();

    if(!textRecognizer.isOperational()) {
        // Note: The first time that an app using a Vision API is installed on a
        // device, GMS will download a native libraries to the device in order to do detection.
        // Usually this completes before the app is run for the first time.  But if that
        // download has not yet completed, then the above call will not detect any text,
        // barcodes, or faces.
        // isOperational() can be used to check if the required native libraries are currently
        // available.  The detectors will automatically become operational once the library
        // downloads complete on device.
        Log.w(LOG_TAG, "Detector dependencies are not yet available.");

        // Check for low storage.  If there is low storage, the native library will not be
        // downloaded, so detection will not become operational.
        IntentFilter lowstorageFilter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW);
        boolean hasLowStorage = registerReceiver(null, lowstorageFilter) != null;

        if (hasLowStorage) {
            Toast.makeText(this,"Low Storage", Toast.LENGTH_LONG).show();
            Log.w(LOG_TAG, "Low Storage");
        }


回答2:

I've noticed that sometimes the problem is the slow internet connection and just need more time the model to be downloaded. To minimize the bad user experience I've added this warm up method and call it as soon as the app starts, so when the actual recognition occurs the model to be already downloaded.

/**
 * We call that on startup with hope the needed model will be downloaded as soon as possible;
 * It is used to prevent: "Waiting for the text recognition model to be downloaded. Please wait."
 * exception when recognizing.
 */
public static void warmUp() {
    Bitmap image = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
    image.eraseColor(android.graphics.Color.GREEN);
    FirebaseVisionImage firebaseVisionImage = FirebaseVisionImage.fromBitmap(image);
    FirebaseVision.getInstance().getOnDeviceTextRecognizer()
            .processImage(firebaseVisionImage)
            .addOnSuccessListener(null)
            .addOnFailureListener(null);
}