-
Notifications
You must be signed in to change notification settings - Fork 144
/
Recognizer.java
218 lines (170 loc) · 6.42 KB
/
Recognizer.java
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
package pp.facerecognizer;
import android.content.ContentResolver;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.RectF;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import java.io.FileDescriptor;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import pp.facerecognizer.env.FileUtils;
import pp.facerecognizer.ml.BlazeFace;
import pp.facerecognizer.ml.FaceNet;
import pp.facerecognizer.ml.LibSVM;
/**
* Generic interface for interacting with different recognition engines.
*/
public class Recognizer {
/**
* An immutable result returned by a Classifier describing what was recognized.
*/
public class Recognition {
/**
* A unique identifier for what has been recognized. Specific to the class, not the instance of
* the object.
*/
private final String id;
/**
* Display name for the recognition.
*/
private final String title;
/**
* A sortable score for how good the recognition is relative to others. Higher should be better.
*/
private final Float confidence;
/** Optional location within the source image for the location of the recognized object. */
private RectF location;
Recognition(
final String id, final String title, final Float confidence, final RectF location) {
this.id = id;
this.title = title;
this.confidence = confidence;
this.location = location;
}
public String getId() {
return id;
}
public String getTitle() {
return title;
}
public Float getConfidence() {
return confidence;
}
public RectF getLocation() {
return new RectF(location);
}
@Override
public String toString() {
String resultString = "";
if (id != null) {
resultString += "[" + id + "] ";
}
if (title != null) {
resultString += title + " ";
}
if (confidence != null) {
resultString += String.format("(%.1f%%) ", confidence * 100.0f);
}
if (location != null) {
resultString += location + " ";
}
return resultString.trim();
}
}
private static Recognizer recognizer;
private BlazeFace blazeFace;
private FaceNet faceNet;
private LibSVM svm;
private List<String> classNames;
private Recognizer() {}
static Recognizer getInstance (AssetManager assetManager) throws Exception {
if (recognizer != null) return recognizer;
recognizer = new Recognizer();
recognizer.blazeFace = BlazeFace.create(assetManager);
recognizer.faceNet = FaceNet.create(assetManager);
recognizer.svm = LibSVM.getInstance();
recognizer.classNames = FileUtils.readLabel(FileUtils.LABEL_FILE);
return recognizer;
}
CharSequence[] getClassNames() {
CharSequence[] cs = new CharSequence[classNames.size() + 1];
int idx = 1;
cs[0] = "+ add new person";
for (String name : classNames) {
cs[idx++] = name;
}
return cs;
}
List<Recognition> recognizeImage(Bitmap bitmap, Matrix matrix) {
synchronized (this) {
List<RectF> faces = blazeFace.detect(bitmap);
final List<Recognition> mappedRecognitions = new LinkedList<>();
for (RectF rectF : faces) {
Rect rect = new Rect();
rectF.round(rect);
FloatBuffer buffer = faceNet.getEmbeddings(bitmap, rect);
LibSVM.Prediction prediction = svm.predict(buffer);
matrix.mapRect(rectF);
int index = prediction.getIndex();
String name = classNames.get(index);
Recognition result =
new Recognition("" + index, name, prediction.getProb(), rectF);
mappedRecognitions.add(result);
}
return mappedRecognitions;
}
}
void updateData(int label, ContentResolver contentResolver, ArrayList<Uri> uris) throws Exception {
synchronized (this) {
ArrayList<float[]> list = new ArrayList<>();
for (Uri uri : uris) {
Bitmap bitmap = getBitmapFromUri(contentResolver, uri);
List<RectF> faces = blazeFace.detect(bitmap);
Rect rect = new Rect();
if (!faces.isEmpty()) {
faces.get(0).round(rect);
}
float[] emb_array = new float[FaceNet.EMBEDDING_SIZE];
faceNet.getEmbeddings(bitmap, rect).get(emb_array);
list.add(emb_array);
}
svm.train(label, list);
}
}
int addPerson(String name) {
FileUtils.appendText(name, FileUtils.LABEL_FILE);
classNames.add(name);
return classNames.size();
}
private Bitmap getBitmapFromUri(ContentResolver contentResolver, Uri uri) throws Exception {
ParcelFileDescriptor parcelFileDescriptor =
contentResolver.openFileDescriptor(uri, "r");
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);
parcelFileDescriptor.close();
return bitmap;
}
void enableStatLogging(final boolean debug){
}
void close() {
blazeFace.close();
faceNet.close();
}
}