25
25
public class LookAtMe extends VideoView {
26
26
27
27
private String status = "" ;
28
+ private String smilingStatus = "" ;
28
29
private CameraSource cameraSource ;
29
30
private Context activityContext ;
31
+ private int timesSmiled = 0 ;
30
32
31
33
public void init (Context activityContext ) {
32
34
this .activityContext = activityContext ;
@@ -43,6 +45,11 @@ public void init(Context activityContext, String mode){
43
45
createCameraSource (mode );
44
46
}
45
47
48
+ public void initWithSmilingStatus (Context activityContext ){
49
+ this .activityContext = activityContext ;
50
+ createCameraSourceWithSmilingStatus ();
51
+ }
52
+
46
53
public void resume (){
47
54
if (cameraSource != null ) {
48
55
try {
@@ -51,7 +58,6 @@ public void resume(){
51
58
Toast .makeText (activityContext , "Grant Permission and restart app" , Toast .LENGTH_SHORT ).show ();
52
59
}
53
60
cameraSource .start ();
54
- //Log.d("some8","Starting first");
55
61
}
56
62
catch (IOException e ) {
57
63
e .printStackTrace ();
@@ -62,11 +68,10 @@ public void resume(){
62
68
public void paused (){
63
69
if (cameraSource != null ) {
64
70
cameraSource .stop ();
65
- //Log.d("some6","stopped here");
66
71
}
72
+
67
73
if (this .isPlaying ()) {
68
74
this .pause ();
69
- //Log.d("some7","Paused() here");
70
75
}
71
76
}
72
77
@@ -114,17 +119,23 @@ public String getStatus(){
114
119
return status ;
115
120
}
116
121
117
- public void setLookMe (){
118
- //Log.d("this is found","camerasource is " + cameraSource);
122
+ public String getSmilingStatus (){
123
+ return smilingStatus ;
124
+ }
125
+
126
+ public int getTimesSmiled (){
127
+ return timesSmiled ;
128
+ }
119
129
130
+ public void setLookMe (){
120
131
try {
121
132
if (ActivityCompat .checkSelfPermission (activityContext , Manifest .permission .CAMERA ) != PackageManager .PERMISSION_GRANTED ) {
122
133
ActivityCompat .requestPermissions ((Activity ) activityContext , new String []{Manifest .permission .CAMERA }, 1 );
123
134
124
135
Toast .makeText (activityContext , "Grant Permission and restart app" , Toast .LENGTH_SHORT ).show ();
125
136
}
126
137
cameraSource .start ();
127
- Log .d ("ReadThis" , "camerasource started, outside" );
138
+ Log .d ("ReadThis" , "camera-source started, outside" );
128
139
}
129
140
catch (IOException e ) {
130
141
e .printStackTrace ();
@@ -142,17 +153,14 @@ private EyesTracker() {
142
153
@ Override
143
154
public void onUpdate (Detector .Detections <Face > detections , Face face ) {
144
155
if (face .getIsLeftEyeOpenProbability () > THRESHOLD || face .getIsRightEyeOpenProbability () > THRESHOLD ) {
145
- // Log.d("some9", "Eyes Detecting");
146
156
147
157
if (!isPlaying ())
148
158
start ();
149
159
150
160
status = "Eyes Detected and open, so video continues" ;
151
- //Log.d("some3", "Started due to eyes detected");
152
161
}
153
162
else {
154
163
if (isPlaying ()){
155
- // Log.d("some4","Paused due to eyes closed");
156
164
pause ();
157
165
}
158
166
@@ -165,7 +173,43 @@ public void onMissing(Detector.Detections<Face> detections) {
165
173
super .onMissing (detections );
166
174
status = "Face Not Detected!" ;
167
175
pause ();
168
- //Log.d("some5","Face is Missing");
176
+ }
177
+
178
+ @ Override
179
+ public void onDone () {
180
+ super .onDone ();
181
+ }
182
+ }
183
+
184
+ private class FaceTracker extends Tracker <Face > {
185
+ private final float THRESHOLD = 0.75f ;
186
+
187
+ private FaceTracker (){
188
+
189
+ }
190
+
191
+ @ Override
192
+ public void onUpdate (Detector .Detections <Face > detections , Face face ) {
193
+ if (face .getIsSmilingProbability () > THRESHOLD ) {
194
+ smilingStatus = "smiling" ;
195
+ timesSmiled ++;
196
+ Log .d ("smile" , String .valueOf (getTimesSmiled ()));
197
+
198
+ if (timesSmiled > 100 ){
199
+ timesSmiled -= 100 ;
200
+
201
+ Log .d ("Smiling" ,"You smiled for 100 frames!" );
202
+
203
+ }
204
+ }
205
+ else {
206
+ smilingStatus = "Not smiling" ;
207
+ }
208
+ }
209
+
210
+ @ Override
211
+ public void onMissing (Detector .Detections <Face > detections ) {
212
+ super .onMissing (detections );
169
213
}
170
214
171
215
@ Override
@@ -188,7 +232,27 @@ private void createCameraSource() {
188
232
}
189
233
else
190
234
cameraSource .start ();
191
- //Log.d("ReadThis", "camerasource started, inside");
235
+ }
236
+ catch (IOException e ) {
237
+ e .printStackTrace ();
238
+ }
239
+ }
240
+
241
+ private void createCameraSourceWithSmilingStatus () {
242
+ FaceDetector detector = new FaceDetector .Builder (activityContext ).setTrackingEnabled (true ).setClassificationType (FaceDetector .ALL_CLASSIFICATIONS ).setMode (FaceDetector .FAST_MODE ).build ();
243
+
244
+ FaceTracker faceTracker = new FaceTracker ();
245
+ detector .setProcessor (new LargestFaceFocusingProcessor (detector , faceTracker ));
246
+
247
+ cameraSource = new CameraSource .Builder (activityContext , detector ).setRequestedPreviewSize (1024 , 768 ).setFacing (CameraSource .CAMERA_FACING_FRONT ).setRequestedFps (30.0f ).build ();
248
+
249
+ try {
250
+ if (ActivityCompat .checkSelfPermission (activityContext , Manifest .permission .CAMERA ) != PackageManager .PERMISSION_GRANTED ) {
251
+ ActivityCompat .requestPermissions ((Activity ) activityContext , new String []{Manifest .permission .CAMERA }, 1 );
252
+ Toast .makeText (activityContext , "Grant Permission and restart app" , Toast .LENGTH_SHORT ).show ();
253
+ }
254
+ else
255
+ cameraSource .start ();
192
256
}
193
257
catch (IOException e ) {
194
258
e .printStackTrace ();
@@ -217,7 +281,6 @@ private void createCameraSource(String mode) {
217
281
}
218
282
else
219
283
cameraSource .start ();
220
- //Log.d("ReadThis", "camerasource started, inside");
221
284
}
222
285
catch (IOException e ) {
223
286
e .printStackTrace ();
@@ -226,7 +289,7 @@ private void createCameraSource(String mode) {
226
289
227
290
private void createCameraSource (String mode , String cameraFace ) {
228
291
229
- // Let the mode be fast or accurate
292
+ // Let the mode be fast or accurate and
230
293
// Let the cameraFace be front or back
231
294
232
295
FaceDetector detector ;
@@ -250,7 +313,6 @@ private void createCameraSource(String mode, String cameraFace) {
250
313
}
251
314
else
252
315
cameraSource .start ();
253
- //Log.d("ReadThis", "camerasource started, inside");
254
316
}
255
317
catch (IOException e ) {
256
318
e .printStackTrace ();
0 commit comments