Skip to content

Commit

Permalink
feat: android work well with vnn
Browse files Browse the repository at this point in the history
  • Loading branch information
jaaronkot committed Feb 19, 2024
1 parent e1f6137 commit 9417635
Show file tree
Hide file tree
Showing 25 changed files with 366 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ public class MainActivity extends AppCompatActivity implements SurfaceHolder.Cal
private GPUPixelSourceCamera sourceCamera;
private GPUPixelView surfaceView;
private GPUPixelFilter beautyFaceFilter;
private GPUPixelFilter thinfaceFilter;
private Button btnStart;
private GPUPixelFilter faceReshapFilter;
private GPUPixelFilter lipstickFilter;
private SeekBar smooth_seekbar;
private SeekBar whiteness_seekbar;
private SeekBar face_reshap_seekbar;
private SeekBar big_eye_seekbar;
private SeekBar lipstick_seekbar;

private ActivityMainBinding binding;

Expand All @@ -48,7 +51,7 @@ protected void onCreate(Bundle savedInstanceState) {
String path = getExternalFilesDir("gpupixel").getAbsolutePath();
Log.i(TAG, path);

GPUPixel.CopyModel(this);
GPUPixel.setContext(this);
// 保持屏幕常亮
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

Expand Down Expand Up @@ -78,7 +81,7 @@ public void onStopTrackingTouch(SeekBar seekBar) {
whiteness_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
beautyFaceFilter.setProperty("whiteness", progress/10.0);
beautyFaceFilter.setProperty(GPUPixelFilter.BeautyFaceFilter.propWhiteLevel, progress/10.0);
}

@Override
Expand All @@ -91,23 +94,87 @@ public void onStopTrackingTouch(SeekBar seekBar) {

}
});


face_reshap_seekbar = findViewById(R.id.thinface_seekbar);
face_reshap_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
faceReshapFilter.setProperty(GPUPixelFilter.FaceReshapeFilter.propThinLevel, progress/200.0);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}
});

big_eye_seekbar = findViewById(R.id.bigeye_seekbar);
big_eye_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
faceReshapFilter.setProperty(GPUPixelFilter.FaceReshapeFilter.propBigeyeLevel, progress/100.0);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}
});

lipstick_seekbar = findViewById(R.id.lipstick_seekbar);
lipstick_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
lipstickFilter.setProperty(GPUPixelFilter.LipstickFilter.propBlendLevel, progress/10.0);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}
});

//
this.checkCameraPermission();
}

