Image Recognition using ML Kit In Kotlin
Image Recognition using ML Kit In Kotlin

In Google I/O‘18 we saw the introduction of Firebase MLKit, a piece of the Firebase suite that tends to give our apps the power to support intelligent features without breaking a sweat. The SDK presently comes with a group of predefined capabilities that are commonly required in applications you will be able to implement these in your application regardless of whether you know machine learning or not.

Image Recognition: In The Context Of ML

The ability of software to distinguish objects, places, people, writing and actions in pictures.  PCs can utilize machine vision advancements, together with a camera and artificial intelligence software, to achieve image recognition. It’s accustomed perform a large number of machine-based visual tasks, like labeling the content of images with meta-tags.

Now, what Firebase ML Kit offers to us is already possible to implement yourself using various machine-learning technologies.

The thing with Firebase ML is that and also offering these abilities underneath a type of wrapper, it like these innovations and offers their abilities within a solitary SDK.

 

Image Recognition : In The Context Of ML
Image Recognition: In The Context Of ML

In light of these, it tends to be hard to know where to begin. This is one of the principal objectives of Firebase ML Kit to make Machine Learning to our Android and iOS applications more accessible to developers and available in more applications. Right now ML Kit offers the capacity to:

  • Image Labeling to classify common elements in pictures.

  • Text Recognition to process and recognize text from pictures.

  • Face Detection to help you know if a face is smiling, tilted, or frowning in pictures.

  • Barcode Scanning to read data encoded in standard barcode formats like QR Codes.

  • Landmark Recognition to identify popular places in images.

 

Present and Future Uses of Image Recognition

Organization of Pictures

Organization of Pictures
Organization of Pictures

 

A popular use of image recognition is the automated organization of photograph collections. Have you ever traveled to another nation and wound up with many pictures stored on your phone? Google Photos is an extraordinary case of such an application to store pictures. It helps you organize your photos in albums by identifying common places, objects, friends or even pets.

Image Labeling on Social Networks

Image Labeling on Social Networks
Image Labeling on Social Networks

You might be familiar with face recognition in Facebook and Instagram. Whenever a user uploads a photograph, Facebook promptly proposes tagging some of your friends. Other than the tagging feature, image recognition translates content for visually impaired people with screen readers. It additionally perceives improper or hostile pictures.

Since you have some background on the possible use cases for image recognition, it’s time to learn about On-Device and On-Cloud APIs in Firebase.

Which is better? On-Device or On-Cloud

On-Device APIs can process information rapidly without the requirement for an Internet connection. This is useful if you don’t want to consume the mobile data of your users and you require quick processing.

The principal disadvantage is the certainty of results given by ML. The confidence is a value showing how happy the ML algorithm is with the answer it provided.

On-Cloud APIs offer significantly more powerful processing capabilities because of Google Cloud Platform’s ML innovation, yet these APIs require an Internet connection with work. On account of utilizing the Google Cloud Platform, this requires an payment after the initial 1,000 requests.

Now let’s talk about how to implement image recognition in your current application.

Setting Up Firebase and ML Kit using Kotlin

ML Kit is a subset of Firebase and it is very to configure, you just need to add your app to Firebase service.

Now create new android project and give name it like ML Kit Demo and add below dependencies to your app level build.gradle file in the dependencies block:

implementation ‘com.google.firebase:firebase-core:16.0.1’
implementation
‘com.google.firebase:firebase-ml-vision:16.0.0’
implementation
‘com.google.firebase:firebase-ml-vision-image-label-model:15.0.0’

Sync your gradle file to ensure everything is working.

Now we are following one of the popular food model of ML Kit so we will create app in which we capture a photo and it will tell us whether it is food or not.

First, you need to set up the camera and for that you need to handle runtime permissions, storagelocation, file formats and much more. Fortunately, there are many Android libraries that can help you in handling all those complexities via an easier API.

We use one sdk named ParaCamera to configure runtime permissions. To use it, open your app level  build.gradle and add following dependency in dependencies section:

implementation ‘com.mindorks:paracamera:0.2.2’

Sync your files and open the MainActivity.kt file and add the following just above onCreate() method:

private lateinit var camera: Camera
private val PERMISSION_REQUEST_CODE = 1

