15
15
*/
16
16
package com .google .android .gms .samples .vision .face .facetracker ;
17
17
18
+ import android .graphics .Bitmap ;
18
19
import android .graphics .Canvas ;
19
20
import android .graphics .Color ;
20
21
import android .graphics .Paint ;
22
+ import android .os .Environment ;
21
23
import android .util .Log ;
22
24
import android .util .SparseArray ;
23
25
24
26
import com .google .android .gms .samples .vision .face .facetracker .ui .camera .GraphicOverlay ;
25
27
import com .google .android .gms .vision .Detector ;
26
28
import com .google .android .gms .vision .face .Face ;
29
+ import com .google .android .gms .vision .Frame ;
27
30
31
+ import java .io .ByteArrayOutputStream ;
32
+ import java .io .File ;
33
+ import java .io .FileOutputStream ;
34
+ import java .io .OutputStreamWriter ;
28
35
import java .util .Map ;
29
36
30
37
/**
31
38
* Graphic instance for rendering face position, orientation, and landmarks within an associated
32
39
* graphic overlay view.
33
40
*/
34
- class FaceGraphic extends GraphicOverlay .Graphic {
41
+ class FaceGraphic extends GraphicOverlay .Graphic implements RecognitionInterface {
35
42
private static final String TAG = "FaceGraphic" ;
43
+
36
44
private static final float FACE_POSITION_RADIUS = 10.0f ;
37
45
private static final float ID_TEXT_SIZE = 40.0f ;
38
46
private static final float ID_Y_OFFSET = 50.0f ;
39
47
private static final float ID_X_OFFSET = -50.0f ;
40
48
private static final float BOX_STROKE_WIDTH = 5.0f ;
41
49
42
50
private static final int COLOR_CHOICES [] = {
43
- Color .BLUE ,
44
- Color .CYAN ,
45
- Color .GREEN ,
46
- Color .MAGENTA ,
47
- Color .RED ,
48
- Color .WHITE ,
49
- Color .YELLOW
51
+ Color .BLUE ,
52
+ Color .CYAN ,
53
+ Color .GREEN ,
54
+ Color .MAGENTA ,
55
+ Color .RED ,
56
+ Color .WHITE ,
57
+ Color .YELLOW
50
58
};
51
59
private static int mCurrentColorIndex = 0 ;
52
60
53
61
private Paint mFacePositionPaint ;
54
62
private Paint mIdPaint ;
55
63
private Paint mBoxPaint ;
56
64
57
- private volatile Face mFace ;
65
+ public volatile Face mFace ;
58
66
private int mFaceId ;
59
67
private float mFaceHappiness ;
60
68
69
+ public int frame_cx = 5 ; //start after 5 frames
61
70
62
71
private Thread mT ;
72
+ private CustomDetector mCustomDetector ;
73
+ private boolean IsRecognized ;
63
74
64
- FaceGraphic (GraphicOverlay overlay ) {
75
+ FaceGraphic (GraphicOverlay overlay , CustomDetector customDetector ) {
65
76
super (overlay );
66
-
77
+ mCustomDetector = customDetector ;
67
78
mCurrentColorIndex = (mCurrentColorIndex + 1 ) % COLOR_CHOICES .length ;
68
79
final int selectedColor = COLOR_CHOICES [mCurrentColorIndex ];
69
80
@@ -84,42 +95,6 @@ void setId(int id) {
84
95
mFaceId = id ;
85
96
}
86
97
87
- public void startRecognition (Face item , Map <Face , String > mFaceNameMap )
88
- {
89
- Log .i (TAG , String .format ("hashmap: %d" , mFaceNameMap .size ()));
90
- // test();
91
- // mT = new Thread(new Runnable() {
92
- // @Override
93
- // public void run() {
94
- // Log.i(TAG, "startRecognition: ");
95
- //
96
- // try {
97
- // Thread.sleep(5000);
98
- // mFaceId = 777;
99
- // } catch (InterruptedException e) {
100
- // e.printStackTrace();
101
- // }
102
- // //mFace
103
- // }
104
- // });
105
- // mT.start();
106
- }
107
-
108
- public void stopRecognition (Detector .Detections <Face > detectionResults , Map <Face , String > mFaceNameMap )
109
- {
110
- Log .i (TAG , "stopRecognition: " );
111
- // if (mT.isAlive())
112
- // {
113
- // mT.stop();
114
- // }
115
- //mFaceNameMap.remove(fa)
116
- SparseArray <Face > faces = detectionResults .getDetectedItems ();
117
- for (int i = 0 ; i < faces .size (); i ++) {
118
- Face f = faces .valueAt (i );
119
- mFaceNameMap .remove (f );
120
- }
121
- }
122
-
123
98
124
99
/**
125
100
* Updates the face instance from the detection of the most recent frame. Invalidates the
@@ -128,6 +103,20 @@ public void stopRecognition(Detector.Detections<Face> detectionResults, Map<Face
128
103
void updateFace (Face face ) {
129
104
mFace = face ;
130
105
postInvalidate ();
106
+
107
+ if (frame_cx > 0 ) {
108
+ frame_cx --;
109
+ } else {
110
+ if (!IsRecognized && mCustomDetector .recognitionHandler == null ) { //one face at time
111
+ mCustomDetector .setHandlerListener (this );
112
+
113
+ int x = (int )face .getPosition ().x ;
114
+ int y = (int )face .getPosition ().y ;
115
+ int w = (int )face .getWidth ();
116
+ int h = (int )face .getHeight ();
117
+ mCustomDetector .startRecognition (mFaceId , x , y , w , h );
118
+ }
119
+ }
131
120
}
132
121
133
122
/**
@@ -140,14 +129,18 @@ public void draw(Canvas canvas) {
140
129
return ;
141
130
}
142
131
132
+ //Log.e(TAG, "canvas.getHeight() " + canvas.getHeight()); //1440
133
+ //Log.e(TAG, "canvas.getWidth() " + canvas.getWidth()); //1080
134
+ // 960 720
135
+
143
136
// Draws a circle at the position of the detected face, with the face's track id below.
144
137
float x = translateX (face .getPosition ().x + face .getWidth () / 2 );
145
138
float y = translateY (face .getPosition ().y + face .getHeight () / 2 );
146
139
canvas .drawCircle (x , y , FACE_POSITION_RADIUS , mFacePositionPaint );
147
140
canvas .drawText ("id: " + mFaceId , x + ID_X_OFFSET , y + ID_Y_OFFSET , mIdPaint );
148
- canvas .drawText ("happiness: " + String .format ("%.2f" , face .getIsSmilingProbability ()), x - ID_X_OFFSET , y - ID_Y_OFFSET , mIdPaint );
149
- canvas .drawText ("right eye: " + String .format ("%.2f" , face .getIsRightEyeOpenProbability ()), x + ID_X_OFFSET * 2 , y + ID_Y_OFFSET * 2 , mIdPaint );
150
- canvas .drawText ("left eye: " + String .format ("%.2f" , face .getIsLeftEyeOpenProbability ()), x - ID_X_OFFSET *2 , y - ID_Y_OFFSET *2 , mIdPaint );
141
+ // canvas.drawText("happiness: " + String.format("%.2f", face.getIsSmilingProbability()), x - ID_X_OFFSET, y - ID_Y_OFFSET, mIdPaint);
142
+ // canvas.drawText("right eye: " + String.format("%.2f", face.getIsRightEyeOpenProbability()), x + ID_X_OFFSET * 2, y + ID_Y_OFFSET * 2, mIdPaint);
143
+ // canvas.drawText("left eye: " + String.format("%.2f", face.getIsLeftEyeOpenProbability()), x - ID_X_OFFSET*2, y - ID_Y_OFFSET*2, mIdPaint);
151
144
152
145
// Draws a bounding box around the face.
153
146
float xOffset = scaleX (face .getWidth () / 2.0f );
@@ -160,4 +153,11 @@ public void draw(Canvas canvas) {
160
153
}
161
154
162
155
public native void test ();
156
+
157
+ @ Override
158
+ public void onRecognized (String str ) {
159
+ Log .w (TAG , "RRRRRRRRRRRRecognized" );
160
+ Log .w (TAG , str );
161
+ IsRecognized = true ;
162
+ }
163
163
}
0 commit comments