加载到tflite android时将图像大小调整为224*224

dgenwo3n  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(366)

我必须把图像作为224224传递给tflite
(我正在从照相机和画廊拍摄照片)
0.8添加到getwidth和getheight会产生错误。。
我如何解决这个问题?
正常情况下,它的工作,为更准确,我需要调整图像大小为224*224
tensorimage loadimage函数

private TensorImage loadImage(Bitmap bitmap, int sensorOrientation) {
        inputImageBuffer.load(bitmap);
        int noOfRotations = sensorOrientation/90;

        int cropSize = Math.min((bitmap.getWidth()*0.8),bitmap.getHeight()0.8);

        ImageProcessor imageProcessor = new ImageProcessor.Builder()
                .add(new ResizeWithCropOrPadOp(cropSize,cropSize))
                .add(new ResizeOp(imageResizeX,imageResizeY,ResizeOp.ResizeMethod.NEAREST_NEIGHBOR))
                .add(new Rot90Op(sensorOrientation))
                .add(new NormalizeOp(IMAGE_MEAN, IMAGE_STD))
                .build();
        return  imageProcessor.process(inputImageBuffer);
    }

完整代码类image classifier.java

package com.example.coco_classif;

import android.app.Activity;
import android.graphics.Bitmap;

import org.checkerframework.checker.nullness.qual.NonNull;
import org.tensorflow.lite.DataType;
import org.tensorflow.lite.Interpreter;
import org.tensorflow.lite.support.common.FileUtil;
import org.tensorflow.lite.support.common.TensorProcessor;
import org.tensorflow.lite.support.common.ops.NormalizeOp;
import org.tensorflow.lite.support.image.ImageProcessor;
import org.tensorflow.lite.support.image.TensorImage;
import org.tensorflow.lite.support.image.ops.ResizeOp;
import org.tensorflow.lite.support.image.ops.ResizeWithCropOrPadOp;
import org.tensorflow.lite.support.image.ops.Rot90Op;
import org.tensorflow.lite.support.label.TensorLabel;
import org.tensorflow.lite.support.tensorbuffer.TensorBuffer;

import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ImageClassifier {

    private static final float PROBABILITY_MEAN = 0.0f;
    private static final float PROABILITY_STD = 255.0f ;
    private static final float IMAGE_STD = 1.0f;
    private static final float IMAGE_MEAN = 0.0f;
    private final Interpreter tensorClassifier;
    private final int imageResizeX;
    private final List<String>labels;
    private final int imageResizeY;
    private  TensorImage inputImageBuffer;
    private final TensorBuffer probabilityImageBuffer;
    private final TensorProcessor probabilityProcessor;

    public  ImageClassifier(Activity activity) throws IOException {

        MappedByteBuffer classfireModel = FileUtil.loadMappedFile(activity,"cocomodel_quant.tflite");

         labels = FileUtil.loadLabels(activity, "coco_Label.txt");

        tensorClassifier = new Interpreter(classfireModel,null);

        int imageTensorIndex = 0;
        int probablityTensorIndex =0;

        int[] inputImageShape = tensorClassifier.getInputTensor(imageTensorIndex).shape();
        DataType inputDataType = tensorClassifier.getInputTensor(imageTensorIndex).dataType();

        int[] outputImaageShape = tensorClassifier.getOutputTensor(probablityTensorIndex).shape();
        DataType outputDataType = tensorClassifier.getOutputTensor(probablityTensorIndex).dataType();

        imageResizeX = inputImageShape[1];
        imageResizeY = inputImageShape[2];

        inputImageBuffer = new TensorImage(inputDataType);

        probabilityImageBuffer = TensorBuffer.createFixedSize(outputImaageShape,outputDataType);

        probabilityProcessor = new TensorProcessor.Builder().add(new NormalizeOp(PROBABILITY_MEAN,PROABILITY_STD)).build();

    }
    public List<Recognition>recognizeImage(final Bitmap bitmap, final  int sensorOriwentation){
        List<Recognition> recognitions = new ArrayList<>();
        inputImageBuffer = loadImage(bitmap,sensorOriwentation);
        tensorClassifier.run(inputImageBuffer.getBuffer(),probabilityImageBuffer.getBuffer().rewind());
        Map<String,Float> labelledProbability = new TensorLabel(labels,
                probabilityProcessor.process(probabilityImageBuffer)).getMapWithFloatValue();

        for (Map.Entry<String, Float>entry : labelledProbability.entrySet()){
            recognitions.add(new Recognition(entry.getKey(),entry.getValue()));
        }
        return recognitions;
    }

    private TensorImage loadImage(Bitmap bitmap, int sensorOrientation) {
        inputImageBuffer.load(bitmap);
        int noOfRotations = sensorOrientation/90;

        int cropSize = Math.min((bitmap.getWidth()),bitmap.getHeight());
       // Bitmap resized = Bitmap.createScaledBitmap(bitmap,(int)(bitmap.getWidth()*0.8), (int)(bitmap.getHeight()*0.8), true);

        ImageProcessor imageProcessor = new ImageProcessor.Builder()
                .add(new ResizeWithCropOrPadOp(cropSize,cropSize))
                .add(new ResizeOp(imageResizeX,imageResizeY,ResizeOp.ResizeMethod.NEAREST_NEIGHBOR))
                .add(new Rot90Op(sensorOrientation))
                .add(new NormalizeOp(IMAGE_MEAN, IMAGE_STD))
                .build();
        return  imageProcessor.process(inputImageBuffer);
    }

    class Recognition implements Comparable{
        private String name;
        private float confidance;

        public Recognition(){

        }

        public Recognition(String name, float confidance) {
            this.name = name;
            this.confidance = confidance;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public float getConfidance() {
            return confidance;
        }

        public void setConfidance(float confidance) {
            this.confidance = confidance;
        }

        @Override
        public String toString() {
            return "Recognition{" +
                    "name='" + name + '\'' +
                    ", confidance=" + confidance +
                    '}';
        }

        @Override
        public int compareTo(Object o) {
            return Float.compare(((Recognition)o).confidance,this.confidance);
        }
    }

}

谢谢你的帮助

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题