How to start with ML in android – Part 2

How to start with ML in android – Part 2
November 25, 2018 No Comments Android Development,Development Pushpendra Kumar

In this chapter I am going to cover Landmark detection in image. Before coming to this chapter you have to go through with How to start with ML in android – Part 1. Because in that post I have already covered the basic integration of ML. Now I am going to tell you solution for how to detect the landmark.

Everything will be remain same as we have integrate in the last chapter, You have to integrate some more lines of code in FaceOverlayView class. So let’s create a new function drawFaceLandmarks(). Your function will look like the below code.

private fun drawFaceLandmarks(canvas: Canvas, scale: Double) {
        val paint = Paint()
        paint.color = Color.GREEN = Paint.Style.STROKE
        paint.strokeWidth = 5.0f

        for (i in 0 until mFaces?.size()!!) {
            val face = mFaces?.valueAt(i)

            if (face != null) {
                for (landmark in face.landmarks) {
                    val cx = (landmark.position.x * scale).toInt()
                    val cy = (landmark.position.y * scale).toInt()
                    canvas.drawCircle(cx.toFloat(), cy.toFloat(), 10.0f, paint)


This function will draw a circle on your landmark. And you have to call this function from override fun onDraw(canvas: Canvas). Officially you can detect 8 landmark on the face. According to these 8 landmark you can create any kind of face shape in your mobile application. And this process comes under the image processing.

Required* How To Start With ML In Android – Part 1

About The Author
Pushpendra Kumar I am passionate about mobile application development and professional developer at Colour Moon Technologies Pvt Ltd ( This website I have made so that I can meet with new challenges and can share here.

Leave a reply

Your email address will not be published. Required fields are marked *