Ayushi Gupta
Ayushi Gupta

Reputation: 1

Why am I unable to use TensorFlow Lite with ML Model Binding in my Android project?

I'm building an Android app using TensorFlow Lite with ML Model Binding. I've followed the required setup but can't get the TensorFlow Lite model to work in my MainActivity. Specifically, the IDE doesn't recognize the generated binding class or methods to load the model.

Here is my build.gradle file:

    plugins {
        id("com.android.application")
        id("org.jetbrains.kotlin.android")
    }

    android {
        namespace = "com.riya.measurify"
        compileSdk = 35

        androidResources {
            noCompress.add("tflite") /
        }

        defaultConfig {
            applicationId = "com.riya.measurify"
            minSdk = 26
            targetSdk = 34
            versionCode = 1
            versionName = "1.0"

            testInstrumentationRunner =      "androidx.test.runner.AndroidJUnitRunner"
        }

        buildFeatures {
            mlModelBinding = true
            dataBinding = true
            viewBinding = true
         }

        buildTypes {
            release {
                isMinifyEnabled = false
                 proguardFiles(
                    getDefaultProguardFile("proguard-android-    optimize.txt"),
                    "proguard-rules.pro"
                )
            }
        }

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = "1.8"
    }
}

dependencies {
    implementation("org.tensorflow:tensorflow-lite:2.12.0")
    implementation("org.tensorflow:tensorflow-lite-task-vision:0.4.2")
    implementation("androidx.core:core-ktx:1.15.0")
    implementation("androidx.appcompat:appcompat:1.7.0")
    implementation("com.google.android.material:material:1.12.0")
    implementation("androidx.constraintlayout:constraintlayout:2.2.0")
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.2.1")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.6.1")
}  

Placed the .tflite file in the src/main/assets folder. Ensured mlModelBinding is enabled in buildFeatures in the build.gradle file. Included the required TensorFlow Lite dependencies in the dependencies block. Rebuilt the project multiple times to regenerate the binding classes. Checked the IDE for errors, but no clear hints are provided. What I Expected I expected the Android Studio IDE to automatically generate a binding class for the TensorFlow Lite model (e.g., YourModelNameBinding) when I added the .tflite file to the assets folder and enabled mlModelBinding. Using this binding, I should be able to load the model and use it for inference in the MainActivity.

What Happened The binding class is not generated, and I can't use the TensorFlow Lite model in my project.

What could be causing this issue, and how can I fix it?

Upvotes: 0

Views: 67

Answers (0)

Related Questions