Skip to content

AppliedRecognition/Spoof-Detection-L500-Android

Repository files navigation

Spoof detection L500

Maven Central Maven Central

Spoof detection for Ver-ID SDK for Android using L500 model

Installation

  1. 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")
  2. 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.

  3. There are two ways to supply the credentials to SpoofDetectionL500:

    1. 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.

      1. Add the values in your local.properties or gradle.properties file:

        L500_API_KEY=your_api_key
        L500_SERVER_URL=https://server.url
        
      2. 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") ?: ""
                ]
            }
        }
      3. 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>
      4. Construct an instance of SpoofDetection500 passing your app’s context to the constructor:

        val spoofDetection = SpoofDetection500(context)
    2. Supply the credentials directly to the SpoofDetectionL500 constructor:

      val spoofDetection = SpoofDetection500("<your API key>", "<server URL>")

Usage

In face capture

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
}

Standalone

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
}

About

Spoof detection for Android using L500 model

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages