I want to make an app that capture an image using mobile camera and pass this image to tensorflow lite to classify, what is in the image.
2 Answers
You can use a TensorImage
(from org.tensorflow.lite.support.image). It has constructors for a TensorBuffer
, a Bitmap
, an int []
or float []
.
So, say you have an image as a bitmap called myImage
, while running in myContext
context, then you can use the following code to run the tflite interpreter on the myModel.tflite
model:
// create tflite options (currently empty) and load the tf model
Interpreter.Options tfliteOptions = (new Interpreter.Options());
tfliteModel = FileUtil.loadMappedFile(myContext, "myModel.tflite");
// create tflite interpreter
tfliteInterpreter = new Interpreter(tfliteModel, tfliteOptions);
// get model parameters (index tensor is 0 for a single image)
DataType myImageDataType = tfliteInterpreter.getInputTensor(0).dataType();
myTensorImage = new TensorImage(myImageDataType);
// load bitmap
myTensorImage.load(myImage);
// run inference
tfliteInterpreter.run(myTensorImage.getBuffer(), output);
If your image input is given as bytes, you can use TensorBuffer
's ByteBuffer
, and the rest is the same.
Note that I did not specify the interpreter output
.
You can also use TensorBuffer
as the output, which can easily provide you with ByteBuffer
, int []
or float []
arrays.
Hope this helps :)

- 431
- 5
- 17
If you are an experienced, you can follow the approach mentioned by @somethingorange.
If you are a beginner and want to develop an image classification app on Mobile, then follow the following approach. For example, you are trying to develop a model to classify whether the given image is Cat
or Dogs
.
- collect data of your classes (Images of
Cats
andDogs
) - Make two folders one for images of Cats and other for images of Dogs
- Use any pretrained models to do develop classification model through transfer learning approach
- train the model and save the model
- convert the model to tflite format (
model.tflite
) - create
label.txt
with the names of classes - move the
model.tflite
andlabel.txt
to assets folder in Android Studio.
Best thing is all the above steps are mentioned in this tutorial by TFLite team which is a great place to start.
Hope these steps help beginners.

- 3,088
- 1
- 16
- 25