...

/

The ImageClassifier Class

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 ByteBufferByteBuffer, and runs the inference using the pretrained model. Let’s explore the fields and methods of the imageClassifier class.

Fields

The following code defines the fields of the ImageClassifier class.

Press + to interact
package com.example.horse_human_classify
import android.content.Context
import android.graphics.Bitmap
import org.tensorflow.lite.Interpreter
import java.io.BufferedReader
import java.io.FileInputStream
import java.io.IOException
import java.io.InputStreamReader
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.MappedByteBuffer
import java.nio.channels.FileChannel
import java.util.*
class ImageClassifier(private val context: Context) {
companion object {
private const val BATCH_SIZE = 1
private const val INPUT_SIZE = 224
private const val NUM_CLASSES = 2
private const val PIXEL_SIZE = 3
private const val IMAGE_MEAN = 0
private const val IMAGE_STD = 255.0f
private 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 a context : 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 the Context 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 property interpreter that’s lazily initialized using the lazy delegate, meaning the interpreter initializes when we access it for the first time. The interpreter is an instance of the TF Lite Interpreter class, which loads a pretrained DL model using the loadModelFile() function and an Interpreter.Options object. The interpreter is responsible for executing the computation graph of the pretrained model, which takes the input image in the form of a ByteBuffer 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 use labelList to map these probabilities to their corresponding labels to notify the app user. The labelList property 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 ...