Skip to content

AppliedRecognition/Facial-Attribute-Detection-Android

Repository files navigation

Facial Attribute Detection for Android

Android library with modules that detect facial attributes. Currently the library contains modules for face covering detection and eyewear detection.

Installation

The library is distributed on Maven Central.

Module Description
com.appliedrec:facial-attribute-detection Core library (imported by the modules below)
com.appliedrec:face-covering-detection Face covering detection
com.appliedrec:eyewear-detection Eyewear detection

Usage

All detectors in the library adopt the FacialAttributeDetection protocol from the VerIDCommonTypes library.

First you have to detect a face. You can use any library that implements the FaceDetection interface, for example FaceDetectionRetinaFace.

Pass the detected face along with the image in which the face was detected to the detect function of the facial attribute detector.

suspend fun detectFaceAttributesInImage(
    context: Context, 
    image: Bitmap
): FaceAttributeDetectionResult {
    
    // Convert image to Ver-ID image
    val verIDImage = Image.fromBitmap(image)

    // Create face detection
    return FaceDetectionRetinaFace.create(context).use { faceDetection ->

        // Detect a face
        faceDetection.detectFacesInImage(verIDImage, 1).firstOrNull()
    }?.let { face ->

        // Create eyewear detector and detect eyewear
        val eyewearDetectionResult = EyewearDetector.create(
            context
        ).use { eyewearDetector ->
            eyewearDetector.detect(face, verIDImage)
        }

        // Create face covering detector and detect face covering
        val faceCoveringDetectionResult = FaceCoveringDetector.create(
            context
        ).use { faceCoveringDetector ->
            faceCoveringDetector.detect(face, verIDImage)
        }

        // Return your custom result type
        FaceAttributeDetectionResult(
            hasFaceCovering = faceCoveringDetectionResult != null,
            hasEyewear = eyewearDetectionResult != null,
            eyewearType = eyewearDetectionResult?.type?.toString()
        )
    } ?: FaceAttributeDetectionResult(
        hasFaceCovering = false,
        hasEyewear = false,
        eyewearType = null
    )
}

// Custom result type that covers both eyewear and face covering
data class FaceAttributeDetectionResult(
    val hasFaceCovering: Boolean,
    val hasEyewear: Boolean,
    val eyewearType: String?
)

About

Facial attribute detection libraries for Android

Resources

License

Stars

Watchers

Forks

Packages

No packages published