2016-03-05 90 views
2

我想在我的Android應用程序中實現人臉檢測,所以我嘗試了這裏的官方文檔提供的示例應用程序:Google's Face Detection Sample App 我確實設法讓應用程序運行並顯示圖像,但與地標相對應的小圓圈不會出現。實際上,我在上面提到的教程中提到了有關安裝應用程序時下載的庫的說明,這意味着可能無法立即使用人臉檢測。Android - 人臉檢測庫不加載

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."); 

我的日誌包含上面的行,但等待一段時間後,我仍然沒有得到任何東西。我嘗試關閉並重新打開應用程序,並重新安裝它,但目前沒有任何工作。日誌中沒有其他內容。

這裏是應用程序的的build.gradle:

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 23 
    buildToolsVersion "22.0.1" 

defaultConfig { 
    applicationId "com.google.android.gms.samples.vision.face.photo" 
    minSdkVersion 9 
    targetSdkVersion 23 
    versionCode 1 
    versionName "1.0" 
} 
buildTypes { 
    release { 
     minifyEnabled false 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    compile 'com.android.support:appcompat-v7:23.0.0' 
    compile 'com.google.android.gms:play-services:8.4.0' 
} 

這裏是清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.google.android.gms.samples.vision.face.photo" 
    android:installLocation="auto" 
    android:versionCode="1" 
    android:versionName="1" > 

<uses-sdk 
    android:minSdkVersion="9" 
    android:targetSdkVersion="21" /> 

<application 
    android:hardwareAccelerated="true" 
    android:label="FacePhotoDemo" 
    android:allowBackup="true" 
    android:icon="@drawable/icon"> 

    <meta-data android:name="com.google.android.gms.vision.DEPENDENCIES" android:value="face"/> 

    <activity 
     android:name=".PhotoViewerActivity" 
     android:icon="@drawable/icon" 
     android:label="@string/title_activity_photo_viewer" 
     android:theme="@android:style/Theme.Black.NoTitleBar" 
     android:screenOrientation="fullSensor"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

這裏是其是當使用isOperational()方法PhotoViewerActivity:

/* 
* Copyright (C) The Android Open Source Project 
* 
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
*  http://www.apache.org/licenses/LICENSE-2.0 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
*/ 
package com.google.android.gms.samples.vision.face.photo; 

import android.app.Activity; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.util.Log; 
import android.util.SparseArray; 
import android.widget.Toast; 

import com.google.android.gms.samples.vision.face.patch.SafeFaceDetector; 
import com.google.android.gms.vision.Detector; 
import com.google.android.gms.vision.Frame; 
import com.google.android.gms.vision.face.Face; 
import com.google.android.gms.vision.face.FaceDetector; 

import java.io.InputStream; 

/** 
* Demonstrates basic usage of the GMS vision face detector by running face landmark detection on a 
* photo and displaying the photo with associated landmarks in the UI. 
*/ 
public class PhotoViewerActivity extends Activity { 
    private static final String TAG = "PhotoViewerActivity"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_photo_viewer); 

     InputStream stream = getResources().openRawResource(R.raw.face); 
     Bitmap bitmap = BitmapFactory.decodeStream(stream); 

     // A new face detector is created for detecting the face and its landmarks. 
     // 
     // Setting "tracking enabled" to false is recommended for detection with unrelated 
     // individual images (as opposed to video or a series of consecutively captured still 
     // images). For detection on unrelated individual images, this will give a more accurate 
     // result. For detection on consecutive images (e.g., live video), tracking gives a more 
     // accurate (and faster) result. 
     // 
     // By default, landmark detection is not enabled since it increases detection time. We 
     // enable it here in order to visualize detected landmarks. 
     FaceDetector detector = new FaceDetector.Builder(getApplicationContext()) 
       .setTrackingEnabled(false) 
       .setLandmarkType(FaceDetector.ALL_LANDMARKS) 
       .build(); 

     // This is a temporary workaround for a bug in the face detector with respect to operating 
     // on very small images. This will be fixed in a future release. But in the near term, use 
     // of the SafeFaceDetector class will patch the issue. 
     Detector<Face> safeDetector = new SafeFaceDetector(detector); 

     // Create a frame from the bitmap and run face detection on the frame. 
     Frame frame = new Frame.Builder().setBitmap(bitmap).build(); 
     SparseArray<Face> faces = safeDetector.detect(frame); 

     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)); 
      } 
     } 

     FaceView overlay = (FaceView) findViewById(R.id.faceView); 
     overlay.setContent(bitmap, faces); 

     // Although detector may be used multiple times for different images, it should be released 
     // when it is no longer needed in order to free native resources. 
     safeDetector.release(); 
    } 
} 
+0

我有這個確切的問題。你解決了嗎? –

+0

對不起,我可以解決它。看起來這是一個相當罕見的問題,只發生在某些Android設備上。我嘗試了3個其他手機,並且實際上工作的代碼完全相同。出現此問題的手機是Android 5.1的LG Leon –

回答

0

我有這個問題,這些是我採取的解決它的步驟

  1. 確保設備或模擬器上有多達500MB的存儲空間。
  2. 將Google Play服務更新到最新版本。由於發現了一些在9.2版本中重新啓用的錯誤,臉部檢測已被禁用了一段時間。
  3. 確保您的互聯網連接正常工作。沒有辦法檢查所需的本地庫的下載進度,但應在幾分鐘後完成。

希望這可以節省幾個小時。 乾杯!