Created
June 19, 2021 05:57
-
-
Save RanaRanvijaySingh/cd06619883d719683a18149bb565dc4b 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
import androidx.appcompat.app.AppCompatActivity; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.widget.TextView; | |
import org.tensorflow.lite.support.image.TensorImage; | |
import org.tensorflow.lite.support.label.Category; | |
import org.tensorflow.lite.task.vision.classifier.Classifications; | |
import org.tensorflow.lite.task.vision.classifier.ImageClassifier; | |
import java.io.IOException; | |
import java.util.List; | |
public class HomeActivity extends AppCompatActivity { | |
protected TextView tvMessage; | |
private String TAG = HomeActivity.class.getName(); | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_home); | |
tvMessage = findViewById(R.id.tvMessage); | |
ImageClassifier.ImageClassifierOptions options = ImageClassifier | |
.ImageClassifierOptions.builder().setMaxResults(1).build(); | |
ImageClassifier imageClassifier = null; | |
try { | |
// This is where the model is being used. | |
imageClassifier = ImageClassifier.createFromFileAndOptions(this, "model.tflite", options); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
if (imageClassifier == null) { | |
Log.e(TAG, "[ImageClassifier] IS NULL"); | |
} | |
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.car); | |
TensorImage inputImage = TensorImage.fromBitmap(bitmap); | |
assert imageClassifier != null; | |
List<Classifications> results = imageClassifier.classify(inputImage); | |
if (results != null && results.get(0) != null) { | |
List<Category> categories = results.get(0).getCategories(); | |
if (categories != null && categories.get(0) != null) { | |
String msg = categories.get(0).getLabel() + " " + categories.get(0).getScore(); | |
Log.i(TAG, msg); | |
tvMessage.setText(msg); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment