Android-tensorFlowDemo

https://github.com/kbjay/tensorflowDemo

  1. tensorFlow使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

public class TensorFlowImageClassifier implements Classifier {

private TensorFlowImageClassifier() {
}

public static Classifier create(
String modelPath,
String modelFilename,
String modelScoreFileName,
String[] labels,
int inputSize,
String inputName,
String outputName) {
TensorFlowImageClassifier c = new TensorFlowImageClassifier();
/********************声明input跟output的name*********************/
c.inputName = inputName;
c.outputName = outputName;
/*************************************************************/
c.tags = labels;
String path = Environment.getExternalStorageDirectory()+modelPath + modelFilename;
String pathScore = Environment.getExternalStorageDirectory()+modelPath + modelScoreFileName;
/********************初始化TensorFlowInferenceInterface****************/
try {
c.inferenceInterface = new TensorFlowInferenceInterface(new FileInputStream(new File(path)));
c.inferenceInterfaceScore = new TensorFlowInferenceInterface(new FileInputStream(new File(pathScore)));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
/*************************************************************/
c.inputSize = inputSize;

/******************声明outPutsName***************************/
c.outputNames = new String[]{outputName};
/****************************************/

c.intValues = new int[inputSize * inputSize];
c.floatValues = new float[inputSize * inputSize * 3];

/******************初始化outPuts用来接受结果*************/
c.outputs = new float[labels.length];
c.outputScore= new float[2];
return c;
/***********************************************/
}

@Override
public List<Recognition> recognizeImage(final Bitmap bitmap) {
final ArrayList<Recognition> recognitions = new ArrayList<Recognition>();

bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
for (int i = 0; i < intValues.length; ++i) {
final int val = intValues[i];
floatValues[i * 3 + 0] = (((val >> 16) & 0xFF)) / 128f - 1.0f;
floatValues[i * 3 + 1] = (((val >> 8) & 0xFF)) / 128f - 1.0f;
floatValues[i * 3 + 2] = ((val & 0xFF)) / 128f - 1.0f;
}
/*********************分别对应输入,运行,输出****************************/
inferenceInterface.feed(inputName, floatValues, 1, inputSize, inputSize, 3);
inferenceInterface.run(outputNames, logStats);
inferenceInterface.fetch(outputName, outputs);
/***********************************************************/
int index = 0;
float maxPercent = 0f;
for (int i = 0; i < outputs.length; i++) {
if (maxPercent < outputs[i]) {
maxPercent = outputs[i];
index = i;
}
}
recognitions.add(new Recognition( "LABEL", tags[index], maxPercent, null));

inferenceInterfaceScore.feed(inputName,floatValues,1,inputSize,inputSize,3);
inferenceInterfaceScore.run(outputNames,logStats);
inferenceInterfaceScore.fetch(outputName,outputScore);
recognitions.add(new Recognition("SCORE","GOOD="+outputScore[0]+" POOR="+outputScore[1],null,null));

return recognitions;
}

@Override
public void enableStatLogging(boolean logStats) {
this.logStats = logStats;
}

@Override
public String getStatString() {
return inferenceInterface.getStatString();
}

@Override
public void close() {
inferenceInterface.close();
}
}
  1. 截取bitmap之后缩放

    参考:https://www.jianshu.com/p/da10fcc9d0c4

1
2
3
4
5
6
7
8
9
10
11
12
13
private static Bitmap getScaleBitmap(Bitmap bitmap, int size) throws IOException {

int width = bitmap.getWidth();
int height = bitmap.getHeight();
int startx = width>height?(width-height)/2:0;
int starty = width>height?0:(height-width)/2;
int cropSize=width>height?height:width;

float scaleSize = ((float) size) / cropSize;
Matrix matrix = new Matrix();
matrix.postScale(scaleSize, scaleSize);
return Bitmap.createBitmap(bitmap, startx, starty, cropSize, cropSize, matrix, true);
}

​ createBitmap:

1
2
3
4
5
6
7
8
9
10
11
12
/*
* @param source 需要裁剪的bitmap
* @param x 裁剪x点起始坐标
* @param y 裁剪y点起始坐标
* @param width 裁剪bitmap的宽度
* @param height 裁剪bitmap的高度
* @param m 针对裁剪后的bitmap进行矩阵缩放(可选)
* @param filter true(对原bitmap进行裁剪操作)
* @return A 返回一个裁剪过的bitmap 或者为操作过的(原)bitmap
*/
public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height,
Matrix m, boolean filter)