Custom face detector with quality function¶
You can create your own a face detector with quality function if you don't want to use the default LocalFaceDetector
.
Creation a custom face detector¶
A custom face detector should implement AutocaptureFaceDetector<Jpeg, *>
interface. In example, we use GoogleFaceDetector
:
// Copy from the official Google repository:
// https://github.com/googlesamples/mlkit/blob/master/android/vision-quickstart/app/src/main/java/com/google/mlkit/vision/demo/kotlin/facedetector/FaceDetectorProcessor.kt
class GoogleFaceDetector : AutocaptureFaceDetector<Jpeg, List<Face>>, AutoCloseable {
private val uiHandler = Handler(Looper.getMainLooper())
var onDetectionResultListener: OnDetectionResultListener? = null
private val detector = FaceDetection.getClient(
FaceDetectorOptions.Builder()
.setClassificationMode(FaceDetectorOptions.CLASSIFICATION_MODE_ALL)
.build(),
)
private var cachedFaces: List<Face> = listOf()
override fun detect(image: Jpeg): List<Face> {
val bitmap = BitmapFactory.decodeByteArray(
image.content,
0,
image.content.size,
)
val task = detector.process(
InputImage.fromBitmap(bitmap, image.imageInfo.rotationDegrees),
)
val latch = CountDownLatch(1)
task.addOnSuccessListener { facesList ->
cachedFaces = facesList
latch.countDown()
}
task.addOnFailureListener {
throw it
latch.countDown()
}
task.addOnCanceledListener {
latch.countDown()
}
latch.await()
uiHandler.post {
onDetectionResultListener?.onDetectionResult(cachedFaces)
}
return cachedFaces
}
override fun isLastImageSuitableForCapturing(): Boolean {
// You should decide here whether the latest image satisfies the conditions.
// It will be used by IadCameraController to automatically capture photos.
return (cachedFaces.firstOrNull()?.smilingProbability ?: 0f) >= 0.5f
}
override fun close() {
detector.close()
}
}
Integration the custom face detector¶
Just replace LocalFaceDetector
with your own face detector in the constructor of IadCameraController
.
IadCameraController(GoogleFaceDetector(), peviewView, lifecycleOwner)
More information¶
You can find an example of this functionality in the idlive-face-capture-android-X.X.X-release/iad-example
folder.