Guest User

MainActivity.java

a guest
Oct 30th, 2025
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.72 KB | Source Code | 0 0
  1. package com.smantass.std;
  2.  
  3. import org.tensorflow.lite.DataType;
  4. import org.tensorflow.lite.support.tensorbuffer.TensorBuffer;
  5.  
  6. import androidx.annotation.Nullable;
  7. import androidx.appcompat.app.AppCompatActivity;
  8.  
  9. import android.Manifest;
  10. import android.content.Intent;
  11. import android.content.pm.PackageManager;
  12. import android.graphics.Bitmap;
  13. import android.media.ThumbnailUtils;
  14. import android.os.Bundle;
  15. import android.provider.MediaStore;
  16. import android.view.View;
  17. import android.widget.Button;
  18. import android.widget.ImageView;
  19. import android.widget.TextView;
  20.  
  21. import com.smantass.std.ml.Model;
  22.  
  23. import java.io.IOException;
  24. import java.nio.ByteBuffer;
  25. import java.nio.ByteOrder;
  26.  
  27. public class MainActivity extends AppCompatActivity {
  28.  
  29.     TextView result, confidence;
  30.     ImageView imageView;
  31.     Button picture;
  32.     int imageSize = 224;
  33.  
  34.     @Override
  35.     protected void onCreate(Bundle savedInstanceState) {
  36.         super.onCreate(savedInstanceState);
  37.         setContentView(R.layout.activity_main);
  38.  
  39.         result = findViewById(R.id.result);
  40.         confidence = findViewById(R.id.confidence);
  41.         imageView = findViewById(R.id.imageView);
  42.         picture = findViewById(R.id.button);
  43.  
  44.         picture.setOnClickListener(new View.OnClickListener() {
  45.             @Override
  46.             public void onClick(View view) {
  47.                 // Launch camera if we have permission
  48.                 if (checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
  49.                     Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  50.                     startActivityForResult(cameraIntent, 1);
  51.                 } else {
  52.                     //Request camera permission if we don't have it.
  53.                     requestPermissions(new String[]{Manifest.permission.CAMERA}, 100);
  54.                 }
  55.             }
  56.         });
  57.     }
  58.     public void classifyImage(Bitmap image){
  59.  
  60.         try {
  61.             Model model = Model.newInstance(getApplicationContext());
  62.  
  63.             // Creates inputs for reference.
  64.             TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{1, 224, 224, 3}, DataType.FLOAT32);
  65.             ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * imageSize * imageSize * 3);
  66.             byteBuffer.order(ByteOrder.nativeOrder());
  67.  
  68.             int [] intValues = new int[imageSize * imageSize];
  69.             image.getPixels(intValues, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight());
  70.             int pixel = 0;
  71.             for (int i = 0; i < imageSize; i++){
  72.                 for (int j = 0; j < imageSize; j++){
  73.                     int val = intValues[pixel++];
  74.                     byteBuffer.putFloat(((val >> 16) & 0xFF) * (1.f / 255.f));
  75.                     byteBuffer.putFloat(((val >> 8) & 0xFF) * (1.f / 255.f));
  76.                     byteBuffer.putFloat((val & 0xFF) * (1.f / 255.f));
  77.                 }
  78.             }
  79.  
  80.             inputFeature0.loadBuffer(byteBuffer);
  81.  
  82.             // Runs model inference and gets result.
  83.             Model.Outputs outputs = model.process(inputFeature0);
  84.             TensorBuffer outputFeature0 = outputs.getOutputFeature0AsTensorBuffer();
  85.  
  86.             float[] confidences = outputFeature0.getFloatArray();
  87.             int maxPos = 0;
  88.             float maxConfidences = 0;
  89.             for (int i = 0; i < confidences.length; i++){
  90.                 if (confidences[i] > maxConfidences){
  91.                     maxConfidences = confidences[i];
  92.                     maxPos = i;
  93.                 }
  94.             }
  95.             String[] classes = {"Organik", "Anorganik", "Plastik", "Sampah B3", "Nonsampah"};
  96.  
  97.             result.setText(classes[maxPos]);
  98.  
  99.             String s = "";
  100.             for (int i = 0; i < classes.length; i++){
  101.                 s += String.format("%s: %.1f%%\n", classes[i], confidences[i] * 100);
  102.             }
  103.             confidence.setText(s);
  104.  
  105.             // Releases model resources if no longer used.
  106.             model.close();
  107.         } catch (IOException e) {
  108.             // TODO Handle the exception
  109.         }
  110.  
  111.     }
  112.  
  113.     @Override
  114.     public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  115.         if (requestCode == 1 && resultCode == RESULT_OK) {
  116.             Bitmap image = (Bitmap) data.getExtras().get("data");
  117.             int dimension = Math.min(image.getWidth(), image.getHeight());
  118.             image = ThumbnailUtils.extractThumbnail(image, dimension, dimension);
  119.             imageView.setImageBitmap(image);
  120.  
  121.             image = Bitmap.createScaledBitmap(image, imageSize, imageSize, false);
  122.             classifyImage(image);
  123.         }
  124.         super.onActivityResult(requestCode, resultCode, data);
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment