Advertisement
Guest User

image classifier

a guest
May 22nd, 2021
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.06 KB | None | 0 0
  1. package com.example.chesshelper;
  2.  
  3. import android.app.Activity;
  4. import android.graphics.Bitmap;
  5.  
  6. import org.checkerframework.checker.nullness.qual.NonNull;
  7. import org.tensorflow.lite.DataType;
  8. import org.tensorflow.lite.Interpreter;
  9. import org.tensorflow.lite.support.common.FileUtil;
  10. import org.tensorflow.lite.support.common.TensorProcessor;
  11. import org.tensorflow.lite.support.common.ops.NormalizeOp;
  12. import org.tensorflow.lite.support.image.ImageProcessor;
  13. import org.tensorflow.lite.support.image.TensorImage;
  14. import org.tensorflow.lite.support.image.ops.ResizeOp;
  15. import org.tensorflow.lite.support.image.ops.ResizeWithCropOrPadOp;
  16. import org.tensorflow.lite.support.label.TensorLabel;
  17. import org.tensorflow.lite.support.tensorbuffer.TensorBuffer;
  18.  
  19. import java.io.IOException;
  20. import java.nio.MappedByteBuffer;
  21. import java.util.ArrayList;
  22. import java.util.Collections;
  23. import java.util.List;
  24. import java.util.Map;
  25.  
  26. public class ImageIdentifier {
  27.     private static final float PROBABILITY_MEAN = 0.0f;
  28.     private static final float PROBABILITY_STD = 255.0f;
  29.     private static final float IMAGE_MEAN = 0.0f;
  30.     private static final float IMAGE_STD = 1.0f;
  31.     private final int imageResizeX;
  32.     private final int imageResizeY;
  33.     private final List<String> labels;
  34.     private TensorImage inputImageBuffer;
  35.     private TensorBuffer probabilityImageBuffer;
  36.     private TensorProcessor probabilityProcessor;
  37.     private Interpreter imageClassifier;
  38.     public ImageIdentifier(Activity activity) throws IOException
  39.     {
  40.         MappedByteBuffer classifierModel = FileUtil.loadMappedFile(activity, "detect.tflite");
  41.         labels = FileUtil.loadLabels(activity, "labelMap.txt");
  42.         imageClassifier = new Interpreter(classifierModel, null);
  43.         int imageTensorIndex = 0;
  44.         int outputImageTensorIndex = 0;
  45.  
  46.         int[] imageShape = imageClassifier.getInputTensor(imageTensorIndex).shape();
  47.         DataType inputData = imageClassifier.getInputTensor(imageTensorIndex).dataType();
  48.         int[] outputImageShape = imageClassifier.getOutputTensor(outputImageTensorIndex).shape();
  49.         DataType outputData = imageClassifier.getOutputTensor(outputImageTensorIndex).dataType();
  50.  
  51.         imageResizeX = imageShape[1];
  52.         imageResizeY = imageShape[2];
  53.  
  54.         inputImageBuffer = new TensorImage(inputData);
  55.         probabilityImageBuffer = TensorBuffer.createFixedSize(outputImageShape, outputData);
  56.         probabilityProcessor = new TensorProcessor.Builder().add(new NormalizeOp(PROBABILITY_MEAN, PROBABILITY_STD)).build();
  57.     }
  58.  
  59.     public List<Recognition> recognisedImage(Bitmap bitmap, int sensorOrientation)
  60.     {
  61.         List<Recognition> recognitionsList = new ArrayList<>();
  62.         inputImageBuffer = loadImage(bitmap, sensorOrientation);
  63.         imageClassifier.run(inputImageBuffer.getBuffer(), probabilityImageBuffer.getBuffer().rewind());
  64.         Map<String, Float> labelledProbability = new TensorLabel(labels,
  65.                 probabilityProcessor.process(probabilityImageBuffer)).getMapWithFloatValue();
  66.         for (Map.Entry<String, Float> entry : labelledProbability.entrySet())
  67.         {
  68.             recognitionsList.add(new Recognition(entry.getKey(), entry.getValue()));
  69.         }
  70.         Collections.sort(recognitionsList);
  71.         recognitionsList.subList(0, 5 > recognitionsList.size() ? recognitionsList.size() : 5).clear();
  72.         return recognitionsList;
  73.     }
  74.  
  75.     private TensorImage loadImage(Bitmap bitmap, int sensorOrientation) {
  76.         inputImageBuffer.load(bitmap);
  77.         int noOfRotations = sensorOrientation / 90;
  78.         int cropSize = Math.min(bitmap.getWidth(), bitmap.getHeight());
  79.         ImageProcessor imageProcessor = new ImageProcessor.Builder()
  80.                 .add(new ResizeWithCropOrPadOp(cropSize, cropSize))
  81.                 .add(new ResizeOp(imageResizeX, imageResizeY, ResizeOp.ResizeMethod.NEAREST_NEIGHBOR))
  82.                 .add(new NormalizeOp(IMAGE_MEAN, IMAGE_STD)).build();
  83.         return imageProcessor.process(inputImageBuffer);
  84.     }
  85.  
  86.     class Recognition implements Comparable
  87.     {
  88.         private String name;
  89.         private float confidence;
  90.  
  91.         public Recognition() {
  92.         }
  93.  
  94.         public Recognition(String name, float confidence) {
  95.             this.name = name;
  96.             this.confidence = confidence;
  97.         }
  98.  
  99.         public String getName() {
  100.             return name;
  101.         }
  102.  
  103.         public void setName(String name) {
  104.             this.name = name;
  105.         }
  106.  
  107.         public float getConfidence() {
  108.             return confidence;
  109.         }
  110.  
  111.         public void setConfidence(float confidence) {
  112.             this.confidence = confidence;
  113.         }
  114.  
  115.         @Override
  116.         public String toString() {
  117.             return "Recognition{" +
  118.                     "name='" + name + '\'' +
  119.                     ", confidence=" + confidence +
  120.                     '}';
  121.         }
  122.  
  123.         @Override
  124.         public int compareTo(Object object) {
  125.             return Float.compare(((Recognition)object).confidence, this.confidence);
  126.         }
  127.     }
  128. }
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement