人物更换背景
APACHE-2.0 License
PaddlePaddlePaddleSeghttp://github.com/PaddlPaddle/PaddleSeg AndroidAndroidPaddlePaddlePaddle LiteAndroid
AndroidPaddlePaddlePaddleLiteSegmentation
java
inputShape``NUM_THREADS
4
private PaddlePredictor paddlePredictor;
private Tensor inputTensor;
public static long[] inputShape = new long[]{1, 3, 513, 513};
private static final int NUM_THREADS = 4;
/**
* @param modelPath model path
*/
public PaddleLiteSegmentation(String modelPath) throws Exception {
File file = new File(modelPath);
if (!file.exists()) {
throw new Exception("model file is not exists!");
}
try {
MobileConfig config = new MobileConfig();
config.setModelFromFile(modelPath);
config.setThreads(NUM_THREADS);
config.setPowerMode(PowerMode.LITE_POWER_HIGH);
paddlePredictor = PaddlePredictor.createPaddlePredictor(config);
inputTensor = paddlePredictor.getInput(0);
inputTensor.resize(inputShape);
} catch (Exception e) {
e.printStackTrace();
throw new Exception("load model fail!");
}
}
Bitmap
public long[] predictImage(String image_path) throws Exception {
if (!new File(image_path).exists()) {
throw new Exception("image file is not exists!");
}
FileInputStream fis = new FileInputStream(image_path);
Bitmap bitmap = BitmapFactory.decodeStream(fis);
long[] result = predictImage(bitmap);
if (bitmap.isRecycled()) {
bitmap.recycle();
}
return result;
}
public long[] predictImage(Bitmap bitmap) throws Exception {
return predict(bitmap);
}
BitmapBitmap
private float[] getScaledMatrix(Bitmap bitmap) {
int channels = (int) inputShape[1];
int width = (int) inputShape[2];
int height = (int) inputShape[3];
float[] inputData = new float[channels * width * height];
Bitmap rgbaImage = bitmap.copy(Bitmap.Config.ARGB_8888, true);
Bitmap scaleImage = Bitmap.createScaledBitmap(rgbaImage, width, height, true);
Log.d(TAG, scaleImage.getWidth() + ", " + scaleImage.getHeight());
if (channels == 3) {
// RGB = {0, 1, 2}, BGR = {2, 1, 0}
int[] channelIdx = new int[]{0, 1, 2};
int[] channelStride = new int[]{width * height, width * height * 2};
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int color = scaleImage.getPixel(x, y);
float[] rgb = new float[]{(float) red(color), (float) green(color), (float) blue(color)};
inputData[y * width + x] = rgb[channelIdx[0]];
inputData[y * width + x + channelStride[0]] = rgb[channelIdx[1]];
inputData[y * width + x + channelStride[1]] = rgb[channelIdx[2]];
}
}
} else if (channels == 1) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int color = scaleImage.getPixel(x, y);
float gray = (float) (red(color) + green(color) + blue(color));
inputData[y * width + x] = gray;
}
}
} else {
Log.e(TAG, "13");
}
return inputData;
}
01
private long[] predict(Bitmap bmp) throws Exception {
float[] inputData = getScaledMatrix(bmp);
inputTensor.setData(inputData);
try {
paddlePredictor.run();
} catch (Exception e) {
throw new Exception("predict image fail! log:" + e);
}
Tensor outputTensor = paddlePredictor.getOutput(0);
long[] output = outputTensor.getLongData();
long[] outputShape = outputTensor.shape();
Log.d(TAG, "shape"+ Arrays.toString(outputShape));
return output;
}
MainActivity
assets
String segmentationModelPath = getCacheDir().getAbsolutePath() + File.separator + "model.nb";
Utils.copyFileFromAsset(MainActivity.this, "model.nb", segmentationModelPath);
try {
paddleLiteSegmentation = new PaddleLiteSegmentation(segmentationModelPath);
Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show();
Log.d(TAG, "");
} catch (Exception e) {
Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show();
Log.d(TAG, "");
e.printStackTrace();
finish();
}
//
Button selectPicture = findViewById(R.id.select_picture);
Button selectBackground = findViewById(R.id.select_background);
Button savePicture = findViewById(R.id.save_picture);
imageView = findViewById(R.id.imageView);
selectPicture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 0);
}
});
selectBackground.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (resultPicture != null){
//
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 1);
}else {
Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show();
}
}
});
savePicture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//
String savePth = Utils.saveBitmap(mergeBitmap1);
if (savePth != null) {
Toast.makeText(MainActivity.this, "" + savePth, Toast.LENGTH_SHORT).show();
Log.d(TAG, "" + savePth);
} else {
Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show();
Log.d(TAG, "");
}
}
});
Uri image_uri = data.getData();
image_path = Utils.getPathFromURI(MainActivity.this, image_uri);
try {
//
FileInputStream fis = new FileInputStream(image_path);
Bitmap b = BitmapFactory.decodeStream(fis);
long start = System.currentTimeMillis();
long[] result = paddleLiteSegmentation.predictImage(image_path);
long end = System.currentTimeMillis();
//
humanPicture = b.copy(Bitmap.Config.ARGB_8888, true);
final int[] colors_map = {0x00000000, 0xFF000000};
int[] objectColor = new int[result.length];
for (int i = 0; i < result.length; i++) {
objectColor[i] = colors_map[(int) result[i]];
}
Bitmap.Config config = humanPicture.getConfig();
Bitmap outputImage = Bitmap.createBitmap(objectColor, (int) PaddleLiteSegmentation.inputShape[2], (int) PaddleLiteSegmentation.inputShape[3], config);
resultPicture = Bitmap.createScaledBitmap(outputImage, humanPicture.getWidth(), humanPicture.getHeight(), true);
imageView.setImageBitmap(b);
Log.d(TAG, "" + (end - start) + "ms");
} catch (Exception e) {
e.printStackTrace();
}
Uri image_uri = data.getData();
image_path = Utils.getPathFromURI(MainActivity.this, image_uri);
try {
FileInputStream fis = new FileInputStream(image_path);
changeBackgroundPicture = BitmapFactory.decodeStream(fis);
mergeBitmap1 = draw();
imageView.setImageBitmap(mergeBitmap1);
} catch (Exception e) {
e.printStackTrace();
}
//
public Bitmap draw() {
//
Bitmap bgBitmap = Bitmap.createScaledBitmap(changeBackgroundPicture, resultPicture.getWidth(), resultPicture.getHeight(), true);
for (int y = 0; y < resultPicture.getHeight(); y++) {
for (int x = 0; x < resultPicture.getWidth(); x++) {
int color = resultPicture.getPixel(x, y);
int a = Color.alpha(color);
if (a == 255) {
bgBitmap.setPixel(x, y, Color.TRANSPARENT);
}
}
}
//
Bitmap bgBitmap2 = Bitmap.createBitmap(bgBitmap.getWidth(), bgBitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas1 = new Canvas(bgBitmap2);
canvas1.drawBitmap(bgBitmap, 0, 0, null);
return mergeBitmap(humanPicture, bgBitmap2);
}
//
public static Bitmap mergeBitmap(Bitmap backBitmap, Bitmap frontBitmap) {
Bitmap bitmap = backBitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(bitmap);
Rect baseRect = new Rect(0, 0, backBitmap.getWidth(), backBitmap.getHeight());
Rect frontRect = new Rect(0, 0, frontBitmap.getWidth(), frontBitmap.getHeight());
canvas.drawBitmap(frontBitmap, frontRect, baseRect, null);
return bitmap;
}