Android Studio的ML kit无法加载OCR模块。

15

我正在开发一个涉及ML kit的Android应用程序。我查看了关于如何使用Firebase设置Android Studio的教程,然后我开始使用文本识别API。以下是我在某个按钮单击监听器中使用的代码:

        FirebaseVisionTextRecognizer detector = FirebaseVision.getInstance()
            .getOnDeviceTextRecognizer();
        Task<FirebaseVisionText> result = detector.processImage(
            FirebaseVisionImage.fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.test))
        )
            .addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
                @Override
                public void onSuccess(FirebaseVisionText firebaseVisionText) {
                    System.out.println(firebaseVisionText);
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    e.printStackTrace();
                }
            });

我得到了以下日志:

W/DynamiteModule: Local module descriptor class for com.google.android.gms.vision.dynamite.ocr not found.
I/DynamiteModule: Considering local module com.google.android.gms.vision.dynamite.ocr:0 and remote module com.google.android.gms.vision.dynamite.ocr:0
D/TextNativeHandle: Cannot load feature, fall back to load dynamite module.
W/DynamiteModule: Local module descriptor class for com.google.android.gms.vision.ocr not found.
I/DynamiteModule: Considering local module com.google.android.gms.vision.ocr:0 and remote module com.google.android.gms.vision.ocr:0
E/Vision: Error loading module com.google.android.gms.vision.ocr optional module true: com.google.android.gms.dynamite.DynamiteModule$LoadingException: No acceptable module found. Local version is 0 and remote version is 0.
D/TextNativeHandle: Broadcasting download intent for dependency ocr
W/TextNativeHandle: Native handle not yet available. Reverting to no-op handle.
W/DynamiteModule: Local module descriptor class for com.google.android.gms.vision.dynamite.ocr not found.
I/DynamiteModule: Considering local module com.google.android.gms.vision.dynamite.ocr:0 and remote module com.google.android.gms.vision.dynamite.ocr:0
D/TextNativeHandle: Cannot load feature, fall back to load dynamite module.
W/DynamiteModule: Local module descriptor class for com.google.android.gms.vision.ocr not found.
I/DynamiteModule: Considering local module com.google.android.gms.vision.ocr:0 and remote module com.google.android.gms.vision.ocr:0
E/Vision: Error loading module com.google.android.gms.vision.ocr optional module true: com.google.android.gms.dynamite.DynamiteModule$LoadingException: No acceptable module found. Local version is 0 and remote version is 0.
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.zzrw.zzd(com.google.firebase:firebase-ml-vision@@24.0.0:21)
        at com.google.android.gms.internal.firebase_ml.zzrw.zza(com.google.firebase:firebase-ml-vision@@24.0.0:39)
        at com.google.android.gms.internal.firebase_ml.zzpd.zza(com.google.firebase:firebase-ml-common@@22.0.0:31)
        at com.google.android.gms.internal.firebase_ml.zzpf.call(Unknown Source:8)
        at com.google.android.gms.internal.firebase_ml.zzoz.zza(com.google.firebase:firebase-ml-common@@22.0.0:32)
        at com.google.android.gms.internal.firebase_ml.zzoy.run(Unknown Source:4)
        at android.os.Handler.handleCallback(Handler.java:873)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:99)
        at com.google.android.gms.internal.firebase_ml.zze.dispatchMessage(com.google.firebase:firebase-ml-common@@22.0.0:6)
        at android.os.Looper.loop(Looper.java:193)
        at android.os.HandlerThread.run(HandlerThread.java:65)

我谷歌了一下,看看我如何修复这些错误:

W/DynamiteModule: Local module descriptor class for com.google.android.gms.vision.dynamite.ocr not found.
I/DynamiteModule: Considering local module com.google.android.gms.vision.dynamite.ocr:0 and remote module com.google.android.gms.vision.dynamite.ocr:0
D/TextNativeHandle: Cannot load feature, fall back to load dynamite module.
W/DynamiteModule: Local module descriptor class for com.google.android.gms.vision.ocr not found.
I/DynamiteModule: Considering local module com.google.android.gms.vision.ocr:0 and remote module com.google.android.gms.vision.ocr:0
E/Vision: Error loading module com.google.android.gms.vision.ocr optional module true: com.google.android.gms.dynamite.DynamiteModule$LoadingException: No acceptable module found. Local version is 0 and remote version is 0.
W/System.err: com.google.firebase.ml.common.FirebaseMLException: Waiting for the text recognition model to be downloaded. Please wait.

