Spoof detection for Ver-ID SDK for Android using L500 model
-
Add the following dependency in your build.gradle.kts file:
implementation(platform("com.appliedrece:verid-bom:3001.3.0")) implementation("com.appliedrec:spoof-detection-l500-cloud")
-
Contact Applied Recognition to obtain an API key and server URL. For testing you can use credentials from the config.json file included in this project’s test target. However, these credentials are rate limited and unsuitable for production use.
-
There are two ways to supply the credentials to
SpoofDetectionL500:-
Store the credentials in a gradle.properties or local.properties file and inject them into your app’s AndroidManifest.xml. This is the recommended way to ensure that the credentials are not checked into SCM.
-
Add the values in your local.properties or gradle.properties file:
L500_API_KEY=your_api_key L500_SERVER_URL=https://server.url -
Expose them as manifest placeholders in your app module’s build.gradle:
android { defaultConfig { manifestPlaceholders = [ L500_API_KEY: project.findProperty("L500_API_KEY") ?: "", L500_SERVER_URL: project.findProperty("L500_SERVER_URL") ?: "" ] } } -
Reference the placeholders in AndroidManifest.xml:
<manifest> <application> <meta-data android:name="com.appliedrec.spoof-detection.l500.apiKey" android:value="${L500_API_KEY}" /> <meta-data android:name="com.appliedrec.spoof-detection.l500.serverUrl" android:value="${L500_SERVER_URL}" /> </application> </manifest>
-
Construct an instance of
SpoofDetection500passing your app’s context to the constructor:val spoofDetection = SpoofDetection500(context)
-
-
Supply the credentials directly to the
SpoofDetectionL500constructor:val spoofDetection = SpoofDetection500("<your API key>", "<server URL>")
-
You’ll most likely want to use spoof detection during face capture. You can add the spoof detector from this library to your face capture session configuration.
val configuration = FaceCaptureConfiguration(
FaceCaptureSessionSettings(),
FaceCaptureViewConfiguration(context),
{ FaceDetectionRetinaFace.create(context) },
{ listOf(
LivenessDetectionPlugin(arrayOf(
SpoofDetectionL500(context)
)) as FaceTrackingPlugin<Any>
) }
)
val result = FaceCapture.captureFaces(context, configuration)
if (result is FaceCaptureSessionResult.Failure) {
// Inspect result.error to see if liveness failed
result.error
}Add Ver-ID serialisation (for converting between image types) and face detection dependencies in your build.gradle.kts file:
implementation("com.appliedrec:verid-serialization")
implementation("com.appliedrec:face-detection-retinaface")Detect a spoof in an image. The function below returns true of the face in the image is spoofed or false otherwise.
suspend fun detectSpoofInImage(bitmap: Bitmap): Boolean {
// Convert bitmap to Ver-ID image type
val image = Image.fromBitmap(bitmap)
// Detect a face
val face = FaceDetectionRetinaFace.create(context).use { faceDetection ->
faceDetection.detectFacesInImage(image, 1).firstOrNull()
} ?: return false // No face = no spoof
// Detect spoof
val isSpoof = SpoofDetection500(context).use { spoofDetection ->
spoofDetection.isImageSpoofed(image, face.bounds)
}
return isSpoof
}