-
Notifications
You must be signed in to change notification settings - Fork 2
/
FaceEngineTest.java
122 lines (101 loc) · 4.49 KB
/
FaceEngineTest.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
package m.co.rh.id.a_jarwis.ml_engine.provider.component;
import static junit.framework.TestCase.assertNotNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.opencv.core.Mat;
import java.util.List;
import m.co.rh.id.a_jarwis.ml_engine.provider.MLEngineTestProviderModule;
import m.co.rh.id.a_jarwis.ml_engine.test.R;
import m.co.rh.id.aprovider.Provider;
/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class FaceEngineTest {
private static FaceEngine mFaceEngine;
private static Provider mMLEngineProvider;
@BeforeClass
public static void beforeAnyTest() {
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
mMLEngineProvider = Provider.createProvider(appContext, new MLEngineTestProviderModule());
mFaceEngine = mMLEngineProvider.get(FaceEngine.class);
}
@AfterClass
public static void afterAnyTest() {
// if provider null it means there is problem creating it
assertNotNull(mMLEngineProvider);
mMLEngineProvider.dispose();
}
@Test
public void faceEngineDetect() {
Context context = mMLEngineProvider.getContext();
Bitmap bitmap = BitmapFactory.decodeStream(context.getResources().
openRawResource(R.raw.abbas_kiarostami_0001));
List<Rect> rectList = mFaceEngine.detectFace(bitmap);
assertEquals(1, rectList.size());
}
@Test
public void faceEngineRecognize() {
Context context = mMLEngineProvider.getContext();
Bitmap face1 = BitmapFactory.decodeStream(context.getResources().
openRawResource(R.raw.abel_pacheco_0001));
List<Rect> face1List = mFaceEngine.detectFace(face1);
assertEquals(1, face1List.size());
Bitmap face2 = BitmapFactory.decodeStream(context.getResources().
openRawResource(R.raw.abel_pacheco_0002));
List<Rect> face2List = mFaceEngine.detectFace(face2);
assertEquals(1, face2List.size());
List<Rect> faceSimilar = mFaceEngine.searchFace(face1, face2);
assertEquals(1, faceSimilar.size());
Rect face1Rect = face1List.get(0);
Rect face2Rect = face2List.get(0);
faceSimilar = mFaceEngine.searchFace(
Bitmap.createBitmap(face1, face1Rect.left, face1Rect.top, face1Rect.width(), face1Rect.height()),
Bitmap.createBitmap(face2, face2Rect.left, face2Rect.top, face2Rect.width(), face2Rect.height()));
assertEquals(1, faceSimilar.size());
}
@Test
public void faceEngineRecognize_faceNotSimilar() {
Context context = mMLEngineProvider.getContext();
Bitmap face1 = BitmapFactory.decodeStream(context.getResources().
openRawResource(R.raw.abel_pacheco_0001));
List<Rect> face1List = mFaceEngine.detectFace(face1);
assertEquals(1, face1List.size());
Bitmap face2 = BitmapFactory.decodeStream(context.getResources().
openRawResource(R.raw.abbas_kiarostami_0001));
List<Rect> face2List = mFaceEngine.detectFace(face2);
assertEquals(1, face2List.size());
List<Rect> faceSimilar = mFaceEngine.searchFace(face1, face2);
assertEquals(0, faceSimilar.size());
}
@Test
public void faceEngine_isFaceSimilar() {
Context context = mMLEngineProvider.getContext();
Bitmap face1 = BitmapFactory.decodeStream(context.getResources().
openRawResource(R.raw.abel_pacheco_0001));
Mat face1List = mFaceEngine.detectFaceRaw(face1);
assertEquals(1, face1List.rows());
Bitmap face2 = BitmapFactory.decodeStream(context.getResources().
openRawResource(R.raw.abel_pacheco_0002));
Mat face2List = mFaceEngine.detectFaceRaw(face2);
assertEquals(1, face2List.rows());
boolean faceSimilar = mFaceEngine.isFaceSimilar(
face1, face2,
face1List.row(0),
face2List.row(0)
);
assertTrue(faceSimilar);
}
}