Search code examples
androidfirebasefirebase-mlkitgoogle-mlkit

Firebase ML Kit Face Detection, failed to retrieve instance ID


I've been trying to set up a PoC where I'm detecting faces over a live camera feed. So far, the camera feed is working fine, but the facial recognition isn't triggering due to the following error: W/FA: Failed to Retrieve Firebase Instance ID. I have checked out related questions but these seem to all revolve around the messaging service, which I do not use.

Here are my dependencies:

implementation fileTree(dir: 'libs', include: ['*.jar'])
annotationProcessor 'com.google.auto.value:auto-value:1.4'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.navigation:navigation-fragment-ktx:2.2.2'
implementation 'androidx.navigation:navigation-ui-ktx:2.2.2'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.github.d-max:spots-dialog:1.1@aar'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:multidex:1.0.3'
implementation 'com.camerakit:camerakit:1.0.0-beta3.11'
implementation 'com.camerakit:jpegkit:0.1.0'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.72'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0'
implementation 'com.google.firebase:firebase-analytics:17.4.2'
implementation 'com.google.firebase:firebase-ml-vision:24.0.3'
implementation 'com.google.firebase:firebase-ml-vision-face-model:20.0.1'

Here is my Activity Code

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

    FirebaseOptions options = new FirebaseOptions.Builder()
            .setDatabaseUrl("https://mobile-fitness-787c0.firebaseio.com")
            .build();

    FirebaseApp.initializeApp(this, options);

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) !=
            PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(
                this,
                new String[] {Manifest.permission.CAMERA},
                50
        );
    }

    faceDetectButton = (Button)findViewById(R.id.capture_button);
    graphicOverlay = (GraphicOverlay)findViewById(R.id.graphic_overlay);
    cameraKitView = (CameraKitView)findViewById(R.id.camera_frame);

    alertDialog = new SpotsDialog.Builder()
            .setContext(this)
            .setMessage("Please wait, Loading...")
            .setCancelable(false).build();
    cameraKitView.setFacing(CameraKit.FACING_FRONT);
    cameraKitView.startVideo();


    faceDetectButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cameraKitView.captureImage(new CameraKitView.ImageCallback() {
                @Override
                public void onImage(CameraKitView cameraKitView, byte[] bytes) {
                    alertDialog.show();
                    Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                    Bitmap mutableBitmap = bmp.copy(Bitmap.Config.ARGB_8888, true);
                    mutableBitmap = Bitmap.createScaledBitmap(mutableBitmap,
                            cameraKitView.getWidth(),
                            cameraKitView.getHeight(),
                            true
                    );

                    processFaceDetection(mutableBitmap);
                }
            });
            cameraKitView.invalidate();
        }
    });
}
private void processFaceDetection(Bitmap bitmap) {
    FirebaseVisionImage fbvImage = FirebaseVisionImage.fromBitmap(bitmap);

    FirebaseVisionFaceDetectorOptions fbvOptions = new FirebaseVisionFaceDetectorOptions
            .Builder().setClassificationMode(FirebaseVisionFaceDetectorOptions.ALL_CLASSIFICATIONS)
            .enableTracking().build();

    FirebaseVisionFaceDetector fbvDetector = FirebaseVision.getInstance()
            .getVisionFaceDetector(fbvOptions);

    fbvDetector.detectInImage(fbvImage).addOnSuccessListener(new OnSuccessListener<List<FirebaseVisionFace>>() {
        @Override
        public void onSuccess(List<FirebaseVisionFace> firebaseVisionFaces) {
            getFaceResults(firebaseVisionFaces);
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Toast.makeText(FaceActivity.this, "Error: " + e, Toast.LENGTH_LONG).show();
        }
    });
}

private void getFaceResults(List<FirebaseVisionFace> fvbFaces) {
    int counter = 0;
    for (FirebaseVisionFace face : fvbFaces) {
        Rect rect = face.getBoundingBox();
        RectOverlay rectOverlay = new RectOverlay(graphicOverlay, rect);
        counter++;
        graphicOverlay.add(rectOverlay);
        alertDialog.dismiss();
    }
}

My manifest:

 <uses-permission android:name="android.permission.CAMERA"/>
<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera.front" android:required="false" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <uses-library android:name="org.apache.http.legacy" android:required="false"/>
    <activity android:name=".FaceActivity">
        <meta-data
            android:name="com.google.firebase.ml.vision.DEPENDENCIES"
            android:value="face" />
    </activity>
    <activity
        android:name=".GameActivity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/title_activity_game"
        android:theme="@style/FullscreenTheme" />
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

I have tried most of the suggestions in other questions, to no avail. The activity loads, the front camera opens and I see a live video feed, but there is no facial detection and I get the error mentioned above. Pressing the button that has a clickListener does trigger, but the cameraKitView does not capture an image or analyze it.

What should I do?


Solution

  • If your app only uses the ML Kit on-device functionality, you can now use a new standalone ML Kit API which can be added with just a gradle dependency and does not require a Firebase instance Id.

    You can choose also between bundling the model with your app:

    implementation 'com.google.mlkit:face-detection:16.0.0'
    

    or let Google Play Services deliver it separately and reduce your app size

    implementation 'com.google.android.gms:play-services-mlkit-face-detection:16.0.0'
    

    If you choose to use Google Play Services, remember to add meta-data tag in your AndroidManifest.xml

    <application ...>
        ...
      <meta-data
          android:name="com.google.mlkit.vision.DEPENDENCIES"
          android:value="face" />
      <!-- If you use multiple ML Kit models via Google Play Services:
           android:value="face,model2,model3" -->
    </application>