到目前为止,我已经尝试了清除Google Play服务数据(版本19.4.20),重新启动/创建模拟器,调整Gradle配置(例如添加/删除firebase-core依赖项)。

项目级别配置:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.1'
        classpath 'com.google.gms:google-services:4.3.2'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

应用程序模块级别配置:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.user.app"
        minSdkVersion 24
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.gms:play-services-vision:19.0.0'
    implementation 'com.google.firebase:firebase-ml-vision:24.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

}

apply plugin: 'com.google.gms.google-services'

我决定尝试使用条形码API,并添加了

implementation 'com.google.firebase:firebase-ml-vision-barcode-model:16.0.1'

作为依赖项。令我惊讶的是,当我对其进行API调用时,成功的监听器被触发,并出现了以下日志:

I/DynamiteModule: Considering local module com.google.firebase.ml.vision.dynamite.barcode:10000 and remote module com.google.firebase.ml.vision.dynamite.barcode:0
    Selected local version of com.google.firebase.ml.vision.dynamite.barcode
W/is.moneytravel: Accessing hidden field Ljava/nio/Buffer;->address:J (light greylist, reflection)
I/tflite: Initialized TensorFlow Lite runtime.
I/native: barcode_detector_client.cc:222 Not using NNAPI

虽然OCR API仍然产生了相同的错误。我认为它与库获取的存储库有关。 条形码API:

Considering local module com.google.firebase.ml.vision.dynamite.barcode:10000 and remote module com.google.firebase.ml.vision.dynamite.barcode:0

OCR API:

Considering local module com.google.android.gms.vision.dynamite.ocr:0 and remote module com.google.android.gms.vision.dynamite.ocr:0

我对Gradle和Firebase的经验太少了,无法解决这个问题。 为什么OCR API来自android.gms,而条形码API来自firebase.ml?

4个回答

6
解决方案是更新Google Play服务应用程序。起初我并没有考虑到这一点,因为我期望API 29模拟器已经安装了最新的Google Play服务。我在模拟器上登录了Google Play,搜索了Google Play服务,卸载了它(没有“更新”选项),然后重新安装了它。虽然已安装版本仍显示为19.4.20,但文本识别开始按预期工作。以下是一些相关日志:
W/DynamiteModule: Local module descriptor class for com.google.android.gms.vision.dynamite.ocr not found.
I/DynamiteModule: Considering local module com.google.android.gms.vision.dynamite.ocr:0 and remote module com.google.android.gms.vision.dynamite.ocr:0
D/TextNativeHandle: Cannot load feature, fall back to load dynamite module.
I/DynamiteModule: Considering local module com.google.android.gms.vision.ocr:0 and remote module com.google.android.gms.vision.ocr:1
I/DynamiteModule: Selected remote version of com.google.android.gms.vision.ocr, version >= 1
V/DynamiteModule: Dynamite loader version >= 2, using loadModule2NoCrashUtils


16
我们不能指望用户更新他们的谷歌播放服务以使我们的应用程序工作。我们该如何将所需内容打包到应用本身中,以进行生产发布? - Corey Cole
1
我安装了20+的Google Play服务,但仍然面临这个问题。也许其他原因解决了它。 - Arshed
@bdevloper,你尝试安装其他模型了吗?我只在OCR方面遇到了问题,其他模型都能正常加载。 - Big Monday
我使用一台OnePlus 6作为测试设备。Play服务已经更新到最新版本,但仍然出现此错误。还有其他人遇到同样的问题吗? - Mike
我已经更新了GMS。但是它还是不起作用。 - Acemond
显示剩余2条评论

1

这里是来自清单文件的代码,请检查你的代码是否有遗漏。

ManifestFile installLocation

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.******.***"
    android:installLocation="auto"
/>
<!-- make sure to add 'android:installLocation="auto"', it was missing for me -->

ManifestFile元数据

<application
    <!-- ..... -->
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" /> <!--version--><!-- make sure added-->
    <meta-data
        android:name="com.google.android.gms.vision.DEPENDENCIES"
        android:value="barcode" /><!-- barcode,face,ocr make sure added-->
    <!-- ..... -->
</application>
  • 检查互联网连接
  • 检查剩余存储空间

如果您使用模拟器

  • 检查 Google 服务是否正常工作
  • 如有需要,请更新它

1

-2

对我来说,使用其他 AVD 解决了问题。 Google Play 商店应该是最新的。 尝试使用模拟器 AVD - Android Nexus 5X。

Android Studio AVD Manager with Google Play Store support


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接