Android Studio ML kit cannot load OCR module

12,681

Solution 1

The solution was to update Google Play Services app. I did not consider this as an option at first as I would have expected an API 29 emulator to have an up-to-date Google Play Services installed. I signed into Google Play on the emulator, searched Google Play Services, uninstalled it (there was no "update" option), and installed it again. It still says that the installed version is 19.4.20 though the text recognition started working as expected. Here are some relevant logs:

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

Solution 2

Try adding following in your AndroidManifest.xml

<application>
    ...

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

</application>

Documentation can be found at https://developers.google.com/ml-kit/vision/text-recognition/android

Share:
12,681
Big Monday
Author by

Big Monday

Updated on June 07, 2022

Comments

  • Big Monday
    Big Monday about 2 years

    I am working on an android app that involves ML kit. I went through the tutorial on how to set up Android Studio to work with Firebase. Then I headed to use the text recognition API. Here is the code that I am having in some button click listener:

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

    I get the following logs:

    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)
    

    I googled around to see how I can fix any of those errors:

    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.
    

    So far I have tried clearing Google Play Services data (version 19.4.20), restarting/recreating the emulator, tweaking Gradle configurations (e.g. adding/removing firebase-core dependency). Project-level config:

    // 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
    }
    

    App-module-level config:

    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'
    

    I decided to give Barcode API a shot, and added

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

    as dependency. To my surprise, when I made an API call to it, the success listner was hit and the following logs appeared:

    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
    

    While the OCR API still produced the same error. I think it has to deal with the repositories the libraries are fetched from. Barcode 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
    

    I have too little experience with Gradle and Firebase to figure this one out. Why is OCR API fetched from android.gms, while Barcode API comes from firebase.ml?

  • Corey Cole
    Corey Cole over 4 years
    We cannot expect our users to update their google play services for our app to work. How can we package up what is needed in the app itself for a production release?
  • Arshed
    Arshed over 4 years
    I'm having google play service of 20+ but still face this issue. Maybe something else solved it.
  • Big Monday
    Big Monday over 4 years
    Did you explicitly update google play services? @Arshed
  • Bhavin Patel
    Bhavin Patel over 4 years
    In some device firebase ML kit not installing. I made him wait for 10 min but still can't installed text recognition model .Do you have any solution ?
  • Big Monday
    Big Monday over 4 years
    @bdevloper have you tried installing any other models? I had issues only with OCR, other models were loading fine.
  • Mike
    Mike about 4 years
    I use a OnePlus 6 for a test device. Play services are up-to-date but this error still pops up. Anyone else having the same issue?