Search code examples
imagefirebase-mlkitgoogle-mlkit

Replacement for FirebaseVisonImageMetadata for standalone ML Kit SDKs


I have been migrating to the new standalone ML Kit from the Firebase ML Kit.

While creating the FirebaseVisionImage I used to pass the rotation as FirebaseVisionImageMetadata.ROTATION_180, but according to documentation, the FirebaseVisionImageMetadata class has been removed.

How to set the rotation value while using new SDK classes?

Old code:

 mFaceDetector.detectInImage(FirebaseVisionImage.fromMediaImage(
       finalImage, 
       FirebaseVisionImageMetadata.ROTATION_180))
            .addOnSuccessListener(faces -> {
                 // Some logic
             });

Solution

  • This was solved by just passing the rotation values in degrees as int.

    Since the new SDK requires InputImage instead of FirebaseVisionImage, its fromMediaImage method implementation gives the idea. Here is the function:

    @NonNull
    @RequiresApi(19)
    public static InputImage fromMediaImage(@NonNull Image var0, int var1) {
        long var2 = SystemClock.elapsedRealtime();
        Preconditions.checkNotNull(var0, "Please provide a valid image");
        Preconditions.checkArgument(var1 == 0 || var1 == 90 || var1 == 180 || var1 == 270, "Invalid rotation. Only 0, 90, 180, 270 are supported currently.");
    
        // ...other logic
    }
    

    This gives the idea that the new function wants the second parameter as 0, 90, 180 or 270 as int values.

    Therefore, we can pass rotation as:

    mFaceDetector.process(InputImage.fromMediaImage(finalImage, 180)).addOnSuccessListener(faces -> {
         // Some logic
    });