The Google Vision APIs provide two main areas of functionality. First is Face Tracking -- not to be confused with Facial Recognition -- which gives your apps the ability to detect faces, and landmarks on faces. This is useful for, for example -- writing a camera app that only takes a picture when everyone is smiling, and nobody is blinking, or for fun apps where you can superimpose hats or moustaches on people in the camera preview window. Second is recognizing visual codes such as bar codes or QR codes, and making it easy for developers to build apps with them.
With Face Detection of Google Play services, Mobile Vision API, you can detect various facial states; such as Smiling, Left Eye Open or Right Eye Open.
Modify from last post of "Google Play services Face Detection, get Landmarks (eyes, nose, etc.)". Call setClassificationType(FaceDetector.ALL_CLASSIFICATIONS) method when build FaceDetector with FaceDetector.Builder. Then call getIsSmilingProbability() method of the detected faces to check if it is similing.
//Create a Paint object for drawing with Paint myRectPaint = new Paint(); myRectPaint.setStrokeWidth(5); myRectPaint.setColor(Color.GREEN); myRectPaint.setStyle(Paint.Style.STROKE);
Paint landmarksPaint = new Paint(); landmarksPaint.setStrokeWidth(10); landmarksPaint.setColor(Color.RED); landmarksPaint.setStyle(Paint.Style.STROKE);
Paint smilingPaint = new Paint(); smilingPaint.setStrokeWidth(4); smilingPaint.setColor(Color.YELLOW); smilingPaint.setStyle(Paint.Style.STROKE);
boolean somebodySmiling = false;
//Create a Canvas object for drawing on Bitmap tempBitmap = Bitmap.createBitmap(myBitmap.getWidth(), myBitmap.getHeight(), Bitmap.Config.RGB_565); Canvas tempCanvas = new Canvas(tempBitmap); tempCanvas.drawBitmap(myBitmap, 0, 0, null);
getLandmarks() method of com.google.android.gms.vision.face.Face return a list of Landmarks (eyes, nose, etc.) found on the face. A landmark detector must be specified via setLandmarkType(int) to detect landmarks. The landmark detector may not find all possible landmarks on any given face.
//Create a Paint object for drawing with Paint myRectPaint = new Paint(); myRectPaint.setStrokeWidth(5); myRectPaint.setColor(Color.GREEN); myRectPaint.setStyle(Paint.Style.STROKE);
Paint landmarksPaint = new Paint(); landmarksPaint.setStrokeWidth(10); landmarksPaint.setColor(Color.RED); landmarksPaint.setStyle(Paint.Style.STROKE);
//Create a Canvas object for drawing on Bitmap tempBitmap = Bitmap.createBitmap(myBitmap.getWidth(), myBitmap.getHeight(), Bitmap.Config.RGB_565); Canvas tempCanvas = new Canvas(tempBitmap); tempCanvas.drawBitmap(myBitmap, 0, 0, null);
public FaceDetector.Builder setTrackingEnabled (boolean trackingEnabled) Enables or disables face tracking, which will maintain a consistent ID for each face when processing consecutive frames. Default: true If your code uses a MultiProcessor or FocusingProcessor instance, tracking must be enabled. Having tracking enabled is also recommended for handling live video. Tracking should be disabled for handling a series of non-consecutive still images. To fix the error, modify the code
FaceDetector faceDetector = new FaceDetector.Builder(getApplicationContext()).build(); faceDetector.setTrackingEnabled(false);
to:
FaceDetector faceDetector = new FaceDetector.Builder(getApplicationContext()) .setTrackingEnabled(false) .build();
In this exercise, I modify the code lab to add the feature to load photos using Intent of ACTION_GET_CONTENT, such that you can test it with your own photos. APK is available on the bottom of this post.
<TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textStyle="bold" android:textSize="18dp" android:text="Face Detection with the Mobile Vision API" />
//Create a Paint object for drawing with Paint myRectPaint = new Paint(); myRectPaint.setStrokeWidth(5); myRectPaint.setColor(Color.RED); myRectPaint.setStyle(Paint.Style.STROKE);
//Create a Canvas object for drawing on Bitmap tempBitmap = Bitmap.createBitmap(myBitmap.getWidth(), myBitmap.getHeight(), Bitmap.Config.RGB_565); Canvas tempCanvas = new Canvas(tempBitmap); tempCanvas.drawBitmap(myBitmap, 0, 0, null);
//Detect the Faces FaceDetector faceDetector = new FaceDetector.Builder(getApplicationContext()).build();
//!!! //Cannot resolve method setTrackingEnabled(boolean) //skip for now //faceDetector.setTrackingEnabled(false);
Frame frame = new Frame.Builder().setBitmap(myBitmap).build(); SparseArray<Face> faces = faceDetector.detect(frame);
- It work on RedMi 2, running Android 4.4.4, with Google Play services version 7.8.99 installed. - But cannot detect face on Nexus 7 2012 (WITHOUT front camera) running Android 5.1.1, with the same Google Play services version 7.8.99 installed! with warning of "FaceDetectorHandle﹕ Native face detector not yet available. Reverting to no-op detection".
Test on RedMi 2, running Android 4.4.4, with Google Play services version 7.8.99 installed.