Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add face detect library #168

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.android.tools.build:gradle:3.0.0-alpha6'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
125 changes: 124 additions & 1 deletion cgeDemo/src/main/java/org/wysaid/cgeDemo/CameraDemoActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PointF;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Bundle;
Expand All @@ -18,12 +19,17 @@
import android.widget.LinearLayout;
import android.widget.SeekBar;

import com.sensetime.stmobileapi.STMobileFaceAction;
import com.sensetime.stmobileapi.STMobileMultiTrack106;
import com.sensetime.stmobileapi.STUtils;

import org.wysaid.camera.CameraInstance;
import org.wysaid.myUtils.FileUtil;
import org.wysaid.myUtils.ImageUtil;
import org.wysaid.myUtils.MsgUtil;
import org.wysaid.nativePort.CGEFrameRecorder;
import org.wysaid.nativePort.CGENativeLibrary;
import org.wysaid.stmobile.Accelerometer;
import org.wysaid.view.CameraRecordGLSurfaceView;

public class CameraDemoActivity extends AppCompatActivity {
Expand Down Expand Up @@ -338,12 +344,110 @@ public void onAutoFocus(boolean success, Camera camera) {
});

mCameraView.setPictureSize(600, 800, true);

Button faceDetectBtn = (Button) findViewById(R.id.face_detect_btn);
faceDetectBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CameraInstance.getInstance().getCameraDevice().setPreviewCallback(new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
if (mNV21Data == null) {
mNV21Data = new byte[CameraInstance.getInstance().previewHeight() * CameraInstance.getInstance().previewWidth() * 2];
}
synchronized (mNV21Data) {
System.arraycopy(data, 0, mNV21Data, 0, data.length);
mIsNV21ready = true;
}
}
});
mCameraView.setFilterWithConfig("@facedetect");
}
});
mAcc = new Accelerometer(this);
mAcc.start();
}

private byte[] mNV21Data;
private boolean mIsFaceDetectThreadKilled = false;
private Thread mFaceDetectThread;
private boolean mIsNV21ready = false;
private Accelerometer mAcc;
private STMobileMultiTrack106 mTracker = null;
private static final int ST_MOBILE_TRACKING_ENABLE_FACE_ACTION = 0x00000020;
private byte[] tmp = null;

