通过android studio打开在官方给你的demo里的TensorflowImageClassifier
// Find the best classifications.
PriorityQueue<Recognition> pq =
new PriorityQueue<Recognition>(
3,
new Comparator<Recognition>() {
@Override
public int compare(Recognition lhs, Recognition rhs) {
// Intentionally reversed to put high confidence at the head of the queue.
return Float.compare(rhs.getConfidence(), lhs.getConfidence());
}
});
for (int i = 0; i < outputs.length; ++i) {
if (outputs[i] > THRESHOLD) {
pq.add(
new Recognition(
"" + i, labels.size() > i ? labels.get(i) : "unknown", outputs[i], null));
}
}
final ArrayList<Recognition> recognitions = new ArrayList<Recognition>();
int recognitionsSize = Math.min(pq.size(), MAX_RESULTS);
//for (int i = 0; i < recognitionsSize; ++i) {//我将输出每一个值改为只输出最接近的一个值
for (int i = 0; i < 1; ++i) { //out the most precise picture
recognitions.add(pq.poll());
}
Trace.endSection(); // "recognizeImage"
return recognitions;
}
然后在ClassifierActivity中将读取模型改为以下
private static final int NUM_CLASSES = 9;
private static final int INPUT_SIZE = 299;
private static final int IMAGE_MEAN = 128;
private static final float IMAGE_STD = 128;
private static final String INPUT_NAME = "Mul";
private static final String OUTPUT_NAME = "final_result";
private static final String MODEL_FILE = "file:///android_asset/optimized_graph.pb";
private static final String LABEL_FILE =
"file:///android_asset/output_labels.txt";
最后通过蓝牙串送出来
public void run() {
final long startTime = SystemClock.uptimeMillis();
final List<Classifier.Recognition> results = classifier.recognizeImage(croppedBitmap);
//lastProcessingTimeMs = SystemClock.uptimeMillis() - startTime;
//LOGGER.i("Detect: %s", results);
cropCopyBitmap = Bitmap.createBitmap(croppedBitmap);
if (resultsView == null) {
resultsView = (ResultsView) findViewById(R.id.results);
}
resultsView.setResults(results);
mtmp = "" + results;
//mtext = mtmp;
arr = mtmp.toCharArray();
if( arr[3] == ']' )
{
mtext = "" + arr[2];
}
else if( arr[3] == '0' )
{
mtext = "a";
}
else if( arr[3] == '1' )
{
mtext = "b";
}
requestRender();
readyForNextImage();
selectDevice=mBluetoothAdapter.getRemoteDevice("80:CA:9E:A1:84:47");
try {
// 判断客户端接口是否为空
if (clientSocket == null) {
// 获取到客户端接口
clientSocket = selectDevice.createRfcommSocketToServiceRecord(MY_UUID);
// 向服务端发送连接
clientSocket.connect();
// 获取到输出流,向外写数据
os = clientSocket.getOutputStream();
}
// 判断是否拿到输出流
if (os != null) {
// 需要发送的信息
//String text ="1";
// 以utf-8的格式发送出去
os.write(mtext.getBytes("UTF-8"));
}
// 吐司一下,告诉用户发送成功
//Toast.makeText(getApplicationContext(), "发送信息成功,请查收", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
// 如果发生异常则告诉用户发送失败
Toast.makeText(getApplicationContext(), "发送信息失败", Toast.LENGTH_SHORT).show();
}
}
先将之前优化的optimized_graph.pb和output_labels.txt放入/opt/tensorflow/tensorflow/examples/android/assets文件夹下
之后在tensorflow文件夹(源代码解压出的文件夹)打开终端输入
bazel build -c opt //tensorflow/examples/android:tensorflow_demo
最后/opt/tensorflow/bazel-bin/tensorflow/examples/android文件夹下的apk文件取出放到手机上即可