Search⌘ K
AI Features

Image Classification App Using Task Library

Explore how to use the TF Lite Task Library's ImageClassifier API to load pretrained models, preprocess images, and display classification results on Android. Understand the builder pattern for configuring options, running inference on input images, and showing results with a Toast message in a simple app.

The ImageClassifier API of the Task Library

The ImageClassifier API of the TF Lite Task Library simplifies the process of loading a pretrained image classification model and performing classification on mobile devices. The ImageClassifier API abstracts away the complexities of model loading, input preprocessing, and result interpretation, allowing us to easily integrate image classification functionality into our mobile apps.

ImageClassifier API within the TF framework
ImageClassifier API within the TF framework

Image classification using the ImageClassifier API

To use the ImageClassifier API of the Task Library for image classification, we import its dependencies into the module’s build.gradle file. We specify that the build system doesn’t compress the model file. We also have to copy the .tflite model file to the assets directory.

Note: Gradle is an open-source build tool used to build numerous types of software.

Kotlin
android {
// Various settings
// We make sure that the tflite file is not compressed for the apk file
aaptOptions {
noCompress "tflite"
}
}
dependencies {
// Various dependencies
// Import the Task Vision library dependency
implementation "org.tensorflow:tensorflow-lite-task-vision"
// Import the GPU delegate library
implementation "org.tensorflow:tensorflow-lite-gpu-delegate-plugin"
}

Next, we import the ImageClassifier API from the Task Library to our app’s activity class that handles image classification.

Kotlin
import org.tensorflow.lite.task.core.BaseOptions
import org.tensorflow.lite.task.vision.classifier.ImageClassifier

The Task Library follows the builder pattern to create complex objects with various ...