android – Google Vision API – 关于空对象引用的Face方法

我正在尝试更改Google提供的示例应用,以便在Android上进行面部检测.

FaceDetector detector = new FaceDetector.Builder(getApplicationContext())
                .setTrackingEnabled(false)
                .setMode(FaceDetector.ACCURATE_MODE) // Accurate mode allows to get better face detection and better position (but the detection will be slower)
                .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.
        Bitmap bitmap = ((BitmapDrawable)ivPhoto.getDrawable()).getBitmap();
        Frame frame = new Frame.Builder().setBitmap(bitmap).build();
        SparseArray<Face> faces = safeDetector.detect(frame);

        if (!safeDetector.isOperational()) {
            Log.w(TAG, "Face detector dependencies are not yet available.");

            // Check for low storage.  If there is low storage, the 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));
            }
        }

我的问题是,当我尝试在检测到的面上调用方法时,例如:

for(int i = 0; i < faces.size(); i++) {
            Face face = faces.get(i);    
            float x = face.getPosition().x + (face.getWidth() / 2);
            float y = face.getPosition().y + (face.getHeight() / 2);
}

然后有时我在应用程序崩溃时遇到此异常:

04-01 09:07:23.154 30199-30199/ch.epfl.proshare E/AndroidRuntime: FATAL EXCEPTION: main
                                                                  Process: ch.epfl.proshare, PID: 30199
                                                                  java.lang.RuntimeException: Unable to start activity ComponentInfo{ch.epfl.proshare/ch.epfl.proshare.main.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.AppCompatActivity.setSupportActionBar(android.support.v7.widget.Toolbar)' on a null object reference
                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2366)
                                                                      at android.app.ActivityThread.access$800(ActivityThread.java:149)
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                      at android.os.Looper.loop(Looper.java:135)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5297)
                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                      at java.lang.reflect.Method.invoke(Method.java:372)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
                                                                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.AppCompatActivity.setSupportActionBar(android.support.v7.widget.Toolbar)' on a null object reference
                                                                      at ch.epfl.proshare.main.MainFragment.onCreateView(MainFragment.java:169)
                                                                      at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
                                                                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
                                                                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
                                                                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230)
                                                                      at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2042)
                                                                      at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:165)
                                                                      at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:543)
                                                                      at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1220)
                                                                      at android.app.Activity.performStart(Activity.java:6036)
                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2366) 
                                                                      at android.app.ActivityThread.access$800(ActivityThread.java:149) 
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284) 
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                      at android.os.Looper.loop(Looper.java:135) 
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5297) 
                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                      at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908) 
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703) 

我真的不明白为什么SafeDetector会返回带有空面的面的SparseArray.有谁遇到过这个问题?

解决方法:

我实际上刚刚找到了解决问题的方法.
面存储在SparseArray中,实际上类似于从整数(ids)到面的映射.因此,获得一张脸应该通过以下方式完成:

Face face = faces.valueAt(i);

代替

Face face = faces.get(i);
上一篇:【Demo 1】基于object_detection API的行人检测 3:模型训练并在OpenCV调用模型


下一篇:javascript – jQuery:如何检查两个元素是否具有相同的类