I'm working on a tflite project in android studio (in Java). I'm failing to run the line:
String textToShow = imageClassifier.classifyFrame(bitmap); (getting an exception every time).
This is the ClassifyFrame function:
ImageClassifier(Activity activity) throws IOException {
tflite = new Interpreter(loadModelFile(activity));
labelList = loadLabelList(activity);
imgData =
ByteBuffer.allocateDirect(
DIM_BATCH_SIZE * DIM_IMG_SIZE_X * DIM_IMG_SIZE_Y * DIM_PIXEL_SIZE);
imgData.order(ByteOrder.nativeOrder());
labelProbArray = new float[1][labelList.size()];
Log.d(TAG, "Created a Tensorflow Lite Image Classifier.");
}
/**
* Classifies a frame from the preview stream.
*/
String classifyFrame(Bitmap bitmap) {
if (tflite == null) {
Log.e(TAG, "Image classifier has not been initialized; Skipped.");
return "Uninitialized Classifier.";
}
convertBitmapToByteBuffer(bitmap);
// Here's where the magic happens!!!
long startTime = SystemClock.uptimeMillis();
int[] dimensions = new int[4];
dimensions[0] = 1; // Batch_size // No of frames at a time
dimensions[1] = 224; // Image Width required by model
dimensions[2] = 224; // Image Height required by model
dimensions[3] = 3; // No of Pixels
//Tensor tensor = tflite.getInputTensor(0);
tflite.resizeInput(0, dimensions);
//Tensor tensor1 = tflite.getInputTensor(0);
tflite.run(imgData, labelProbArray);
long endTime = SystemClock.uptimeMillis();
Log.d(TAG, "Timecost to run model inference: " + Long.toString(endTime - startTime));
String textToShow = printTopKLabels();
textToShow = Long.toString(endTime - startTime) + "ms" + textToShow;
return textToShow;
}
/**
* Reads label list from Assets.
*/
private List<String> loadLabelList(Activity activity) throws IOException {
List<String> labelList = new ArrayList<String>();
BufferedReader reader =
new BufferedReader(new InputStreamReader(activity.getAssets().open(LABEL_PATH)));
String line;
while ((line = reader.readLine()) != null) {
labelList.add(line);
}
reader.close();
return labelList;
}
The output type for the model is float as recommended here to solve the problem: private float[][] labelProbArray = null;
This is the comment in the Logcut: E/OpenGLRenderer: ReliableSurface: perform returned an error
Would appreciate any help!
question from:
https://stackoverflow.com/questions/66046558/tflite-implantation-in-android-studio-fails-to-classify-frame 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…