1

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.

Sumair Aslam
  • 11
  • 1
  • 3

2 Answers2

1

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 :)

0

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.

  1. collect data of your classes (Images of Cats and Dogs)
  2. Make two folders one for images of Cats and other for images of Dogs
  3. Use any pretrained models to do develop classification model through transfer learning approach
  4. train the model and save the model
  5. convert the model to tflite format (model.tflite)
  6. create label.txt with the names of classes
  7. move the model.tflite and label.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.

Vishnuvardhan Janapati
  • 3,088
  • 1
  • 16
  • 25