Recognizing Text With Firebase ML Kit on iOS and Android

A practical guide to implementing text recognition with Firebase’s ML Kit

Bapusaheb Patil
Apr 19, 2019 · 4 min read
Image for post
Image for post
On-device APIs for text recognition and barcode scanning; Cloud-based APIs for image labeling

iOS

Step 1: Include the pods

pod 'Firebase/Core'# On-device API
pod 'Firebase/MLVisionTextModel'
# Cloud-based API
pod 'Firebase/MLVision'

Step 2: Import Firebase

import Firebase

Step 3: Get an instance of Vision

let vision = Vision.vision()

Step 4: Get an instance of the text recognizer

// On-device API
let recognizer = vision.onDeviceTextRecognizer()
// Cloud-based API
let recognizer = vision.cloudTextRecognizer()

Step 5 (optional): Configure your text recognizer to detect certain languages

let options = VisionCloudDocumentTextRecognizerOptions()// Setting languages to English, French & Hindi
options.languageHints = ["en", "fr", "hi"]
// Create your text recognizer with the above options
let recognizer = vision.cloudDocumentTextRecognizer(options: options)

Step 6: Get your image as a VisionImage

let visionImage = VisionImage(image: UIImage)

Step 7: Process your VisionImage with your text recognizer


Android

Step 1: Add Firebase ML Vision as a dependency

implementation 'com.google.firebase:firebase-ml-vision:19.0.3'

Step 2: Auto-download the text recognition ML model

<application ...>
...
<meta-data
android:name="com.google.firebase.ml.vision.DEPENDENCIES"
android:value="ocr" />
</application>

Step 3: Get an instance of the text recognizer

// On-device API
val recognizer = FirebaseVision.getInstance().onDeviceTextRecognizer
// Cloud-based API
val recognizer = FirebaseVision.getInstance().cloudTextRecognizer

Step 4 (optional): Configure your text recognizer to detect certain languages

// Setting languages to English, French & Hindi
val options = FirebaseVisionCloudTextRecognizerOptions.Builder()
.setLanguageHints(Arrays.asList("en", "fr", "hi"))
.build()
// Create your text recognizer with the above options
val recognizer = FirebaseVision.getInstance().getCloudTextRecognizer(options)

Step 5: Get your image as a FirebaseVisionImage

val image = FirebaseVisionImage.fromBitmap(bitmap)

Step 6: Process your FirebaseVisionImage with your text recognizer


Better Programming

Advice for programmers.

Bapusaheb Patil

Written by

I tell stories, design things & build stuff. Storyteller. PSM I. IDF Certified UX Designer. Google Certified Dev. I work here: www.softway.com

Better Programming

Advice for programmers.

Bapusaheb Patil

Written by

I tell stories, design things & build stuff. Storyteller. PSM I. IDF Certified UX Designer. Google Certified Dev. I work here: www.softway.com

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight.

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox.

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store