RemoveBackground Huawei - mayurparmar2/AlarmDemo GitHub Wiki
test.gradle
buildscript {
repositories {
google()
jcenter()
maven { url 'https://developer.huawei.com/repo/' }
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.3'
classpath 'com.huawei.agconnect:agcp:1.9.0.300'
}
}
allprojects {
repositories {
google()
jcenter()
maven { url "https://jitpack.io" }
mavenCentral()
maven { url 'https://maven.google.com' }
maven { url 'https://developer.huawei.com/repo/' }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
test.java
//implementation 'com.huawei.hms:ml-computer-vision-image-segmentation-body-model:3.7.0.302'
//implementation 'com.huawei.hms:ml-computer-vision-segmentation:3.7.0.302'
private MLImageSegmentationAnalyzer analyzer;
private void removeImageBackground(Bitmap image) {
MLImageSegmentationSetting setting = new MLImageSegmentationSetting.Factory()
.setAnalyzerType(MLImageSegmentationSetting.HAIR_SEG)
.setExact(true)
.create();
this.analyzer = MLAnalyzerFactory.getInstance().getImageSegmentationAnalyzer(setting);
if (this.isChosen(image)) {
MLFrame mlFrame = new MLFrame.Creator().setBitmap(image).create();
Task<MLImageSegmentation> task = this.analyzer.asyncAnalyseFrame(mlFrame);
task.addOnSuccessListener(new OnSuccessListener<MLImageSegmentation>() {
@Override
public void onSuccess(MLImageSegmentation mlImageSegmentationResults) {
if (mlImageSegmentationResults != null && mlImageSegmentationResults.getForeground()!= null) {
Bitmap foregroundBitmap = mlImageSegmentationResults.getForeground();
if (!isBitmapTransparent(foregroundBitmap)) {
EraserActivity.this.removedBackgroundBitmap = mlImageSegmentationResults.getForeground();
addImageToEraserTool(foregroundBitmap);
} else {
EraserActivity.this.addImageToEraserTool(image);
displayFailure();
}
} else {
EraserActivity.this.addImageToEraserTool(image);
displayFailure();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
displayFailure();
}
});
} else {
Toast.makeText(this.getApplicationContext(), R.string.txt_not_detect_human, Toast.LENGTH_SHORT).show();
}
}
private void displayFailure() {
Toast.makeText(this.getApplicationContext(), "Fail", Toast.LENGTH_SHORT).show();
}
private boolean isChosen(Bitmap bitmap) {
if (bitmap == null) {
return false;
} else {
return true;
}
}
private boolean isBitmapTransparent(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if (bitmap.getPixel(x, y) != Color.TRANSPARENT) {
return false;
}
}
}
return true; // If all pixels are transparent, it's considered transparent
}