Created
July 27, 2020 13:54
-
-
Save ksdkamesh99/c06ccc01cada7d47577f046ddd2d71c3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.regression; | |
| import androidx.appcompat.app.AppCompatActivity; | |
| import org.tensorflow.lite.Interpreter; | |
| import android.content.res.AssetFileDescriptor; | |
| import android.os.Bundle; | |
| import android.view.View; | |
| import android.widget.Button; | |
| import android.widget.EditText; | |
| import android.widget.TextView; | |
| import java.io.FileInputStream; | |
| import java.io.IOException; | |
| import java.nio.MappedByteBuffer; | |
| import java.nio.channels.FileChannel; | |
| public class MainActivity extends AppCompatActivity { | |
| Interpreter tflite; | |
| EditText inp; | |
| TextView outp; | |
| Button pred; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| pred=findViewById(R.id.bt); | |
| outp=findViewById(R.id.hw); | |
| inp=findViewById(R.id.et); | |
| try { | |
| tflite = new Interpreter(loadModelFile()); | |
| }catch (Exception ex){ | |
| ex.printStackTrace(); | |
| } | |
| pred.setOnClickListener(new View.OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| float prediction=doInference(inp.getText().toString()); | |
| System.out.println(prediction); | |
| outp.setText(Float.toString(prediction)); | |
| } | |
| }); | |
| } | |
| private MappedByteBuffer loadModelFile() throws IOException { | |
| AssetFileDescriptor fileDescriptor=this.getAssets().openFd("degree.tflite"); | |
| FileInputStream inputStream=new FileInputStream(fileDescriptor.getFileDescriptor()); | |
| FileChannel fileChannel=inputStream.getChannel(); | |
| long startOffset=fileDescriptor.getStartOffset(); | |
| long declareLength=fileDescriptor.getDeclaredLength(); | |
| return fileChannel.map(FileChannel.MapMode.READ_ONLY,startOffset,declareLength); | |
| } | |
| private float doInference(String inputString) { | |
| float[] inputVal=new float[1]; | |
| inputVal[0]=Float.parseFloat(inputString); | |
| float[][] output=new float[1][1]; | |
| tflite.run(inputVal,output); | |
| return output[0][0]; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment