Machine Learning Made Easy: Text Recognition Using Kotlin, MVVM, and Huawei ML Kit
Anywhere repetitive manual reading is done, we can replace it with text recognition.
Join the DZone community and get the full member experience.Join For Free
Text recognition basically takes help from the concept of OCR.
OCR (optical character recognition) basically reads an image character by character and matches the current character with its previously stored data. It behaves exactly like a human reads.
Anywhere repetitive manual reading is done, we can replace it with text recognition. This includes e-commerce, learning, logistics, and a lot more.
Now, let’s discuss the Huawei Text Recognition Service.
- Text is read from camera stream or static image.
- Image is sent to machine learning model.
- Model analyzes this image via OCR and sends a String response.
You can recognize text in both a static image or dynamic camera stream.
You can even call these APIs synchronously or asynchronously as per your application requirement.
You can use this service on a device, i.e. a small machine learning algorithm that can be added to your application, and it will work perfectly fine.
You can use this service on the cloud, i.e. once we fetch our image, we can transmit that image onto the cloud and can get even better accuracy and result within milliseconds.
Below is the list of languages supported by ML Kit:
- Simplified Chinese
- Latin-based languages
Step 1: Create a new project in Android Studio.
Step 2: Choose a dependency as per your project requirements.
If you want a lite version, use the below dependency:
Step 3: Automatically update the machine learning model:
Step 4: Add the below permissions in the manifest file:
I won’t be covering the part where how we can get image form device via camera or gallery.
Let’s jump into the
TextRecognitionViewModel class, where we have received a bitmap that contains the user image.
Below is the code you can use to call the text recognition API and get the String response:
- I wanted to use cloud services, so I choose
- As per density of characters, we can set
- Once density is set, we will set text language by
- We can pass a collection object of
ArrayList<String>to it. I have added five languages to my model but you can add languages as per the need.
MLRemoteTextSetting.ARC: Return the vertices of a polygon border in an arc format.
- Now, our custom
MLRemoteTextSettingobject is ready and we can pass this to
Next step is to create an MLFrame using the below code and provide your previously fetched image in bitmap format.
On the analyser object, we will be calling
asyncAnalyseFrame(frame) and providing
MLFrame which we recently created.
This will yield you a
Task<MLText> object, on this object you will get two callbacks.
You can save the new resource from
onSuccess() and stop the analyzer to release detection resources with the
If you want to use the on device model, only the below changes are required.
I hope you liked this article. I would love to hear your ideas on how you use this kit in your applications.
Opinions expressed by DZone contributors are their own.