The steps for this recipe are as follows:
- In the header section of the MainActivity.java file, add the necessary reference to TensorFlow:
import org.tensorflow.lite.Interpreter
- In the variables section, initialize the tflite interpreter:
Interpreter tflite;
- In the OnCreate method, add the code that will load the model from the file into the tflite interpreter:
tflite = new Interpreter(loadModelFile(activity));
- Then, create a method that will load the model file:
private MappedByteBuffer loadModelFile(Activity activity) throws IOException {
AssetFileDescriptor fileDescriptor =
activity.getAssets().openFd(getModelPath());
FileInputStream inputStream = new
FileInputStream(fileDescriptor.getFileDescriptor());
FileChannel fileChannel = inputStream.getChannel();
long startOffset = fileDescriptor.getStartOffset();
long declaredLength = fileDescriptor.getDeclaredLength();
return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset,
declaredLength...