private void stopFaceDetectThread() {
mIsFaceDetectThreadKilled = true;
if (mFaceDetectThread != null) {
try {
mFaceDetectThread.join(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}

private void startFaceDetectThread() {
mIsFaceDetectThreadKilled = false;

mFaceDetectThread = new Thread() {
@Override
public void run() {
while (!mIsFaceDetectThreadKilled) {
if (!mIsNV21ready) {
continue;
}
if (tmp == null) {
tmp = new byte[CameraInstance.getInstance().previewHeight() * CameraInstance.getInstance().previewWidth() * 2];
}
synchronized (mNV21Data) {
System.arraycopy(mNV21Data, 0, tmp, 0, mNV21Data.length);
mIsNV21ready = false;
}
boolean frontCamera = (CameraInstance.getInstance().getFacing() == Camera.CameraInfo.CAMERA_FACING_FRONT);
int dir = Accelerometer.getDirection();
/**
* 请注意前置摄像头与后置摄像头旋转定义不同
* 请注意不同手机摄像头旋转定义不同
*/
if (frontCamera &&
((CameraInstance.getInstance().getCameraInfo().orientation == 270 && (dir & 1) == 1) ||
(CameraInstance.getInstance().getCameraInfo().orientation == 90 && (dir & 1) == 0))) {
dir = (dir ^ 2);
}
STMobileFaceAction[] faceActions = mTracker.trackFaceAction(tmp, dir, CameraInstance.getInstance().previewWidth(), CameraInstance.getInstance().previewHeight());
boolean rotate270 = CameraInstance.getInstance().getCameraInfo().orientation == 270;
Log.e("xuezi", "rotate270" + rotate270);
if (faceActions != null && faceActions.length > 0) {
STMobileFaceAction r = faceActions[0];
PointF[] points = r.getFace().getPointsArray();
float[] pointArray = new float[212];
for (int i = 0; i < points.length; i++) {
if (rotate270) {
points[i] = STUtils.RotateDeg270(points[i], CameraInstance.getInstance().previewWidth(), CameraInstance.getInstance().previewHeight(), frontCamera);
} else {
points[i] = STUtils.RotateDeg90(points[i], CameraInstance.getInstance().previewWidth(), CameraInstance.getInstance().previewHeight(), frontCamera);
}
pointArray[2*i] = points[i].x / CameraInstance.getInstance().previewHeight();
pointArray[2*i+1] = points[i].y / CameraInstance.getInstance().previewWidth();
}
mCameraView.setFaceDetectPoints(pointArray);
}
else {
mCameraView.clearFaceDetectPoints();
}
}
}
};
mFaceDetectThread.start();
}

private View.OnClickListener mFilterSwitchListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
MyButtons btn = (MyButtons) v;
CameraInstance.getInstance().getCameraDevice().setPreviewCallback(null);
MyButtons btn = (MyButtons)v;
mCameraView.setFilterWithConfig(btn.filterConfig);
mCurrentConfig = btn.filterConfig;
}
Expand Down Expand Up @@ -388,13 +492,32 @@ public void onPause() {
Log.i(LOG_TAG, "activity onPause...");
mCameraView.release(null);
mCameraView.onPause();
mAcc.stop();
stopFaceDetectThread();
if (mTracker != null) {
System.out.println("destroy tracker");
mTracker.destory();
mTracker = null;
}
}

@Override
public void onResume() {
super.onResume();

mCameraView.onResume();
mAcc.start();
if (mTracker == null) {
// long start_init = System.currentTimeMillis();
// int config = 0; //default config
int config = ST_MOBILE_TRACKING_ENABLE_FACE_ACTION;
mTracker = new STMobileMultiTrack106(this, config);
int max = 1;
mTracker.setMaxDetectableFaces(max);
// long end_init = System.currentTimeMillis();
// Log.i("track106", "init cost "+(end_init - start_init) +" ms");
}
startFaceDetectThread();
}

@Override
Expand Down
7 changes: 7 additions & 0 deletions cgeDemo/src/main/res/layout/activity_camera_demo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
</FrameLayout>

<HorizontalScrollView
android:id="@+id/hsl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
Expand Down Expand Up @@ -123,4 +124,10 @@
</LinearLayout>
</HorizontalScrollView>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FaceDetect"
android:id="@+id/face_detect_btn"
android:layout_above="@id/hsl"/>
</RelativeLayout>
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Fri Jun 16 17:02:18 CST 2017
#Fri Jul 14 16:02:35 CST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-milestone-1-all.zip
1 change: 1 addition & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.2.0'
compile project(":stmobile")
}
7 changes: 7 additions & 0 deletions library/src/main/java/org/wysaid/camera/CameraInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public class CameraInstance {

private Camera mCameraDevice;
private Camera.Parameters mParams;
protected Camera.CameraInfo mCameraInfo = null;
public Camera.CameraInfo getCameraInfo() {
return mCameraInfo;
}

public static final int DEFAULT_PREVIEW_RATE = 30;

Expand Down Expand Up @@ -96,6 +100,8 @@ public synchronized boolean tryOpenCamera(CameraOpenCallback callback, int facin
if (cameraInfo.facing == facing) {
mDefaultCameraID = i;
mFacing = facing;
mCameraInfo = cameraInfo;
break;
}
}
}
Expand Down Expand Up @@ -305,6 +311,7 @@ public void initCamera(int previewRate) {

Log.i(LOG_TAG, String.format("Camera Picture Size: %d x %d", szPic.width, szPic.height));
Log.i(LOG_TAG, String.format("Camera Preview Size: %d x %d", szPrev.width, szPrev.height));

}

public synchronized void setFocusMode(String focusMode) {
Expand Down
15 changes: 12 additions & 3 deletions library/src/main/java/org/wysaid/nativePort/CGEFrameRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void setRenderFlipScale(float x, float y) {
//initialize the filters width config string
public void setFilterWidthConfig(final String config) {
if(mNativeAddress != 0)
nativeSetFilterWidthConfig(mNativeAddress, config);
nativeSetFilterWithConfig(mNativeAddress, config);
}

//set the mask rotation (radian)
Expand All @@ -95,7 +95,6 @@ public void setMaskRotation(float rot) {
public void setMaskFlipScale(float x, float y) {
if(mNativeAddress != 0)
nativeSetMaskFlipScale(mNativeAddress, x, y);

}


Expand Down Expand Up @@ -152,6 +151,14 @@ public void setNativeFilter(long nativeFilter) {
nativeSetFilterWithAddr(mNativeAddress, nativeFilter);
}

public void setFaceDetectPoints(float[] keyPoints) {
nativeSetFaceDetectPoints(mNativeAddress, keyPoints);
}

public void clearFaceDetectPoints() {
nativeClearFaceDetectPoints(mNativeAddress);
}

///////////////// protected ///////////////////////

protected native long nativeCreateRenderer();
Expand All @@ -166,7 +173,7 @@ public void setNativeFilter(long nativeFilter) {
protected native void nativeSetSrcFlipScale(long holder, float x, float y);
protected native void nativeSetRenderRotation(long holder, float rad);
protected native void nativeSetRenderFlipScale(long holder, float x, float y);
protected native void nativeSetFilterWidthConfig(long holder, String config);
protected native void nativeSetFilterWithConfig(long holder, String config);
protected native void nativeSetFilterIntensity(long holder, float value);
protected native void nativeSetMaskRotation(long holder, float value);
protected native void nativeSetMaskFlipScale(long holder, float x, float y);
Expand All @@ -188,4 +195,6 @@ public void setNativeFilter(long nativeFilter) {

//特殊用法, 谨慎使用, 使用不当可能造成程序运行异常.
protected native void nativeSetFilterWithAddr(long holder, long filter);
protected native void nativeSetFaceDetectPoints(long holder, float[] keyPoints);
protected native void nativeClearFaceDetectPoints(long holder);
}
Loading