androidkotlinhuawei-mobile-serviceshuawei-ml-kit

Android Huawei image segmentation not working on release build


I'm using Huawei image segmentation for background removal from images. This code work perfectly fine on debug build but it does not work on a release build. I don't understand what could be the case.

Code:

  private fun imageSegmentation(bitmap: Bitmap?) {
            if (bitmap == null) {
                dialog.dismiss()
                Toast.makeText(requireContext(), "Something went wrong. Try again!", Toast.LENGTH_LONG).show()
                return
            }
    
            val setting =
                MLImageSegmentationSetting.Factory()
                    .setAnalyzerType(MLImageSegmentationSetting.BODY_SEG)
                    .setExact(true)
                    .create()
            val analyzer = MLAnalyzerFactory.getInstance().getImageSegmentationAnalyzer(setting)
            val mlFrame = MLFrame.Creator().setBitmap(bitmap).create()
            val task = analyzer?.asyncAnalyseFrame(mlFrame)
            task?.addOnSuccessListener { mlImageSegmentationResults ->
                if (mlImageSegmentationResults != null) {
                    removalFlag = true
                    removalBitmap = mlImageSegmentationResults.foreground
                } else
                    Toast.makeText(context, "No human body is detected!", Toast.LENGTH_LONG).show()
                dialog.dismiss()
            }?.addOnFailureListener {
                Toast.makeText(context, "No human body is detected!", Toast.LENGTH_LONG).show()
                dialog.dismiss()
            }
        }

Dependencies:

 implementation 'com.huawei.hms:ml-computer-vision-segmentation:2.2.0.300'
 implementation 'com.huawei.hms:ml-computer-vision-image-segmentation-body-model:2.2.0.300'

Note: According to my understanding the task?.addOnSuccessListener is called but mlImageSegmentationResults return's null.


Solution

  • Stuff like this usually happens when you have ProGuard enabled but not correctly configured. Make sure to add appropriate rules to proguard-rules.pro file to prevent it from obfuscating relevant classes.

    Information about this is usually provided by the library developers. After a quick search I came up with this example. Sources seem to be documented well enough, so that it should not be a problem to find the correct settings.

    Keep in mind that you probably need to add rules for more than one library.