Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Vision API - Face methods on null object references

I am trying to change the sample app provided by Google for the face detection on 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));
            }
        }

My problem is, when I try to invoke methods on the detected faces, such as:

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

Then sometimes I get this exception when the app crashes:

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) 

I really don't see why the SafeDetector would return a SparseArray of faces with null faces. Has anyone encountered this problem?

like image 513
Loïs Talagrand Avatar asked Dec 25 '22 07:12

Loïs Talagrand


1 Answers

I actually just found the solution to my problem. The faces are stored in a SparseArray, which is actually similar to a map from integers (ids) to faces. As a consequence, getting a face should be done via:

Face face = faces.valueAt(i);

instead of

Face face = faces.get(i);
like image 111
Loïs Talagrand Avatar answered Dec 28 '22 05:12

Loïs Talagrand