assetfiledescriptor falledescriptor = getAssets (). openfd ("model1.tflite");
fileInputStream inputStream = new FileInptream (FileScripROMENSELEMANELEMANELEMANELEMANELEM (FILEDESCRIPTOREMANELEMANELEMANELEMANEMELEMSEREMANEMANEMEANER (); inputStream.getChannel ();
return fileChannel.map (fileChannel.mapmode.read_only, filedescriptor.getStartOffset (), filedescriptor.getDeclaredLength ());
} < /p>
Код: Выделить всё
private void runModelOnImage() {
if (imageBitmap == null) {
Toast.makeText(this, "No image selected", Toast.LENGTH_SHORT).show();
return;
}
Bitmap resizedImage = Bitmap.createScaledBitmap(imageBitmap, MODEL_INPUT_SIZE, MODEL_INPUT_SIZE, true);
ByteBuffer byteBuffer = convertBitmapToByteBuffer(resizedImage);
if (tflite == null) {
Toast.makeText(this, "Model not loaded!", Toast.LENGTH_SHORT).show();
return;
}
float[][] output = new float[1][4]; // Adjust based on the number of classes
try {
tflite.run(byteBuffer, output); // Ensure input matches expected shape
displayPrediction(output);
} catch (Exception e) {
Toast.makeText(this, "Error running model: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
private ByteBuffer convertBitmapToByteBuffer(Bitmap bitmap) {
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * 1 * MODEL_INPUT_SIZE * MODEL_INPUT_SIZE * 3); // Adding batch size
byteBuffer.order(ByteOrder.nativeOrder());
int[] intValues = new int[MODEL_INPUT_SIZE * MODEL_INPUT_SIZE];
bitmap.getPixels(intValues, 0, MODEL_INPUT_SIZE, 0, 0, MODEL_INPUT_SIZE, MODEL_INPUT_SIZE);
for (int pixelValue : intValues) {
byteBuffer.putFloat(((pixelValue >> 16) & 0xFF) / 255.0f);
byteBuffer.putFloat(((pixelValue >> 8) & 0xFF) / 255.0f);
byteBuffer.putFloat((pixelValue & 0xFF) / 255.0f);
}
return byteBuffer;
}
Подробнее здесь: https://stackoverflow.com/questions/795 ... r-of-bytes