【发布时间】:2016-09-15 00:21:05
【问题描述】:
背景
Google 几个月前发布了一个新的 API 来检测位图上的人脸:
- 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
它应该比built-in FaceDetector class 更快更好,并且与它相比没有任何限制/限制(例如输入位图需要配置为 565 ,位图宽度均匀,并且具有最大面数检测)。
代码也很简单:
清单:
<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);
问题
该API似乎在某些设备上不可用,调用“isOperational()”时返回“false”。
- 在 Nexus 4 (Android 4.4.4) 和 Nexus 5 (Android 6.0.1) 上根本无法使用
- 在 Nexus 5x (Android 6.0.1) 和 Galaxy S4 (Android 5.0) 上运行良好
- 在 LG G2 (Android 4.2.2) 上,它仅在示例的第二次运行时有效。
我发现了什么
我找到了一些线索:
在 Github (here) 上,其他人也发现了这个问题。
-
示例says(在“photo-demo”中,在“PhotoViewerActivity.java”文件中)该库可能不可用,但如果不可用,它将被下载:
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)); } }
我尝试重置播放服务应用程序(这样可能会强制重新下载库),但它似乎不起作用(在 Nexus 5 上尝试过)。
问题
有没有办法强制下载库,然后开始使用 API?这个操作有监听器吗?
使用“isOperational”会触发此下载吗?
为什么它在某些 Nexus 设备上不可用?我是否在这里遗漏了一些关于使用这个库我应该知道的东西?
真的像我发现的那样对输入没有限制/限制吗?我注意到有人报告了内存问题 (here)。使用 try-catch (of OutOfMemoryError) 会帮助处理 OOM 吗?
【问题讨论】:
-
“它只在第二次运行”——意思是,第二次
isOperational()调用返回true? -
@CommonsWare 更新了问题。从第二轮开始。第一次运行返回 false。第二次运行及以后,它返回 true。
标签: android google-play-services face-detection