...

/

Image Classification App Using Task Library

Image Classification App Using Task Library

Use the ImageClassifier API of the TF Lite Task Library to deploy a DL model to an Android app for image classification.

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.

Press + to interact
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.

Press + to interact
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.

Press + to interact
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 options. This pattern of object construction allows step-by-step initialization of properties and configuration of various options for objects. We use the builder pattern to configure ImageClassifier.Options for additional settings (optional). We also create an instance of the ImageClassifier class by specifying the model file path or the model asset name, and giving the options.

Press + to interact
/* Code for the activity lifecycle methods, UI initialization, etc.
... */
// We can place the following code within the onCreate() method of the activity that uses the ImageClassifier API
// Configuring ImageClassifier.Options
val options = ImageClassifier.ImageClassifierOptions.builder()
.setBaseOptions(BaseOptions.builder().build()) //.useGpu() before .build()
.setMaxResults(3)
.build()
// Specify the path to the model file or the model asset name
val modelPath = "image_classifier_model_with_metadata.tflite"
// Create an instance of ImageClassifier
val imageClassifier = ImageClassifier.createFromFileAndOptions(
this, modelPath, options
)
...