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 ByteBuffer
imageClassifier
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
ImageClassifier
class. It takes acontext : Context
object 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 theContext
class that holds application-specific information, resources, or context.Lines 18–27: These declare a companion object associated with the
ImageClassifier
class. 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
val
propertyinterpreter
that’s lazily initialized using thelazy
delegate, meaning theinterpreter
initializes when we access it for the first time. Theinterpreter
is an instance of the TF LiteInterpreter
class, which loads a pretrained DL model using theloadModelFile()
function and anInterpreter.Options
object. The interpreter is responsible for executing the computation graph of the pretrained model, which takes the input image in the form of aByteBuffer
and returns the classification result.Lines 32–34: These declare another
val
property,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 uselabelList
to map these probabilities to their corresponding labels to notify the app user. ThelabelList
property is of the typeList<String>
that’s initialized by loading the labels associated with the image classes using theloadLabels()
function.
The loadModelFile()
method
The loadModelFile()
method of the ImageClassifier
class ...