0

I am working on an app where I have to pass an image to my own TFLite model. It will then return me result containing different values. Input is an image

Sample output is:

 [
  {
   "points": {
   "left": -5.070770263671875,
   "top": 6.558062851428986,
   "width": 994.1088562011719,
   "height": 627.7321452498436
  },
  "type": "Doc",
  "score": 0.998
 }
]

My code to run tflite model is:

protected Interpreter tflite;
private TensorImage inputImageBuffer;
private  int imageSizeX;
private  int imageSizeY;
private TensorBuffer outputProbabilityBuffer;
private static final float PROBABILITY_MEAN = 0.0f;
private static final float PROBABILITY_STD = 1.0f;
Context context;
ResultBitmap resultBitmap;
Bitmap bitmap;
String modelName;
public void run(Context context, ResultBitmap resultBitmap, Bitmap bitmap, String modelName) {
    this.context=context;
    this.resultBitmap= resultBitmap;
    this.bitmap=bitmap;
    this.modelName=modelName;
    try{
        tflite=new Interpreter(loadmodelfile((Activity) context));
    }catch (Exception e) {
        e.printStackTrace();
    }
    int imageTensorIndex = 0;
    int[] imageShape = tflite.getInputTensor(imageTensorIndex).shape();
    imageSizeY = imageShape[1];
    imageSizeX = imageShape[2];
    DataType imageDataType = tflite.getInputTensor(imageTensorIndex).dataType();
    int probabilityTensorIndex = 0;
    int[] probabilityShape = tflite.getOutputTensor(probabilityTensorIndex).shape();
    DataType probabilityDataType = tflite.getOutputTensor(probabilityTensorIndex).dataType();
    inputImageBuffer = new TensorImage(imageDataType);
    outputProbabilityBuffer = TensorBuffer.createFixedSize(probabilityShape, probabilityDataType);
    inputImageBuffer = loadImage(bitmap);
    tflite.run(inputImageBuffer.getBuffer(),outputProbabilityBuffer.getBuffer().rewind());
// here I want to get output
}
private TensorImage loadImage(final Bitmap bitmap) {
    inputImageBuffer.load(bitmap);
    ImageProcessor imageProcessor =
            new ImageProcessor.Builder()
                    .add(new ResizeOp(imageSizeX, imageSizeY, ResizeOp.ResizeMethod.BILINEAR))
                    .add(new NormalizeOp(0, 255))
                    .add(new CastOp(DataType.UINT8))
                    .build();
    return imageProcessor.process(inputImageBuffer);
}
private MappedByteBuffer loadmodelfile(Activity activity) throws IOException {
    AssetFileDescriptor fileDescriptor=activity.getAssets().openFd(modelName);
    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);
}

I want to get output (as sample output) after running this model.

Faizan Ahmad
  • 352
  • 4
  • 20

0 Answers0