I am trying to run hair segmentation on Android but it's not working. It is returning null results as outputs while Human segmentation works perfectly fine.
This is the code.
public class MainActivity extends AppCompatActivity {
MLImageSegmentationAnalyzer analyzer;
Bitmap originialBitmap;
Bitmap foregroundBitmap;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.person);
bitmap = Bitmap.createScaledBitmap(bitmap, 1000, 1500, true);
createImageTransactor(bitmap);
}
private void createImageTransactor(Bitmap bitmap) {
MLImageSegmentationSetting setting = new MLImageSegmentationSetting.Factory()
.setExact(true)
.setAnalyzerType(MLImageSegmentationSetting.HAIR_SEG)
.create();
this.analyzer = MLAnalyzerFactory.getInstance().getImageSegmentationAnalyzer(setting);
MLFrame mlFrame = new MLFrame.Creator().setBitmap(bitmap).create();
Task<MLImageSegmentation> task = this.analyzer.asyncAnalyseFrame(mlFrame);
task.addOnSuccessListener(new OnSuccessListener<MLImageSegmentation>() {
@Override
public void onSuccess(MLImageSegmentation mlImageSegmentationResults) {
// Transacting logic for segment success.
if (mlImageSegmentationResults != null) {
Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_SHORT).show();
foregroundBitmap = mlImageSegmentationResults.getGrayscale();
imageView.setImageBitmap(foregroundBitmap);
} else {
Toast.makeText(MainActivity.this, "Failure", Toast.LENGTH_SHORT).show();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Toast.makeText(MainActivity.this, "Failure", Toast.LENGTH_SHORT).show();
// Transacting logic for segment failure.
return;
}
});
}
}
And this is the gradle file.
implementation 'com.huawei.hms:ml-computer-vision-cloud:2.0.5.300'
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'
implementation 'com.huawei.hms:ml-computer-vision-image-segmentation-multiclass-model:2.2.0.300'
implementation 'com.huawei.hms:ml-computer-vision-image-segmentation-hair-model:2.2.0.300'
But when the code is run it outputs null image and the masks in segmentation results contain whole 0's in the array.
Please let me know what is the issue.
You may refer to the
HairSegmentationStillAnalyseActivity
class in the sample code: