Commit 0d97cc8c authored by Sugon_ldc's avatar Sugon_ldc
Browse files

add new model

parents
Pipeline #316 failed with stages
in 0 seconds
package com.paddle.demo.matting.config;
import android.graphics.Bitmap;
public class Config {
public String modelPath = "";
public String labelPath = "";
public String imagePath = "";
public String bgPath = "";
public int cpuThreadNum = 1;
public String cpuPowerMode = "";
public String inputColorFormat = "";
public long[] inputShape = new long[]{};
public void init(String modelPath, String labelPath, String imagePath,String bgPath, int cpuThreadNum,
String cpuPowerMode, String inputColorFormat,long[] inputShape){
this.modelPath = modelPath;
this.labelPath = labelPath;
this.imagePath = imagePath;
this.bgPath = bgPath;
this.cpuThreadNum = cpuThreadNum;
this.cpuPowerMode = cpuPowerMode;
this.inputColorFormat = inputColorFormat;
this.inputShape = inputShape;
}
public void setInputShape(Bitmap inputImage){
this.inputShape[0] = 1;
this.inputShape[1] = 3;
this.inputShape[2] = inputImage.getHeight();
this.inputShape[3] = inputImage.getWidth();
}
}
package com.paddle.demo.matting.preprocess;
import android.graphics.Bitmap;
import android.util.Log;
import com.paddle.demo.matting.config.Config;
import static android.graphics.Color.blue;
import static android.graphics.Color.green;
import static android.graphics.Color.red;
public class Preprocess {
private static final String TAG = Preprocess.class.getSimpleName();
Config config;
int channels;
int width;
int height;
public float[] inputData;
public void init(Config config){
this.config = config;
this.channels = (int) config.inputShape[1];
this.height = (int) config.inputShape[2];
this.width = (int) config.inputShape[3];
this.inputData = new float[channels * width * height];
}
public void normalize(float[] inputData){
for (int i = 0; i < inputData.length; i++) {
inputData[i] = (float) ((inputData[i] / 255 - 0.5) / 0.5);
}
}
public boolean to_array(Bitmap inputImage){
if (channels == 3) {
int[] channelIdx = null;
if (config.inputColorFormat.equalsIgnoreCase("RGB")) {
channelIdx = new int[]{0, 1, 2};
} else if (config.inputColorFormat.equalsIgnoreCase("BGR")) {
channelIdx = new int[]{2, 1, 0};
} else {
Log.i(TAG, "unknown color format " + config.inputColorFormat + ", only RGB and BGR color format is " +
"supported!");
return false;
}
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 = inputImage.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 = inputImage.getPixel(x, y);
float gray = (float) (red(color) + green(color) + blue(color));
inputData[y * width + x] = gray;
}
}
} else {
Log.i(TAG, "unsupported channel size " + Integer.toString(channels) + ", only channel 1 and 3 is " +
"supported!");
return false;
}
return true;
}
}
package com.paddle.demo.matting.visual;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.Log;
import com.baidu.paddle.lite.Tensor;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public class Visualize {
private static final String TAG = Visualize.class.getSimpleName();
public Bitmap segdraw(Bitmap inputImage, Tensor outputTensor,Bitmap bg){
//设置叠加颜色(ARGB格式) 背景采用黑色,前景采用黄色(黄色由红绿两种颜色叠加)
final int[] colors_map = {0xFF000000, 0xFFFFFF00};
long[] output = outputTensor.getLongData();
long outputShape[] = outputTensor.shape();
long outputSize = 1;
for (long s : outputShape) {
outputSize *= s;
}
int[] objectColor = new int[(int)outputSize];
for(int i=0;i<output.length;i++){
objectColor[i] = colors_map[(int)output[i]];
}
Bitmap.Config config = inputImage.getConfig();
Bitmap outputImage = null;
if(outputShape.length==3){
outputImage = Bitmap.createBitmap(objectColor, (int)outputShape[2], (int)outputShape[1], config);
outputImage = Bitmap.createScaledBitmap(outputImage, inputImage.getWidth(), inputImage.getHeight(),true);
}
else if (outputShape.length==4){
outputImage = Bitmap.createBitmap(objectColor, (int)outputShape[3], (int)outputShape[2], config);
}
Bitmap bmOverlay = Bitmap.createBitmap(inputImage.getWidth(), inputImage.getHeight() , inputImage.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(inputImage, new Matrix(), null);
Paint paint = new Paint();
paint.setAlpha(0x80);//采用一半对应的透明度进行叠加
canvas.drawBitmap(outputImage, 0, 0, paint);
return bmOverlay;
}
public Bitmap draw(Bitmap inputImage, Tensor outputTensor,Bitmap bg){
float[] output = outputTensor.getFloatData();
long outputShape[] = outputTensor.shape();
int outputSize = 1;
for (long s : outputShape) {
outputSize *= s;
}
List<Float> arralist = new LinkedList<>();
for (int i=0; i<outputSize;i++){
arralist.add((float)output[i]);
}
Bitmap mALPHA_IMAGE = floatArrayToBitmap(arralist,(int)outputShape[3],(int)outputShape[2]);
//调整尺寸
Bitmap alpha = Bitmap.createScaledBitmap(mALPHA_IMAGE,inputImage.getWidth(),inputImage.getHeight(),true);
Bitmap bgImg = Bitmap.createScaledBitmap(bg,inputImage.getWidth(),inputImage.getHeight(),true);
//重新合成图像
Bitmap result = synthetizeBitmap(inputImage,bgImg, alpha);
return result;
}
//将float数组转成bitmap格式的图片
private Bitmap floatArrayToBitmap(List<Float> floatArray,int width,int height){
byte alpha = (byte) 255 ;
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) ;
ByteBuffer byteBuffer = ByteBuffer.allocate(width*height*4*3) ;
float Maximum = Collections.max(floatArray);
float minmum = Collections.min(floatArray);
float delta = Maximum - minmum + 0.00000000001f ;
int i = 0 ;
for (float value : floatArray){
byte temValue = (byte) ((((value-minmum)/delta)*255));
byteBuffer.put(4*i, temValue) ;
byteBuffer.put(4*i+1, temValue) ;
byteBuffer.put(4*i+2, temValue) ;
byteBuffer.put(4*i+3, alpha) ;
i++;
}
bmp.copyPixelsFromBuffer(byteBuffer) ;
return bmp ;
}
//将原图与背景按照推理得到的alpha图进行合成
private Bitmap synthetizeBitmap(Bitmap front,Bitmap background, Bitmap alpha){
int width = front.getWidth();
int height = front.getHeight();
Bitmap result=Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
int[] frontPixels = new int[width * height];
int[] backgroundPixels = new int[width * height];
int[] alphaPixels = new int[width * height];
front.getPixels(frontPixels,0,width,0,0,width,height);
background.getPixels(backgroundPixels,0,width,0,0,width,height);
alpha.getPixels(alphaPixels,0,width,0,0,width,height);
float frontA = 0,frontR = 0,frontG = 0,frontB = 0;
float backgroundR = 0,backgroundG = 0,backgroundB = 0;
float alphaR = 0,alphaG = 0,alphaB = 0;
int index=0;
//逐个像素赋值(这种写法比较耗时,后续可以优化)
for (int row=0; row < height; row++){
for (int col=0; col < width; col++){
index = width*row +col;
//取出前景图像像素值
frontA=(frontPixels[index]>>24)&0xff;
frontR=(frontPixels[index]>>16)&0xff;
frontG=(frontPixels[index]>>8)&0xff;
frontB=frontPixels[index]&0xff;
//取出alpha像素值
alphaR=(alphaPixels[index]>>16)&0xff;
alphaG=(alphaPixels[index]>>8)&0xff;
alphaB=alphaPixels[index]&0xff;
//取出背景图像像素值
backgroundR=(backgroundPixels[index]>>16)&0xff;
backgroundG=(backgroundPixels[index]>>8)&0xff;
backgroundB=backgroundPixels[index]&0xff;
//重新合成 ImgOut = F * alpha/255 + BG * ( 1 - alpha/255 )
frontR= frontR*alphaR/255 + backgroundR*(1-alphaR/255);
frontG=frontG*alphaG/255 + backgroundG*(1-alphaG/255);
frontB=frontB*alphaB/255 + backgroundB*(1-alphaB/255);
frontPixels[index]=(int)frontA<<24|((int)frontR<<16)|((int)frontG<<8)|(int)frontB;
}
}
result.setPixels(frontPixels,0,width,0,0,width,height);;
return result;
}
}
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="108dp"
android:height="108dp"
android:viewportWidth="108"
android:viewportHeight="108">
<path
android:fillType="evenOdd"
android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z"
android:strokeWidth="1"
android:strokeColor="#00000000">
<aapt:attr name="android:fillColor">
<gradient
android:endX="78.5885"
android:endY="90.9159"
android:startX="48.7653"
android:startY="61.0927"
android:type="linear">
<item
android:color="#44000000"
android:offset="0.0" />
<item
android:color="#00000000"
android:offset="1.0" />
</gradient>
</aapt:attr>
</path>
<path
android:fillColor="#FFFFFF"
android:fillType="nonZero"
android:pathData="M66.94,46.02L66.94,46.02C72.44,50.07 76,56.61 76,64L32,64C32,56.61 35.56,50.11 40.98,46.06L36.18,41.19C35.45,40.45 35.45,39.3 36.18,38.56C36.91,37.81 38.05,37.81 38.78,38.56L44.25,44.05C47.18,42.57 50.48,41.71 54,41.71C57.48,41.71 60.78,42.57 63.68,44.05L69.11,38.56C69.84,37.81 70.98,37.81 71.71,38.56C72.44,39.3 72.44,40.45 71.71,41.19L66.94,46.02ZM62.94,56.92C64.08,56.92 65,56.01 65,54.88C65,53.76 64.08,52.85 62.94,52.85C61.8,52.85 60.88,53.76 60.88,54.88C60.88,56.01 61.8,56.92 62.94,56.92ZM45.06,56.92C46.2,56.92 47.13,56.01 47.13,54.88C47.13,53.76 46.2,52.85 45.06,52.85C43.92,52.85 43,53.76 43,54.88C43,56.01 43.92,56.92 45.06,56.92Z"
android:strokeWidth="1"
android:strokeColor="#00000000" />
</vector>
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="108dp"
android:height="108dp"
android:viewportWidth="108"
android:viewportHeight="108">
<path
android:fillColor="#008577"
android:pathData="M0,0h108v108h-108z" />
<path
android:fillColor="#00000000"
android:pathData="M9,0L9,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M19,0L19,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M29,0L29,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M39,0L39,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M49,0L49,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M59,0L59,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M69,0L69,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M79,0L79,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M89,0L89,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M99,0L99,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,9L108,9"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,19L108,19"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,29L108,29"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,39L108,39"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,49L108,49"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,59L108,59"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,69L108,69"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,79L108,79"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,89L108,89"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,99L108,99"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M19,29L89,29"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M19,39L89,39"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M19,49L89,49"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M19,59L89,59"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M19,69L89,69"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M19,79L89,79"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M29,19L29,89"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M39,19L39,89"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M49,19L49,89"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M59,19L59,89"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M69,19L69,89"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M79,19L79,89"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
</vector>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/v_input_info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical">
<TextView
android:id="@+id/tv_input_setting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginTop="10dp"
android:layout_marginRight="12dp"
android:layout_marginBottom="5dp"
android:lineSpacingExtra="4dp"
android:maxLines="6"
android:scrollbars="vertical"
android:singleLine="false"
android:text="" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/v_output_info"
android:layout_below="@+id/v_input_info">
<ImageView
android:id="@+id/iv_input_image"
android:layout_width="400dp"
android:layout_height="400dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/v_output_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<TextView
android:id="@+id/tv_output_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scrollbars="vertical"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:textAlignment="center"
android:lineSpacingExtra="5dp"
android:singleLine="false"
android:maxLines="5"
android:text=""
android:gravity="center_horizontal" />
<TextView
android:id="@+id/tv_inference_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_output_result"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textAlignment="center"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="10dp"
android:text=""
android:gravity="center_horizontal" />
<Button
android:id="@+id/save_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_inference_time"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textAlignment="center"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="10dp"
android:text="保存图像"
android:gravity="center"
android:onClick="clickSaveImg"/>
<ImageView
android:id="@+id/paddlelogo"
android:layout_width="400dp"
android:layout_height="40dp"
android:layout_below="@+id/save_img"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="@drawable/paddle_logo"/>
</RelativeLayout>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
\ No newline at end of file
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:id="@+id/pick_image">
<item
android:id="@+id/open_gallery"
android:title="打开本地相册"
app:showAsAction="withText" />
<item
android:id="@+id/take_photo"
android:title="打开摄像头拍照"
app:showAsAction="withText" />
</group>
<group>
<item
android:id="@+id/settings"
android:title="设置"
app:showAsAction="withText" />
</group>
</menu>
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment