Skip to content

pranaypatel512/MLKitTextRecognize

Repository files navigation

MLKitTextRecognize

App will:

Utilize the ML Kit Text Recognition API to detect text in images.

You'll learn:

How we can use Firebase ML Kit SDK in Android with Machine learning capability.

     private fun runTextRecognition() {
        val image = mSelectedImage?.let { FirebaseVisionImage.fromBitmap(it) }
        val detector = FirebaseVision.getInstance().visionTextDetector
        button_text.isEnabled = false
        image?.let {
            detector.detectInImage(it)
                    .addOnSuccessListener {
                        button_text.isEnabled = true
                        processTextRecognitionResult(it)
                    }
                    .addOnFailureListener {
                        button_text.isEnabled = true
                        showToast(it.localizedMessage)
                    }
        }


     }
  • Get texts elements result from FirebaseVisionText :
     private fun processTextRecognitionResult(firebaseVisionText: FirebaseVisionText?) {
        firebaseVisionText?.let {
            val blocks = firebaseVisionText.blocks
            when (blocks.size) {
                0 -> showToast("No texts found")
                else -> {
                    graphic_overlay.clear()
                    for (block in blocks.indices) {
                        val lines = blocks[block].lines
                        for (line in lines.indices) {
                            val elements = lines[line].elements
                            for (element in elements.indices) {
                                val textGraphic = TextGraphic(graphic_overlay, elements[element])
                                graphic_overlay.add(textGraphic)
                            }
                        }
                    }
                }
            }
        }
     }

Output:

Image One result Image Two result Image Three result

NOTE:

To successfully sync this project, need to set up this app into your Firebase project. Kindly follow below link to "Add Firebase to Your Android Project": https://firebase.google.com/docs/android/setup

References:

Built With

  • Android Studio - The Official IDE for Android
  • Kotlin - The Official Language for Android ❤️
  • Gradle - Build tool for Android Studio

Pull Requests

I welcome and encourage all pull requests to learn something new.

Find this project useful ? ❤️

Support it by clicking the ⭐️ button on the upper right of this page. ✌️

Images credits

https://unsplash.com/

About

Detect text using Firebase ML Kit Text Recognition API

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages