【问题标题】:Android Studio ML kit cannot load OCR moduleAndroid Studio ML 套件无法加载 OCR 模块
【发布时间】:2020-02-17 00:56:14
【问题描述】:

我正在开发一个涉及机器学习套件的 Android 应用。我通过tutorial 了解如何设置 Android Studio 以使用 Firebase。然后我开始使用文本识别 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'

我决定试一试 Barcode 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,而Barcode API取自firebase.ml

【问题讨论】:

    标签: android firebase gradle google-play-services firebase-mlkit


    【解决方案1】:

    解决方案是更新 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
    
    

    【讨论】:

    • 我们不能指望我们的用户更新他们的 google play 服务以使我们的应用程序正常工作。我们如何打包应用程序本身所需的内容以用于生产版本?
    • 我有 20+ 的谷歌播放服务,但仍然面临这个问题。也许别的东西解决了它。
    • 您是否明确更新了 google play 服务? @Arshed
    • 在某些设备中未安装 Firebase ML 套件。我让他等了10分钟,还是无法安装文本识别模型。你有什么解决办法吗?
    • @bdevloper 你试过安装其他型号吗?我只有 OCR 有问题,其他模型加载正常。
    【解决方案2】:

    尝试在您的 AndroidManifest.xml 中添加以下内容

    <application>
        ...
    
        <meta-data android:name="com.google.android.gms.vision.DEPENDENCIES" android:value="ocr"/>
    
    </application>
    

    文档可以在https://developers.google.com/ml-kit/vision/text-recognition/android找到

    【讨论】:

      【解决方案3】:

      这是清单中的一些代码,请检查您是否遗漏了一些代码。

      ManifestFile 安装位置

      <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 服务是否正常工作
      • 如果需要更新它

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-11-22
          • 2013-07-23
          • 1970-01-01
          • 2016-12-24
          • 1970-01-01
          • 2011-03-08
          相关资源
          最近更新 更多