How to start with ML in android – Part 1

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

Let’s begin with the ML, Here you will learn how to work with machine learning. And in my android application tutorial I am taking base is Firebase ML Kit. If you want to know more about machine learning then you can click here. So in a very simple way let’s start with the following steps. And keep in mind this tutorial is based on Kotlin language.

  1. Create a new project
  2. Start with empty activity
  3. Follow the steps with Kotlin language

Lets suppose you have done with your project integration now you have to do some work with firebase, because we are going to use Firebase ML Kit for the machine learning integration. So again follow few steps.

  1. Click on the given link Firebase
  2. If you have already project on firebase then skip this process
  3. Click on Add Project and follow the steps which is given by firebase for setup the firebase on your android application.


In the overall Machine Learning Tutorial we are going to cover the following things

  1. Face detection
  2. Landmark detection
  3. Text recognition (OCR)
  4. Image labeling
  5. Barcode scanning


How to get started with Android To get started with ML Kit just need to add the library in gradle and you are ready to go.

Face detection

Include the dependency first in your app Gradle file

implementation ''

And add this line into androidManifest

Now you have successful done with first step integration with the Firebase ML Kit.

Create New Kotline class with FaceOverlayView name. We are going to cover the face detection.

class FaceOverlayView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : View(context, attrs, defStyleAttr) {
    private var mBitmap: Bitmap? = null
    private var mFaces: SparseArray? = null

    fun setBitmap(bitmap: Bitmap) {
        mBitmap = bitmap

        val detector = FaceDetector.Builder(context)

        if (!detector.isOperational) {
        } else {
            val frame = Frame.Builder().setBitmap(bitmap).build()
            mFaces = detector.detect(frame)

    override fun onDraw(canvas: Canvas) {

        if (mBitmap != null && mFaces != null) {
            val scale = drawBitmap(canvas)
            drawFaceBox(canvas, scale)

    private fun drawBitmap(canvas: Canvas): Double {
        val viewWidth = canvas.width.toDouble()
        val viewHeight = canvas.height.toDouble()
        val imageWidth = mBitmap?.width
        val imageHeight = mBitmap?.height
        val scale = Math.min(viewWidth / imageWidth!!, viewHeight / imageHeight!!)

        val destBounds = Rect(0, 0, (imageWidth * scale).toInt(), (imageHeight * scale).toInt())
        canvas.drawBitmap(mBitmap, null, destBounds, null)
        return scale

    private fun drawFaceBox(canvas: Canvas, scale: Double) {
        //paint should be defined as a member variable rather than
        //being created on each onDraw request, but left here for
        val paint = Paint()
        paint.color = Color.GREEN = Paint.Style.STROKE
        paint.strokeWidth = 5.0f

        var left = 0f
        var top = 0f
        var right = 0f
        var bottom = 0f

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

            if (face != null) {
                left = (face.position.x * scale).toFloat()
            if (face != null) {
                top = (face.position.y * scale).toFloat()
            if (face != null) {
                right = scale.toFloat() * (face.position.x + face.width)
            if (face != null) {
                bottom = scale.toFloat() * (face.position.y + face.height)

            canvas.drawRect(left, top, right, bottom, paint)

In next step you need to import some package above the class which is as –

import android.content.Context
import android.util.SparseArray
import android.util.AttributeSet
import android.view.View
import android.widget.Toast

Now we I am going to define the XML of MainActivity.

Upto now you have done very well!. Now let’s come back to the MainActivity and keep in mind the MainActivity must have AppCompatActivity. Write some code of line in onCreate(savedInstanceState: Bundle?).

val faceImage = findViewById(;
        val stream = resources.openRawResource(R.raw.mans_im)
        val bitmap = BitmapFactory.decodeStream(stream)

So that’s great you have done with the face detection, and I hope this is your best experiment. Now you can continue with upcoming tutorial.

Good Luck 🙂 Happy Coding

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.