Сообщество IT-специалистов
Ответы на любые вопросы об IT
Профессиональное развитие в IT
Удаленная работа для IT-специалистов
MainActivity:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.tensorflow.lite.DataType;
import org.tensorflow.lite.support.tensorbuffer.TensorBuffer;
import java.io.IOException;
public class MainActivity extends Activity {
private EditText mainEditText;
private EditText additionalEditText;
private TextView resultTextView;
private Model15 model; // Модель
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Инициализация UI элементов
mainEditText = findViewById(R.id.main);
additionalEditText = findViewById(R.id.additional);
resultTextView = findViewById(R.id.resultTextView);
Button predictButton = findViewById(R.id.predictButton);
// Загрузка модели
try {
model = Model15.newInstance(this);
} catch (IOException e) {
e.printStackTrace();
resultTextView.setText("Ошибка при загрузке модели");
}
private void predictDisease(float[] inputArray) {
try {
// Создание входного буфера для модели
TensorBuffer inputTensor = TensorBuffer.createFixedSize(new int[]{1, 225}, DataType.FLOAT32);
inputTensor.loadArray(inputArray);
// Запуск модели и получение результатов
Model15.Outputs outputs = model.process(inputTensor);
TensorBuffer outputTensor = outputs.getOutputFeature0AsTensorBuffer();
float[] predictions = outputTensor.getFloatArray();
Пример самого кода во вкладке:
try {
Model15 model = Model15.newInstance(context);
// Creates inputs for reference.
TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{1, 225}, DataType.FLOAT32);
inputFeature0.loadBuffer(byteBuffer);
// Runs model inference and gets result.
Model15.Outputs outputs = model.process(inputFeature0);
TensorBuffer outputFeature0 = outputs.getOutputFeature0AsTensorBuffer();
// Releases model resources if no longer used.
model.close();
} catch (IOException e) {
// TODO Handle the exception
}
Jacen11,