我是android开发的新手, 我需要获得一个经过预先训练的图像分类模型,并使用我选择的图像来运行它, 我设法从图库中选择图像,并在加载TFLite模型的同时从URI中检索图像
我还看到了有关TFLite模型转换的教程,并设法运行了一个将C度转换为F度的简单模型
我无法通过图片执行此操作,有帮助吗?
用于图像选择和模型加载的Android代码:
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import org.tensorflow.lite.Interpreter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class MainActivity extends AppCompatActivity {
Button btnChoose;
ImageView imageView;
Interpreter tflite;
TextView textView;
String[] classes;
private static int GALLERY_REQUEST_CODE = 35;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
textView = findViewById(R.id.textView);
btnChoose = findViewById(R.id.btnChoose);
btnChoose.setOnClickListener(new View.OnClickListener() {
@SuppressLint("IntentReset")
@Override
public void onClick(View v) {
Intent intent=new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
String[] mimeTypes = {"image/jpeg", "image/png"};
intent.putExtra(Intent.EXTRA_MIME_TYPES,mimeTypes);
startActivityForResult(intent,GALLERY_REQUEST_CODE);
}
});
try{
tflite = new Interpreter(loadModelFile());
} catch (Exception ex){
ex.printStackTrace();
}
try{
InputStream is = getAssets().open("labels_mobilenet_quant_v1_224.txt");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
classes = new String(buffer).split("\n");
textView.setText(classes[1]);
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_REQUEST_CODE && resultCode == Activity.RESULT_OK && null != data) {
Uri selectedImage = data.getData();
imageView.setImageURI(selectedImage);
}
}
// TODO:
/*
doInf Function
*/
private MappedByteBuffer loadModelFile() throws IOException {
AssetFileDescriptor fileDescriptor = this.getAssets().openFd("mobilenet_v1_1.0_224_quant.tflite");
FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
FileChannel fileChannel = inputStream.getChannel();
long startOffset = fileDescriptor.getStartOffset();
long declaredLength = fileDescriptor.getDeclaredLength();
return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength);
}
}
我之前完成的TFLite简单模型代码
Button btn;
TextView tvOutput;
EditText etInput;
Interpreter tflite;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.button);
etInput = findViewById(R.id.etInput);
tvOutput = findViewById(R.id.tvOutput);
try{
tflite = new Interpreter(loadModelFile());
} catch (Exception ex){
ex.printStackTrace();
}
btn.setOnClickListener(new View.OnClickListener() {
@SuppressLint("SetTextI18n")
@Override
public void onClick(View v) {
float pred = doInf(etInput.getText().toString());
tvOutput.setText(Float.toString(pred));
}
});
}
public float doInf(String inputString){
float[] inputVal = new float[1];
inputVal[0] = Float.parseFloat(inputString);
float[][] outputVal = new float[1][1];
tflite.run(inputVal, outputVal);
return outputVal[0][0];
}
private MappedByteBuffer loadModelFile() throws IOException{
AssetFileDescriptor fileDescriptor = this.getAssets().openFd("linear.tflite");
FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
FileChannel fileChannel = inputStream.getChannel();
long startOffset = fileDescriptor.getStartOffset();
long declaredLength = fileDescriptor.getDeclaredLength();
return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength);
}
答案 0 :(得分:0)
似乎您正在尝试集成mobilenet的图像分类模型。尝试使用TFLite任务库,该库将运行5行代码,并自动为您封装图像处理和输出处理。请参见instruction和Android app example。