Add the following code in the onCreate():

camera = Camera.Builder()
       .resetToCorrectOrientation(true)//1
       .setTakePhotoRequestCode(Camera.REQUEST_TAKE_PHOTO)//2
       .setDirectory(“pics”)//3
       .setName(“delicious_${System.currentTimeMillis()}”)//4
       .setImageFormat(Camera.IMAGE_JPEG)//5
       .setCompression(75)//6
       .build(this)

Taking each commented section in turn:

  • Rotates the camera bitmap to the correct orientation from meta data.

  • Sets the request code for your onActivityResult() method.

  • Sets the directory in which your pictures will be saved.

  • Sets the name of each picture taken according to the system time.

  • Sets the image format to JPEG.

  • Sets a compression rate of 75% to use less system resources.

Now, add  the takePicture() method as below:

fun takePicture(view: View) {
 if (!hasPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) ||
       !hasPermission(android.Manifest.permission.CAMERA)) {
     // If do not have permissions then request it
     requestPermissions()
   } else {
     // else all permissions granted, go ahead and take a picture using camera
     try {
       camera.takePicture()
     } catch (e: Exception) {
       // Show a toast for exception
       Toast.makeText(this.applicationContext, getString(R.string.error_taking_picture),
           Toast.LENGTH_SHORT).show()
     }
   }
}

Next you need to take permission so add requestPermissions() method:

private fun requestPermissions(){
 if (ActivityCompat.shouldShowRequestPermissionRationale(this,
           android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {     mainLayout.snack(getString(R.string.permission_message), Snackbar.LENGTH_INDEFINITE) {
       action(getString(R.string.OK)) {
         ActivityCompat.requestPermissions(this,
             arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
                 android.Manifest.permission.CAMERA), PERMISSION_REQUEST_CODE)
       }
     }
   } else {
     ActivityCompat.requestPermissions(this,
         arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
             android.Manifest.permission.CAMERA), PERMISSION_REQUEST_CODE)
     return
   }
}

requestPermissions() asks for the CAMERA and WRITE_EXTERNAL_STORAGE permissions. If the user rejected those permissions previously, it will display a nice snack bar with a message.

Now, implement onRequestPermissionsResult() method,

override fun onRequestPermissionsResult(requestCode: Int,
   permissions: Array<String>, grantResults: IntArray) {
 when (requestCode) {
   PERMISSION_REQUEST_CODE -> {
     // If request is cancelled, the result arrays are empty.
     if (grantResults.isNotEmpty()
         && grantResults[0] == PackageManager.PERMISSION_GRANTED
         && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
       try {
         camera.takePicture()
       } catch (e: Exception) {
         Toast.makeText(this.applicationContext, getString(R.string.error_taking_picture),
             Toast.LENGTH_SHORT).show()
       }
     }
     return
   }
 }
}

Above code calls the method takePicture() when user granted all the permission to use camera. Now you need to handle activity result in onActivityResult() and set image in your image view,

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
 super.onActivityResult(requestCode, resultCode, data) if (resultCode == Activity.RESULT_OK) {
   if (requestCode == Camera.REQUEST_TAKE_PHOTO) {
     val bitmap = camera.cameraBitmap
     if (bitmap != null) {
       imageView.setImageBitmap(bitmap)
       detectDeliciousFoodOnDevice(bitmap)
     } else {
       Toast.makeText(this.applicationContext, getString(R.string.picture_not_taken),
           Toast.LENGTH_SHORT).show()
     }
   }
 }
}

That’s all, now build and run the app and you will see:

ML Kit for Image Recognition
ML Kit for Image Recognition

Hurray, your app is a toast detecting marvel!

You may like to read:  How Kotlin changed App Development Paradigm.

How Let’s Nurture helps for building Apps using ML Kit for Image Recognition?

Let’s Nurture, a top mobile app development company in India has got the expertise in providing many solutions based applications with E-Commerce and Social Networking. Our team Let’s Nurture provides best solutions for business (saving time), shopping experience, teaching experience and many more. If you want to know more about this technology in your existing mobile app, Get a free consultation from our experts at Let’s Nurture.

 

 

Want to work with us? We're hiring!