I'm working on a project where I have to change the hair color of a person's image and for this, I'm using Huawei Hair segmentation.
Here is the code of Analyzer which analyze the image.
val setting = MLImageSegmentationSetting.Factory()
.setExact(true)
.setAnalyzerType(MLImageSegmentationSetting.HAIR_SEG)
.create()
analyzer = MLAnalyzerFactory.getInstance().getImageSegmentationAnalyzer(setting)
// Create an MLFrame by using android.graphics.Bitmap. Recommended image size: large than 224*224.
bitmap = BitmapFactory.decodeResource(this.resources, R.drawable.imgseg_foreground);
val mlFrame = MLFrame.Creator().setBitmap(bitmap).create()
val task: Task<MLImageSegmentation> = analyzer!!.asyncAnalyseFrame(mlFrame)
task.addOnSuccessListener(OnSuccessListener<MLImageSegmentation?> { imageSegmentationResult -> // Processing logic for recognition success.
if (imageSegmentationResult != null) {
[email protected](imageSegmentationResult)
} else {
[email protected]("imageSegmentationResult is null.")
}
})
.addOnFailureListener(OnFailureListener { e -> // Processing logic for recognition failure.
[email protected](e.message!!)
})
And here is the code of displaySuccess()
private fun displaySuccess(imageSegmentationResult: MLImageSegmentation) {
if (bitmap == null) {
displayFailure("bitmap is null.")
return
}
// Here I get null value in getMask()
if (imageSegmentationResult.getMasks() == null) {
return
}
var processedBitmap: Bitmap? = null
val pixels: IntArray = byteArrToIntArr(imageSegmentationResult.getMasks())
processedBitmap = Bitmap.createBitmap(
pixels,
0,
bitmap!!.width,
bitmap!!.width,
bitmap!!.height,
Bitmap.Config.ARGB_8888
)
if (processedBitmap != null) {
binding.imgHolder.setImageBitmap(processedBitmap)
} else {
displayFailure("bitmapFore is null.")
}
}
and I get the null value here.
if (imageSegmentationResult.getMasks() == null) {
return
}
This is the ml kit sdk version I'm using
// Import the base SDK.
implementation 'com.huawei.hms:ml-computer-vision-segmentation:3.4.0.300'
// Import the multiclass segmentation model package.
implementation 'com.huawei.hms:ml-computer-vision-image-segmentation-multiclass-model:3.4.0.300'
// Import the human body segmentation model package.
implementation 'com.huawei.hms:ml-computer-vision-image-segmentation-body-model:3.4.0.300'
// Import the hair segmentation model package.
implementation 'com.huawei.hms:ml-computer-vision-image-segmentation-hair-model:3.4.0.300'
And this is the log info
2022-04-29 13:58:31.623 7423-7423/com.android.backgroundremover E/CountryCodeBean: getVendorCountry=UNKNOWN 2022-04-29 13:58:31.624 7423-7423/com.android.backgroundremover E/CountryCodeBean: getSimCountryCode by not enableNetwork, countryCode=pk 2022-04-29 13:58:31.701 7423-8129/com.android.backgroundremover I/HaLogProvider: in common ha, BUILD_MODE is: APK 2022-04-29 13:58:31.702 7423-8129/com.android.backgroundremover E/HaLogProvider: forbiddenHiLog.getVenderCountry=UNKNOWN 2022-04-29 13:58:31.702 7423-8129/com.android.backgroundremover E/HaLogProvider: forbiddenHiLog openHa = false 2022-04-29 13:58:31.702 7423-8129/com.android.backgroundremover I/ImgSeg_ImageSegImpl: accuracy:1,model:2,timeType:0 2022-04-29 13:58:31.704 7423-8129/com.android.backgroundremover I/HMSNativate: accuracy:1,modelKey:2,isVedio:0,scene:0 2022-04-29 13:58:31.704 7423-8129/com.android.backgroundremover I/HCLJNI: [HCL] 202112292 in runnet(). new.a. Elab: 1, MKey: 2, isVideo: 0, set: 0 2022-04-29 13:58:31.704 7423-8129/com.android.backgroundremover I/HCLJNI: [HCL] enter runnet(). 2022-04-29 13:58:31.707 7423-8129/com.android.backgroundremover I/HCLJNI: [HCL] InputHeight,InputWidth: 1031, 688 2022-04-29 13:58:31.715 7423-8129/com.android.backgroundremover I/HCLJNI: [HCL] after resize, h: 289, w: 289. 2022-04-29 13:58:31.721 7423-8129/com.android.backgroundremover E/HIAI_DDK_MSG: /general_model_executor.cpp CheckInputs(811)::"input size not match:2, 1" 2022-04-29 13:58:31.721 7423-8129/com.android.backgroundremover E/HIAI_DDK_MSG: /general_model_executor.cpp Execute(868)::"check geBaseBuffInput failed" 2022-04-29 13:58:31.721 7423-8129/com.android.backgroundremover E/AI_DDK_MSG: /ai_model_executor_manager.cpp Execute(698)::"aicp::IModelExecutor::Execute failed! ret:0xffffffff." 2022-04-29 13:58:31.721 7423-8129/com.android.backgroundremover E/AI_DDK_MSG: Process(145)::"Model synchrous process failed: seg_1c.om" 2022-04-29 13:58:31.722 7423-8129/com.android.backgroundremover E/HIAI_DDK_MSG: /HiAiModelManagerService.cpp Process(374)::"run seg_1c.om failed" 2022-04-29 13:58:31.722 7423-8129/com.android.backgroundremover I/HCLJNI: [HCL] Line 1611, run 1cModel failed, ret: 1. 2022-04-29 13:58:31.722 7423-8129/com.android.backgroundremover I/HMSNativate: ---> mindSporeRunnet time = 18 2022-04-29 13:58:31.723 7423-8129/com.android.backgroundremover E/HMSNativate: runnet fail 2022-04-29 13:58:31.723 7423-8129/com.android.backgroundremover E/ImageSegImpl: ProcessImage failed imageSliceDecteorParcel is null
I don't know why it's not working as I've extracted this code from the demo app and there it's working perfectly.