-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainActivity.java
398 lines (327 loc) · 13.6 KB
/
MainActivity.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package com.example.s_oss.facedetector;
import android.app.Activity;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.YuvImage;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Display;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;
//Tensorflow libs for DeepLearning
import org.tensorflow.lite.Interpreter;
//OpenCV libs for ImageProcessing
import org.opencv.android.OpenCVLoader;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
import org.opencv.android.Utils;
import org.opencv.core.MatOfByte;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;
import static java.sql.DriverManager.println;
import static org.opencv.core.CvType.CV_32F;
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {
TextView testView;
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
Camera.PictureCallback rawCallback;
Camera.ShutterCallback shutterCallback;
Camera.PictureCallback jpegCallback;
private Interpreter tflite;
public static Bitmap rotateImage(Bitmap source, float angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
matrix, true);
}
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Activity activity = this;
setContentView(R.layout.activity_main);
try {
tflite = new Interpreter(loadModelFile());
} catch (Exception ex) {
ex.printStackTrace();
}
if (OpenCVLoader.initDebug()) {
Toast.makeText(getApplicationContext(), "OpenCV loaded Successfully", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Could not Load OpenCV", Toast.LENGTH_SHORT).show();
}
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
surfaceHolder = surfaceView.getHolder();
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
surfaceHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
jpegCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
List<String> emotion_labels = Arrays.asList("angry", "disgusted", "fearful", "happy", "sad", "surprised", "neutral");
FileOutputStream outStream = null;
try {
String root = Environment.getExternalStorageDirectory().toString();
String filepath = String.format(root + String.format("/%d.jpg", System.currentTimeMillis()));
//outStream = new FileOutputStream(filepath);
//get the camera parameters
Camera.Parameters parameters = camera.getParameters();
int width = parameters.getPreviewSize().width;
int height = parameters.getPreviewSize().height;
Mat mat = Imgcodecs.imdecode(new MatOfByte(data), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
Mat rotationMatrix = Imgproc.getRotationMatrix2D(new Point(mat.rows() / 2, mat.cols() / 2), 90, 1);
Imgproc.warpAffine(mat, mat, rotationMatrix, new Size(mat.cols(), mat.cols()));
File path = new File(Environment.getExternalStorageDirectory() + "/Images/");
path.mkdirs();
File file = new File(path, "image.png");
String filename = file.toString();
Mat format_im = format_image(mat);
//float array2d[][] = Mat_to_2dArray(format_im);
float[][][][] reshape = reshape(format_im);
float[] returnedArray = new float[7];
float[][] arrayofreturnedArray = {returnedArray};
tflite.run(reshape, arrayofreturnedArray);
float maxVal = findMaxValue(arrayofreturnedArray[0]);
int maxInd = findMaxIndex(arrayofreturnedArray[0]);
String emotionText = emotion_labels.get(maxInd);
//Boolean bool = Imgcodecs.imwrite(filename, output);
TextView textview = (TextView) findViewById(R.id.textView);
textview.setText("Detected emotion: " + emotionText);
}catch (Exception e) {
e.printStackTrace();
}
refreshCamera();
}
};
}
public void captureImage(View v) throws IOException {
//take the picture
camera.takePicture(null, null, jpegCallback);
}
public void refreshCamera() {
if (surfaceHolder.getSurface() == null) {
// preview surface does not exist
return;
}
// stop preview before making changes
try {
camera.stopPreview();
} catch (Exception e) {
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or
// reformatting changes here
// start preview with new settings
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (Exception e) {
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Camera.Parameters parameters = camera.getParameters();
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
if (display.getRotation() == Surface.ROTATION_0) {
camera.setDisplayOrientation(90);
}
if (display.getRotation() == Surface.ROTATION_90) {
}
if (display.getRotation() == Surface.ROTATION_180) {
}
if (display.getRotation() == Surface.ROTATION_270) {
camera.setDisplayOrientation(180);
}
camera.setParameters(parameters);
refreshCamera();
}
private int FFC() {
int cameraCount = 0;
int cam = 0;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
cameraCount = Camera.getNumberOfCameras();
for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
Camera.getCameraInfo(camIdx, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
try {
cam = camIdx;
} catch (RuntimeException e) {
Log.e("err: ", "Camera failed to open: " + e.getLocalizedMessage());
}
}
}
return cam;
}
public void surfaceCreated(SurfaceHolder holder) {
try {
// open the camera
camera = Camera.open(FFC());
} catch (RuntimeException e) {
// check for exceptions
System.err.println(e);
return;
}
Camera.Parameters param;
param = camera.getParameters();
// modify parameter
param.setPreviewSize(352, 288);
camera.setParameters(param);
try {
// The Surface has been created, now tell the camera where to draw
// the preview.
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (Exception e) {
// check for exceptions
System.err.println(e);
return;
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// stop preview and release camera
camera.stopPreview();
camera.release();
camera = null;
}
private MappedByteBuffer loadModelFile() throws IOException {
AssetFileDescriptor fileDescriptor = this.getAssets().openFd("model.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);
}
private Mat format_image(Mat image) {
// load cascade file from application resources
File mCascadeFile = null;
try {
InputStream is = getResources().getAssets().open("haarcascade_frontalface_default.xml");
File cascadeDir = getDir("cascade", Context.MODE_PRIVATE);
mCascadeFile = new File(cascadeDir, "haarcascade_frontalface_default.xml");
FileOutputStream os = new FileOutputStream(mCascadeFile);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
is.close();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
CascadeClassifier cascade_classifier = new CascadeClassifier(mCascadeFile.getAbsolutePath());
cascade_classifier.load(mCascadeFile.getAbsolutePath());
if (cascade_classifier.empty()) {
Toast.makeText(getApplicationContext(), "Can not find haarcascade file", Toast.LENGTH_SHORT).show();
}
if (image.channels() > 2) {
Imgproc.cvtColor(image, image, Imgproc.COLOR_RGB2GRAY);
} else {
}
MatOfRect faceVectors = new MatOfRect();
cascade_classifier.detectMultiScale(image, faceVectors, 1.1, 2);
Rect[] faces = faceVectors.toArray();
Rect[] max_area_face = faces;
for (Rect face : faces) {
System.out.println(face);
if (face.width * face.height > max_area_face[0].height * max_area_face[0].width) {
max_area_face[0] = face;
}
}
Rect face = max_area_face[0];
for (Rect rect : faceVectors.toArray()) {
Imgproc.rectangle(image, new Point(rect.x, rect.y),
new Point(rect.x + rect.width, rect.y + rect.height),
new Scalar(0, 255, 0));
face = new Rect(rect.x, rect.y, rect.width, rect.height);
}
Mat image_roi = new Mat(image, face);
try {
int scaledWidth = 64;
int scaledHeight = 64;
Size sz = new Size(scaledWidth, scaledHeight);
int interpolation = Imgproc.INTER_CUBIC;
Imgproc.resize(image_roi, image_roi, sz, 0, 0, interpolation);
image_roi.convertTo(image_roi, CvType.CV_32F, 1.0 / 255, 0);
} catch (Exception ex) {
ex.printStackTrace();
}
return image_roi;
}
private static float[][][][] reshape(Mat mArray) {
float[][][][] resultArray = new float[1][mArray.cols()][mArray.rows()][1];
for (int i = 0; i < mArray.cols(); i++) {
for (int j = 0; j < mArray.rows(); j++) {
resultArray[0][i][j][0] = (float) mArray.get(i, j)[0];// mArray[i][j];
}
}
return resultArray;
}
private float[][] Mat_to_2dArray(Mat image) {
int size = (int) image.total() * image.channels();
float[] data = new float[size];
image.put(0, 0, data);
float array2d[][] = new float[64][64];
for (int i = 0; i < 64; i++) {
for (int j = 0; j < 64; j++) {
array2d[i][j] = data[(j * 10) + i];
}
}
return array2d;
}
private float findMaxValue(float[] array) {
float max = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
}
return max;
}
private int findMaxIndex(float[] array) {
float max = array[0];
int index = 0;
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
index = i;
}
}
return index;
}
}