Skip to content

Commit

Permalink
Merge pull request #4 from gwwhuawei/master
Browse files Browse the repository at this point in the history
#3 Change show locig of the image segmentation.
  • Loading branch information
HMS-MLKit authored May 26, 2020
2 parents 987fdb4 + 1d46d75 commit 24247d4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

import androidx.core.content.FileProvider;

import com.huawei.hms.mlsdk.imgseg.MLImageSegmentationScene;
import com.mlkit.sample.R;
import com.mlkit.sample.callback.ImageSegmentationResultCallBack;
import com.mlkit.sample.callback.ImageUtilCallBack;
Expand Down Expand Up @@ -216,7 +217,11 @@ private void createLensEngine() {
this.lensEngine = new LensEngine(this, this.cameraConfiguration, this.graphicOverlay);
}
try {
this.setting = new MLImageSegmentationSetting.Factory().setAnalyzerType(MLImageSegmentationSetting.BODY_SEG).setExact(false).create();
this.setting = new MLImageSegmentationSetting.Factory()
.setAnalyzerType(MLImageSegmentationSetting.BODY_SEG)
.setExact(false)
.setScene(MLImageSegmentationScene.FOREGROUND_ONLY)
.create();
this.transactor = new ImageSegmentationTransactor(this.getApplicationContext(), this.setting, this.background);
this.transactor.setImageSegmentationResultCallBack(this);
this.lensEngine.setMachineLearningFrameTransactor(this.transactor);
Expand Down Expand Up @@ -274,7 +279,9 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
this.facing = CameraConfiguration.CAMERA_FACING_BACK;
}
this.cameraConfiguration.setCameraFacing(this.facing);
this.setting = new MLImageSegmentationSetting.Factory().setAnalyzerType(MLImageSegmentationSetting.BODY_SEG).create();
this.setting = new MLImageSegmentationSetting.Factory()
.setAnalyzerType(MLImageSegmentationSetting.BODY_SEG)
.create();
this.transactor = new ImageSegmentationTransactor(this.getApplicationContext(), this.setting, this.background);
this.transactor.setImageSegmentationResultCallBack(this);
this.lensEngine.setMachineLearningFrameTransactor(this.transactor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.util.SparseArray;
import android.widget.Toast;
Expand All @@ -31,6 +30,7 @@
import com.mlkit.sample.callback.ImageSegmentationResultCallBack;
import com.mlkit.sample.camera.CameraConfiguration;
import com.mlkit.sample.camera.FrameMetadata;
import com.mlkit.sample.util.BitmapUtils;
import com.mlkit.sample.views.graphic.CameraImageGraphic;
import com.mlkit.sample.views.overlay.GraphicOverlay;
import com.huawei.hms.mlsdk.MLAnalyzerFactory;
Expand All @@ -55,7 +55,6 @@ public class ImageSegmentationTransactor extends BaseTransactor<MLImageSegmentat
private Context context;
private Bitmap foregroundBitmap;
private Bitmap backgroundBitmap;
//private String savePath = "";
private ImageSegmentationResultCallBack imageSegmentationResultCallBack;

/**
Expand All @@ -70,15 +69,6 @@ public ImageSegmentationTransactor(Context context, MLImageSegmentationSetting o
MLAnalyzerFactory.getInstance().getImageSegmentationAnalyzer();
this.detector = MLAnalyzerFactory.getInstance().getImageSegmentationAnalyzer(options);
this.backgroundBitmap = backgroundBitmap;
this.clearPath();
}

public void setSavePath(String path) {
//this.savePath = path;
}

private void clearPath() {
//this.savePath = "";
}

// Return to processed image.
Expand All @@ -96,8 +86,6 @@ public void stop() {
}
}

//private Long startTime = 0L;

@Override
protected Task<MLImageSegmentation> detectInImage(MLFrame frame) {
return this.detector.asyncAnalyseFrame(frame);
Expand All @@ -114,20 +102,15 @@ protected void onSuccess(
@NonNull FrameMetadata frameMetadata,
@NonNull GraphicOverlay graphicOverlay) {
graphicOverlay.clear();
if (results.getMasks() == null) {
Log.i(TAG, "detection failed");
return;
}
byte[] masks = results.getMasks();
if (masks == null) {
if (results.getForeground() == null) {
Log.i(TAG, "detection failed.");
return;
}
// Replace background.
this.foregroundBitmap = results.getOriginal();
int facing = frameMetadata.getCameraFacing();
foregroundBitmap = results.foreground;

Bitmap resultBitmap = this.changeNextBackground(masks);
if (facing == CameraConfiguration.CAMERA_FACING_FRONT) {
// Replace background.
Bitmap resultBitmap = this.changeNextBackground(foregroundBitmap);
if (frameMetadata.getCameraFacing() == CameraConfiguration.CAMERA_FACING_FRONT) {
resultBitmap = this.convert(resultBitmap);
}
if (this.imageSegmentationResultCallBack != null) {
Expand All @@ -146,7 +129,7 @@ protected void onFailure(@NonNull Exception e) {
/**
* Replace the images in the assets directory as the background image in order.
*/
private Bitmap changeNextBackground(byte[] masks) {
private Bitmap changeNextBackground(Bitmap foregroundBitmap) {
Bitmap result;
if (this.backgroundBitmap == null) {
Toast.makeText(this.context, "No Background Image", Toast.LENGTH_SHORT).show();
Expand All @@ -159,38 +142,10 @@ private Bitmap changeNextBackground(byte[] masks) {
int[] pixels = new int[this.backgroundBitmap.getWidth() * this.backgroundBitmap.getHeight()];
this.backgroundBitmap.getPixels(pixels, 0, this.backgroundBitmap.getWidth(), 0, 0,
this.backgroundBitmap.getWidth(), this.backgroundBitmap.getHeight());
// todo:masks
result = this.doMaskOnBackgroundImage(masks);
result = BitmapUtils.joinBitmap(backgroundBitmap, foregroundBitmap);
return result;
}

/**
* Process the picture according to the mask value of the returned classification.
*/
private Bitmap doMaskOnBackgroundImage(byte[] maskByte) {
if (this.backgroundBitmap == null) {
Toast.makeText(this.context, "No Background Image", Toast.LENGTH_SHORT).show();
return null;
}

//todo;
int[] masks = this.byteArrToIntArr(maskByte);
int[] foregroundPixels = new int[this.foregroundBitmap.getWidth() * this.foregroundBitmap.getHeight()];
int[] backgroundPixels = new int[this.backgroundBitmap.getWidth() * this.backgroundBitmap.getHeight()];
this.foregroundBitmap.getPixels(foregroundPixels, 0, this.foregroundBitmap.getWidth(), 0, 0, this.foregroundBitmap.getWidth(), this.foregroundBitmap.getHeight());
this.backgroundBitmap.getPixels(backgroundPixels, 0, this.backgroundBitmap.getWidth(), 0, 0, this.backgroundBitmap.getWidth(), this.backgroundBitmap.getHeight());

for (int i = 0; i < masks.length; i++) {
if (masks[i] == 0) {
foregroundPixels[i] = backgroundPixels[i];
}
}
Bitmap bitmap = Bitmap.createBitmap(foregroundPixels, 0, this.foregroundBitmap.getWidth(), this.foregroundBitmap.getWidth(), this.foregroundBitmap.getHeight(), Bitmap.Config.ARGB_8888);
BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
bitmapDrawable.setAntiAlias(true);
return bitmap;
}

/**
* Stretch background image size to foreground image's.
*
Expand All @@ -212,14 +167,6 @@ private boolean equalToForegroundImageSize() {
return this.backgroundBitmap.getHeight() == this.foregroundBitmap.getHeight() && this.backgroundBitmap.getWidth() == this.foregroundBitmap.getWidth();
}

private int[] byteArrToIntArr(byte[] masks) {
int[] results = new int[masks.length];
for (int i = 0; i < masks.length; i++) {
results[i] = masks[i];
}
return results;
}

/**
* Front camera image position changed.
*/
Expand All @@ -228,5 +175,4 @@ private Bitmap convert(Bitmap bitmap) {
m.setScale(-1, 1);// horizontal flip.
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ImageFormat;
import android.graphics.Matrix;
import android.graphics.Rect;
Expand Down Expand Up @@ -214,5 +215,30 @@ public static Bitmap scaleBitmap(Bitmap origin, int newWidth, int newHeight) {
Bitmap newBitmap = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
return newBitmap;
}

/**
* Fusion of two images.
* @param background background image.
* @param foreground foreground image.
* @return
*/
public static Bitmap joinBitmap(Bitmap background, Bitmap foreground) {
if (background == null || foreground == null) {
Log.e(TAG, "bitmap is null.");
return null;
}

if (background.getHeight() != foreground.getHeight() || background.getWidth() != foreground.getWidth()) {
Log.e(TAG, "bitmap size is not match.");
return null;
}
Bitmap newmap = Bitmap.createBitmap(background.getWidth(), background.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newmap);
canvas.drawBitmap(background, 0, 0, null);
canvas.drawBitmap(foreground, 0, 0, null);
canvas.save();
canvas.restore();
return newmap;
}
}

0 comments on commit 24247d4

Please sign in to comment.