Firebase ML 套件给出 FirebaseMLException: Waiting for the text recognition model to be download。请稍等

Posted

技术标签:

【中文标题】Firebase ML 套件给出 FirebaseMLException: Waiting for the text recognition model to be download。请稍等【英文标题】:Firebase ML kit give FirebaseMLException: Waiting for the text recognition model to be downloaded. Please wait 【发布时间】:2019-03-22 04:15:31 【问题描述】:

我使用 firebase ml 工具包进行文本识别,但在模拟器和真实设备上给出了这个例外。

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)

这是我的代码

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
    

我也试过了:

1- 设置->应用->Google Play 服务->存储->管理空间->清除所有数据

2-低存储检查(至少 1Gig 免费)

并添加元数据

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

但还是同样的错误!

更新

卡了几天后,我尝试使用 Google Mobile Vision

所以我将它添加到我的依赖项中

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

OCR 和这段代码中使用this article

    //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() 总是返回 false。这意味着它也不起作用。我认为这两个问题有一些共同点。

所以我被困在 android 的文本识别器上!

测试:Nox 模拟器、google Nexus 5X API 26 模拟器以及华为 p10 和三星 Galaxy S7 真机。

有什么办法可以解决这个问题吗?

【问题讨论】:

【参考方案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");
        

【讨论】:

这是检查存储的好点,我这样做了,hasLowStorage 返回 false 还可以尝试在 (!textRecognizer.isOperational) 等待某个时间,如此链接 [链接] (github.com/googlesamples/android-vision/issues/180) 中所建议的那样【参考方案2】:

我注意到有时问题是互联网连接速度慢,只是需要更多时间来下载模型。 为了尽量减少糟糕的用户体验,我添加了这个预热方法,并在应用启动后立即调用它,因此当实际识别发生时,模型已经下载。

/**
 * 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);

【讨论】:

【参考方案3】:

尝试从 google play 服务卸载更新。

【讨论】:

请详细说明:为什么、如何、在哪里。

以上是关于Firebase ML 套件给出 FirebaseMLException: Waiting for the text recognition model to be download。请稍等的主要内容,如果未能解决你的问题,请参考以下文章

Firebase ML套件:预训练模型

如何在 Firebase ML kit Android 中正确绘制检测到的人脸边界框?

Android Studio ML 套件无法加载 OCR 模块

无法解决:com.google.firebase:firebase-ml-vision:24.1.3;无法解决:com.google.firebase:firebase-core:20.0.2

如何使用用于 JS 开发的 firebase Emulator 设置 firebase firestore 和云功能测试套件

Firebase ML Kit 无法检测到人脸