public void startCameraFilter() {
// 美颜滤镜
beautyFaceFilter = GPUPixelFilter.create(GPUPixelFilter.BeautyFaceFilter.name);
thinfaceFilter = GPUPixelFilter.create(GPUPixelFilter.FaceReshapeFilter.name);
// beautyFaceFilter = GPUPixelFilter.create(GPUPixelFilter.BeautyFaceFilter.name);

faceReshapFilter = GPUPixelFilter.create(GPUPixelFilter.FaceReshapeFilter.name);
lipstickFilter = GPUPixelFilter.create(GPUPixelFilter.LipstickFilter.name);
// camera
sourceCamera = new GPUPixelSourceCamera(this.getApplicationContext());
sourceCamera.addTarget(beautyFaceFilter);
// thinfaceFilter.addTarget(beautyFaceFilter);

//
sourceCamera.addTarget(lipstickFilter);
lipstickFilter.addTarget(faceReshapFilter);
faceReshapFilter.addTarget(beautyFaceFilter);
beautyFaceFilter.addTarget(surfaceView);

sourceCamera.setLandmarkCallbck(thinfaceFilter);
sourceCamera.setLandmarkCallbck(new GPUPixel.GPUPixelLandmarkCallback() {
@Override
public void onFaceLandmark(float[] landmarks) {
faceReshapFilter.setProperty(GPUPixelFilter.FaceReshapeFilter.propFaceLandmark, landmarks);
lipstickFilter.setProperty(GPUPixelFilter.BlusherFilter.propFaceLandmark, landmarks);
}
});
// set default value
beautyFaceFilter.setProperty(GPUPixelFilter.BeautyFaceFilter.propSmoothLevel, 0.5);
beautyFaceFilter.setProperty(GPUPixelFilter.BeautyFaceFilter.propWhiteLevel, 0.4);
Expand Down
77 changes: 71 additions & 6 deletions src/android/java/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,104 @@
android:id="@+id/mopi"
android:layout_width="100dp"
android:layout_height="30dp"
android:text="Skin Smooth"
android:text="smooth"
android:textColor="@color/white"
android:layout_alignParentBottom="true"
android:layout_marginLeft="20dp"
android:layout_marginBottom="90dp" />
android:layout_marginBottom="200dp" />

<SeekBar
android:id="@+id/smooth_seekbar"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="80dp"
android:layout_marginBottom="190dp"
android:max="10"
android:progress="0" />

<!--white-->
<TextView
android:id="@+id/meibai"
android:id="@+id/skin_white"
android:layout_width="100dp"
android:layout_height="30dp"
android:text="Skin White"
android:text="white"
android:textColor="@color/white"
android:layout_alignParentBottom="true"
android:layout_marginLeft="20dp"
android:layout_marginBottom="40dp" />
android:layout_marginBottom="160dp" />

<SeekBar
android:id="@+id/whiteness_seekbar"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="150dp"
android:max="10"
android:progress="0" />

<!--thinface-->
<TextView
android:id="@+id/thinface"
android:layout_width="100dp"
android:layout_height="30dp"
android:text="thinface"
android:textColor="@color/white"
android:layout_alignParentBottom="true"
android:layout_marginLeft="20dp"
android:layout_marginBottom="120dp" />

<SeekBar
android:id="@+id/thinface_seekbar"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="110dp"
android:max="10"
android:progress="0" />

<!--big eye-->
<TextView
android:id="@+id/bigeye"
android:layout_width="100dp"
android:layout_height="30dp"
android:text="big eye"
android:textColor="@color/white"
android:layout_alignParentBottom="true"
android:layout_marginLeft="20dp"
android:layout_marginBottom="80dp" />

<SeekBar
android:id="@+id/bigeye_seekbar"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="70dp"
android:max="10"
android:progress="0" />

<!--lipstick-->
<TextView
android:id="@+id/lipstick"
android:layout_width="100dp"
android:layout_height="30dp"
android:text="lipstick"
android:textColor="@color/white"
android:layout_alignParentBottom="true"
android:layout_marginLeft="20dp"
android:layout_marginBottom="40dp" />

<SeekBar
android:id="@+id/lipstick_seekbar"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="30dp"
android:max="10"
android:progress="0" />

</RelativeLayout>
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
import java.io.InputStream;

public class GPUPixel {

public interface GPUPixelLandmarkCallback {
public void onFaceLandmark(float[] landmarks);
}
public static final int NoRotation = 0;
public static final int RotateLeft = 1;
public static final int RotateRight = 2;
Expand All @@ -27,7 +31,7 @@ public class GPUPixel {
public static final int RotateRightFlipHorizontal = 6;
public static final int Rotate180 = 7;

public static String model_path;
public static String resource_path;
private GPUPixelRenderer mRenderer = null;
private GLSurfaceView mGLSurfaceView = null;
private int mGLSurfaceViewRenderMode = GLSurfaceView.RENDERMODE_WHEN_DIRTY;
Expand All @@ -49,6 +53,9 @@ public static final GPUPixel getInstance() {
public boolean isInited() {
return mRenderer != null;
}
public static void setContext(Context context) {
copyResource(context);
}

public void init() {
mRenderer = new GPUPixelRenderer();
Expand Down Expand Up @@ -150,14 +157,14 @@ public void runOnPostDraw(Runnable runnable) {
System.loadLibrary("vnn_face");
}

public static void CopyModel(Context context) {
public static void copyResource(Context context) {
String exPath = context.getExternalFilesDir(null).getAbsolutePath();
copyAssetsToFiles(context, "vnn_models", exPath + "/vnn_models");
model_path = exPath + "/vnn_models";
copyAssetsToFiles(context, "resource", exPath + "/resource");
resource_path = exPath + "/resource";
}

public static String getModel_path() {
return model_path;
public static String getResource_path() {
return resource_path;
}

public static void copyAssetsToFiles(Context context, String oldPath, String newPath) {
Expand Down Expand Up @@ -205,8 +212,8 @@ public static void copyAssetsToFiles(Context context, String oldPath, String ne
public static native void nativeFilterFinalize(long classID);
public static native void nativeFilterSetPropertyFloat(long classID, String property, float value);
public static native void nativeFilterSetPropertyInt(long classID, String property, int value);
public static native void nativeFilterSetPropertyFloatArray(long classID, String property, float[] array);
public static native void nativeFilterSetPropertyString(long classID, String prooerty, String value);

// SourceImage
public static native long nativeSourceImageNew();
public static native void nativeSourceImageDestroy(final long classID);
Expand Down Expand Up @@ -247,6 +254,6 @@ public static void copyAssetsToFiles(Context context, String oldPath, String ne
// utils
public static native void nativeYUVtoRBGA(byte[] yuv, int width, int height, int[] out);

public static native void nativeSetLandmarkCallback(final long classID, final long filterID);
public static native void nativeSetLandmarkCallback(Object source, final long classID);

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ public void run() {
});
}

public void setProperty(final String property, final float[] array){
GPUPixel.getInstance().runOnDraw(new Runnable() {
@Override
public void run() {
if (mNativeClassID != 0) {
GPUPixel.nativeFilterSetPropertyFloatArray(mNativeClassID, property, array);
}
}
});
}

public void setProperty(final String property, final int value){
GPUPixel.getInstance().runOnDraw(new Runnable() {
@Override
Expand Down Expand Up @@ -123,5 +134,18 @@ public final class FaceReshapeFilter {
public static final String name = "FaceReshapeFilter";
public static final String propThinLevel = "thin_face";
public static final String propBigeyeLevel = "big_eye";
public static final String propFaceLandmark = "face_landmark";
}

public final class LipstickFilter {
public static final String name = "LipstickFilter";
public static final String propBlendLevel = "blend_level";
public static final String propFaceLandmark = "face_landmark";
}

public final class BlusherFilter {
public static final String name = "BlusherFilter";
public static final String propBlendLevel = "blend_level";
public static final String propFaceLandmark = "face_landmark";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@
import android.hardware.Camera;
import android.opengl.GLES20;
import android.os.Build;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.WindowManager;
import java.io.IOException;
import java.nio.IntBuffer;
import com.pixpark.gpupixel.GPUPixelSourceRawInput;

public class GPUPixelSourceCamera extends GPUPixelSource implements Camera.PreviewCallback {
private Camera mCamera;
Expand All @@ -30,9 +28,11 @@ public class GPUPixelSourceCamera extends GPUPixelSource implements Camera.Previ
private Context mContext;
private SurfaceTexture mSurfaceTexture = null;
private GPUPixelSourceRawInput SourceRawDataInput = null;

private Object object_this;
private GPUPixel.GPUPixelLandmarkCallback landmarkCallback;
public GPUPixelSourceCamera(Context context) {
mContext = context;
object_this = this;
if (mNativeClassID != 0) return;
GPUPixel.getInstance().runOnDraw(new Runnable() {
@Override
Expand All @@ -44,14 +44,22 @@ public void run() {
setUpCamera(mCurrentCameraId);
}

public void setLandmarkCallbck(GPUPixelFilter filter) {
public void setLandmarkCallbck(GPUPixel.GPUPixelLandmarkCallback filter) {
landmarkCallback = filter;

GPUPixel.getInstance().runOnDraw(new Runnable() {
@Override
public void run() {
GPUPixel.nativeSetLandmarkCallback(mNativeClassID, filter.getNativeClassID());
GPUPixel.nativeSetLandmarkCallback(object_this, mNativeClassID);
}
});
}

// callback by native
public void onFaceLandmark(float[] landmarks) {
if(landmarkCallback != null) {
landmarkCallback.onFaceLandmark(landmarks);
}
}

@Override
Expand Down
Loading

0 comments on commit 9417635

Please sign in to comment.