Background
Google has released a few months ago a new API to detect faces on bitmaps:
- http://android-developers.blogspot.co.il/2015/08/face-detection-in-google-play-services.html
- https://developers.google.com/vision/introduction#apis
- https://developers.google.com/vision/face-detection-concepts#face_orientation
- https://search-codelabs.appspot.com/codelabs/face-detection#1
- https://github.com/googlesamples/android-vision
It's supposed to be faster and better than the built-in FaceDetector class, and it has no restrictions/limitations compared to it (like the need that the input bitmap will be with config 565 , bitmap width that's even, and have a max faces to detect).
The code is pretty simple, too:
manifest:
<meta-data
android:name="com.google.android.gms.vision.DEPENDENCIES"
android:value="face" />
java:
FaceDetector faceDetector = new
FaceDetector.Builder(getApplicationContext()).setTrackingEnabled(false)
.build();
if(!faceDetector.isOperational()){
//show error
return;
}
Frame frame = new Frame.Builder().setBitmap(myBitmap).build();
SparseArray<Face> faces = faceDetector.detect(frame);
The problem
It seems that the API isn't available on some devices, returning "false" when calling "isOperational()".
- On Nexus 4 (Android 4.4.4) and Nexus 5 (Android 6.0.1) it doesn't work at all
- On Nexus 5x (Android 6.0.1) and Galaxy S4 (Android 5.0) it works fine
- On LG G2 (Android 4.2.2) it worked only from the second run of the sample.
What I've found
I've found some clues:
On Github (here), other people also found this issue.
The sample says (in "photo-demo", inside "PhotoViewerActivity.java" file) the library might not be available, yet that if it's not, it will be downloaded :
if (!safeDetector.isOperational()) { // Note: The first time that an app using face API is installed on a device, GMS will // download a native library to the device in order to do detection. Usually this // completes before the app is run for the first time. But if that download has not yet // completed, then the above call will not detect any faces. // // isOperational() can be used to check if the required native library is currently // available. The detector will automatically become operational once the library // download completes on device. Log.w(TAG, "Face detector dependencies are not yet available."); // Check for low storage. If there is low storage, the native library will not be // downloaded, so detection will not become operational. IntentFilter lowstorageFilter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW); boolean hasLowStorage = registerReceiver(null, lowstorageFilter) != null; if (hasLowStorage) { Toast.makeText(this, R.string.low_storage_error, Toast.LENGTH_LONG).show(); Log.w(TAG, getString(R.string.low_storage_error)); } }
I tried to reset the play-services app (so that maybe the library will be forced to be re-downloaded), but it didn't seem to work (tried on Nexus 5).
The question
Is there a way to force the library to be downloaded, and then start using the API? Is there a listener to this operation?
Would using "isOperational" trigger this download?
How come it's not available on some Nexus devices? Am I missing here something that I should have known about using this library?
Is it really without limitations/restrictions on the input, as I've found? I've noticed someone reported about memory issues (here). Would using try-catch (of OutOfMemoryError) help handling OOM ?
isOperational()
call returnstrue
?