-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The first version of our implemented liveness detection demo.
- Loading branch information
1 parent
05b8c59
commit f81f001
Showing
36 changed files
with
946 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
/.idea/caches | ||
/.idea/libraries | ||
/.idea/modules.xml | ||
/.idea/workspace.xml | ||
/.idea/navEditor.xml | ||
/.idea/assetWizardSettings.xml | ||
.DS_Store | ||
/build | ||
/captures | ||
.externalNativeBuild | ||
.cxx | ||
local.properties | ||
/.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
plugins { | ||
id 'com.android.application' | ||
id 'org.jetbrains.kotlin.android' | ||
} | ||
|
||
android { | ||
namespace 'com.kbyai.facelivedemo' | ||
compileSdk 33 | ||
|
||
defaultConfig { | ||
applicationId "com.kbyai.facelivedemo" | ||
minSdk 24 | ||
targetSdk 33 | ||
versionCode 1 | ||
versionName "1.0" | ||
|
||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_8 | ||
targetCompatibility JavaVersion.VERSION_1_8 | ||
} | ||
kotlinOptions { | ||
jvmTarget = '1.8' | ||
} | ||
} | ||
|
||
dependencies { | ||
|
||
implementation 'androidx.core:core-ktx:1.7.0' | ||
implementation 'androidx.appcompat:appcompat:1.6.1' | ||
implementation 'com.google.android.material:material:1.8.0' | ||
implementation 'androidx.constraintlayout:constraintlayout:2.1.4' | ||
implementation project(path: ':libfacesdk') | ||
testImplementation 'junit:junit:4.13.2' | ||
androidTestImplementation 'androidx.test.ext:junit:1.1.5' | ||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' | ||
|
||
implementation "androidx.camera:camera-core:1.0.0-beta12" | ||
implementation "androidx.camera:camera-camera2:1.0.0-beta12" | ||
implementation "androidx.camera:camera-lifecycle:1.0.0-beta12" | ||
implementation 'androidx.camera:camera-view:1.0.0-alpha19' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
24 changes: 24 additions & 0 deletions
24
app/src/androidTest/java/com/kbyai/facelivedemo/ExampleInstrumentedTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.kbyai.facelivedemo | ||
|
||
import androidx.test.platform.app.InstrumentationRegistry | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
|
||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
import org.junit.Assert.* | ||
|
||
/** | ||
* Instrumented test, which will execute on an Android device. | ||
* | ||
* See [testing documentation](http://d.android.com/tools/testing). | ||
*/ | ||
@RunWith(AndroidJUnit4::class) | ||
class ExampleInstrumentedTest { | ||
@Test | ||
fun useAppContext() { | ||
// Context of the app under test. | ||
val appContext = InstrumentationRegistry.getInstrumentation().targetContext | ||
assertEquals("com.kbyai.facelivedemo", appContext.packageName) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools"> | ||
|
||
<uses-permission android:name="android.permission.CAMERA"/> | ||
<uses-feature android:name="android.hardware.camera"/> | ||
|
||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | ||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:dataExtractionRules="@xml/data_extraction_rules" | ||
android:fullBackupContent="@xml/backup_rules" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:supportsRtl="true" | ||
android:theme="@style/Theme.FaceLivenessDetection" | ||
tools:targetApi="31"> | ||
|
||
<activity android:name=".CameraActivity" | ||
android:screenOrientation="portrait" | ||
android:exported="true"> | ||
|
||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
|
||
</activity> | ||
</application> | ||
|
||
</manifest> |
226 changes: 226 additions & 0 deletions
226
app/src/main/java/com/kbyai/facelivedemo/CameraActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
package com.kbyai.facelivedemo; | ||
|
||
|
||
import android.Manifest; | ||
import android.annotation.SuppressLint; | ||
import android.content.pm.PackageManager; | ||
import android.graphics.Bitmap; | ||
import android.media.Image; | ||
import android.os.Bundle; | ||
import android.util.Log; | ||
import android.util.Size; | ||
import android.view.View; | ||
import android.widget.ImageView; | ||
import android.widget.TextView; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.appcompat.app.AppCompatActivity; | ||
import androidx.camera.core.Camera; | ||
import androidx.camera.core.CameraSelector; | ||
import androidx.camera.core.ImageAnalysis; | ||
import androidx.camera.core.ImageProxy; | ||
import androidx.camera.core.Preview; | ||
import androidx.camera.lifecycle.ProcessCameraProvider; | ||
import androidx.camera.view.PreviewView; | ||
import androidx.core.app.ActivityCompat; | ||
import androidx.core.content.ContextCompat; | ||
|
||
import com.google.common.util.concurrent.ListenableFuture; | ||
import com.kbyai.facesdk.FaceBox; | ||
import com.kbyai.facesdk.FaceSDK; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.List; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
import static androidx.camera.core.ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST; | ||
|
||
public class CameraActivity extends AppCompatActivity { | ||
|
||
static String TAG = CameraActivity.class.getSimpleName(); | ||
static int PREVIEW_WIDTH = 720; | ||
static int PREVIEW_HEIGHT = 1280; | ||
static float LIVENESS_THRESHOLD = 0.7f; | ||
|
||
private ExecutorService cameraExecutorService; | ||
private PreviewView viewFinder; | ||
private int lensFacing = CameraSelector.LENS_FACING_FRONT; | ||
|
||
private Preview preview = null; | ||
private ImageAnalysis imageAnalyzer = null; | ||
private Camera camera = null; | ||
private CameraSelector cameraSelector = null; | ||
private ProcessCameraProvider cameraProvider = null; | ||
|
||
private FaceView faceView; | ||
|
||
private TextView initView; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_camera); | ||
|
||
viewFinder = findViewById(R.id.preview); | ||
faceView = findViewById(R.id.faceView); | ||
initView = findViewById(R.id.initView); | ||
cameraExecutorService = Executors.newFixedThreadPool(1); | ||
|
||
int ret = FaceSDK.setActivation("GcVbBmdTI24WPXz3tBsr87dHU/KzmFuMYWm4SrLoFrFECoUouwdeSkWhAHi+k/AAYQW22jvctX+j\n" + | ||
"oDg+I1VLKJQgnJS2d80bwCKceYFXOc9A7jBi0/BEKx4PeSGz/PEpoPm/fFT9GrXXnLI4jZj8tMRX\n" + | ||
"cYS4MO7ECQkQmFtqU4wx63mV2MP97WCurOGBRUuVm4dK+mObTVA/rBNrgS75ArcjwwIKexMU5YCY\n" + | ||
"qjLsmADlcnEu5vuYCagDeHyCkUIqvl0/KLicM68BEG2ItUDflhOZJkjOof3gWPSmzJveXuoyPCR5\n" + | ||
"aSBeesUcwQv9xdKciBacMX/pDDMsbqv614gQ5A=="); | ||
|
||
if(ret == FaceSDK.SDK_SUCCESS) { | ||
ret = FaceSDK.init(getAssets()); | ||
} | ||
|
||
if(ret != FaceSDK.SDK_SUCCESS) { | ||
initView.setVisibility(View.VISIBLE); | ||
if(ret == FaceSDK.SDK_LICENSE_KEY_ERROR) { | ||
initView.setText("Invalid license!"); | ||
} else if(ret == FaceSDK.SDK_LICENSE_APPID_ERROR) { | ||
initView.setText("Invalid error!"); | ||
} else if(ret == FaceSDK.SDK_LICENSE_EXPIRED) { | ||
initView.setText("License expired!"); | ||
} else if(ret == FaceSDK.SDK_NO_ACTIVATED) { | ||
initView.setText("No activated!"); | ||
} else if(ret == FaceSDK.SDK_INIT_ERROR) { | ||
initView.setText("Init error!"); | ||
} | ||
} | ||
|
||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) | ||
== PackageManager.PERMISSION_DENIED) { | ||
|
||
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1); | ||
} else { | ||
viewFinder.post(() -> | ||
{ | ||
setUpCamera(); | ||
}); | ||
} | ||
} | ||
|
||
@Override | ||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | ||
super.onRequestPermissionsResult(requestCode, permissions, grantResults); | ||
|
||
if(requestCode == 1) { | ||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) | ||
== PackageManager.PERMISSION_GRANTED) { | ||
|
||
viewFinder.post(() -> | ||
{ | ||
setUpCamera(); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
private void setUpCamera() | ||
{ | ||
ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(CameraActivity.this); | ||
cameraProviderFuture.addListener(() -> { | ||
|
||
// CameraProvider | ||
try { | ||
cameraProvider = cameraProviderFuture.get(); | ||
} catch (ExecutionException e) { | ||
} catch (InterruptedException e) { | ||
} | ||
|
||
// Build and bind the camera use cases | ||
bindCameraUseCases(); | ||
|
||
}, ContextCompat.getMainExecutor(CameraActivity.this)); | ||
} | ||
|
||
@SuppressLint({"RestrictedApi", "UnsafeExperimentalUsageError"}) | ||
private void bindCameraUseCases() | ||
{ | ||
int rotation = viewFinder.getDisplay().getRotation(); | ||
|
||
cameraSelector = new CameraSelector.Builder().requireLensFacing(lensFacing).build(); | ||
|
||
preview = new Preview.Builder() | ||
.setTargetResolution(new Size(PREVIEW_WIDTH, PREVIEW_HEIGHT)) | ||
.setTargetRotation(rotation) | ||
.build(); | ||
|
||
imageAnalyzer = new ImageAnalysis.Builder() | ||
.setBackpressureStrategy(STRATEGY_KEEP_ONLY_LATEST) | ||
.setTargetResolution(new Size(PREVIEW_WIDTH, PREVIEW_HEIGHT)) | ||
// Set initial target rotation, we will have to call this again if rotation changes | ||
// during the lifecycle of this use case | ||
.setTargetRotation(rotation) | ||
.build(); | ||
|
||
imageAnalyzer.setAnalyzer(cameraExecutorService, new FaceAnalyzer()); | ||
|
||
cameraProvider.unbindAll(); | ||
|
||
try { | ||
camera = cameraProvider.bindToLifecycle( | ||
this, cameraSelector, preview, imageAnalyzer); | ||
|
||
preview.setSurfaceProvider(viewFinder.getSurfaceProvider()); | ||
} catch (Exception exc) { | ||
} | ||
} | ||
|
||
class FaceAnalyzer implements ImageAnalysis.Analyzer | ||
{ | ||
@SuppressLint("UnsafeExperimentalUsageError") | ||
@Override | ||
public void analyze(@NonNull ImageProxy imageProxy) | ||
{ | ||
analyzeImage(imageProxy); | ||
} | ||
} | ||
|
||
@SuppressLint("UnsafeExperimentalUsageError") | ||
private void analyzeImage(ImageProxy imageProxy) | ||
{ | ||
try | ||
{ | ||
Image image = imageProxy.getImage(); | ||
|
||
Image.Plane[] planes = image.getPlanes(); | ||
ByteBuffer yBuffer = planes[0].getBuffer(); | ||
ByteBuffer uBuffer = planes[1].getBuffer(); | ||
ByteBuffer vBuffer = planes[2].getBuffer(); | ||
|
||
int ySize = yBuffer.remaining(); | ||
int uSize = uBuffer.remaining(); | ||
int vSize = vBuffer.remaining(); | ||
|
||
byte[] nv21 = new byte[ySize + uSize + vSize]; | ||
yBuffer.get(nv21, 0, ySize); | ||
vBuffer.get(nv21, ySize, vSize); | ||
uBuffer.get(nv21, ySize + vSize, uSize); | ||
|
||
Bitmap bitmap = FaceSDK.yuv2Bitmap(nv21, image.getWidth(), image.getHeight(), 7); | ||
List<FaceBox> faceBoxes = FaceSDK.faceDetection(bitmap); | ||
|
||
runOnUiThread(new Runnable() { | ||
@Override | ||
public void run() { | ||
faceView.setFrameSize(new Size(bitmap.getWidth(), bitmap.getHeight())); | ||
faceView.setFaceBoxes(faceBoxes); | ||
} | ||
}); | ||
} | ||
catch (Exception e) | ||
{ | ||
e.printStackTrace(); | ||
} | ||
finally | ||
{ | ||
imageProxy.close(); | ||
} | ||
} | ||
} |
Oops, something went wrong.