We implemented Android ML Kit for face detection in Android. It works like charm, detect faces.
The problem: We want to draw rectangles around detected faces when multiple faces detected
What we have done:
Implemented
implementation 'com.google.android.gms:play-services-mlkit-face-detection:16.1.5'
Created a custom View :
class FaceView(val theContext : Context, val bounds : Rect) : View(theContext) {
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
val myPaint = Paint()
myPaint.color = Color.BLACK
myPaint.style = Paint.Style.STROKE
myPaint.strokeWidth = 10f
canvas?.drawRect(bounds, myPaint)
}
}
Tried to draw a rectangle to the bound we got from the face object ML kit created
val result = detector.process(image).addOnSuccessListener { faces ->
for (face in faces) {
val bounds = face.boundingBox
val view = FaceView(requireContext(), bounds)
binding.actionRoot.addView(view)
val lp : ConstraintLayout.LayoutParams =
ConstraintLayout.LayoutParams(bounds.width(),bounds.height())
lp.startToStart = binding.actionPhoto.id
lp.topToTop = binding.actionPhoto.id
lp.marginStart = bounds.right
lp.topMargin = bounds.bottom
view.layoutParams = lp
}}
Result :
How can we draw a rectangle for each face that we produced from URI(not from CameraX) and make them clickable?