The ImageClassifier Class
Learn to implement the ImageClassifier class of an image classification Android app that utilizes a TF Lite model to classify images.
The ImageClassifier class is responsible for classifying an image into predefined categories. It takes a Bitmap image as input, resizes it to the required input size, converts it to a ByteBufferimageClassifier class.
Fields
The following code defines the fields of the ImageClassifier class. 
package com.example.horse_human_classifyimport android.content.Contextimport android.graphics.Bitmapimport org.tensorflow.lite.Interpreterimport java.io.BufferedReaderimport java.io.FileInputStreamimport java.io.IOExceptionimport java.io.InputStreamReaderimport java.nio.ByteBufferimport java.nio.ByteOrderimport java.nio.MappedByteBufferimport java.nio.channels.FileChannelimport java.util.*class ImageClassifier(private val context: Context) {companion object {private const val BATCH_SIZE = 1private const val INPUT_SIZE = 224private const val NUM_CLASSES = 2private const val PIXEL_SIZE = 3private const val IMAGE_MEAN = 0private const val IMAGE_STD = 255.0fprivate const val MODEL_FILE = "model.tflite"private const val LABEL_FILE = "labels.txt"}private val interpreter: Interpreter by lazy {Interpreter(loadModelFile(), Interpreter.Options())}private val labelList : List<String> by lazy {loadLabels()}// Implement methods here}
- Line 5: This imports the TF Lite interpreter, - import org.tensorflow.lite.Interpreter.
- Line 16: This declares the - ImageClassifierclass. It takes a- context : Contextobject as a constructor parameter, which allows access to the app’s resources, like assets, and creates a file path to store the DL model file. It’s an instance of the- Contextclass that holds application-specific information, resources, or context.
- Lines 18–27: These declare a companion object associated with the - ImageClassifierclass. It contains various constants related to the image classification model, such as the batch size, input image size, number of classes, pixel size, image mean, image standard deviation, model file name, and label file name. These constants define properties of the model and we can access them using the class name followed by the constant name, for instance,- ImageClassifier.MODEL_FILE.
- Lines 29–31: These declare a - valproperty- interpreterthat’s lazily initialized using the- lazydelegate, meaning the- interpreterinitializes when we access it for the first time. The- interpreteris an instance of the TF Lite- Interpreterclass, which loads a pretrained DL model using the- loadModelFile()function and an- Interpreter.Optionsobject. The interpreter is responsible for executing the computation graph of the pretrained model, which takes the input image in the form of a- ByteBufferand returns the classification result.
- Lines 32–34: These declare another - valproperty,- labelList : List, that stores the list of labels a TF Lite model can recognize. The output of the model is a probability distribution over all the possible classes, and we use- labelListto map these probabilities to their corresponding labels to notify the app user. The- labelListproperty is of the type- List<String>that’s initialized by loading the labels associated with the image classes using the- loadLabels()function.
The loadModelFile() method
The loadModelFile() method of the ImageClassifier class loads the TF ...