Text Recognition with Huawei ML Kit

Oğuzhan Demirci
Huawei Developers
Published in
4 min readJul 22, 2020

Huawei ML Kit continues giving our apps the power of AI with 16 different functions currently, covering many scenarios from text related, language-related, image-related and body-related function groups. ML Kit has a rich function list guaranteeing high concurrency and reliability. One of these functions that Huawei ML Kit offers us is “Text Recognition”.

As the name implies text recognition service extracts text from images of cards, receipts, paper documents, labels etc. This service is widely used in travel apps, chat/communication apps, translation/language learning apps, office apps and so on.

Text Recognition service recognizes text in images of various sources, such as static images from our device, from a URL or frames from a camera stream. It is completely up to us to choose among them according to our needs. The service runs on all Huawei devices above EMUI 5.0.

Huawei ML Kit can run both on-device and on the cloud. It can perform recognition of the latin based characters listed here, Simplified Chinese, Japanese and Korean. While running on the cloud, the service can recognize text in languages such as English, Spanish, Portuguese, Italian, German, French, Russian, Simplified Chinese, Japanese, Korean, Polish, Finnish, Norwegian, Swedish, Danish, Turkish, Thai, Arabic, Hindi, and Indonesian.

In order to have a deeper understanding about text recognition, we are going to build a sample Android application which receives images from device storage via intent, displays it on an ImageView, recognizes text in the selected image (if exists any) and writes this text in a TextView. Our current example will cover only recognizing text in static images. You can also visit Huawei Developer ML Kit for more detailed information.

Let’s start!

  1. Create and sign your project in AppGalleryConnect as described here.
  2. In HUAWEI Developer AppGallery Connect, go to Develop > Manage APIs. Make sure ML Kit is activated.

3. Add related dependencies in your build.gradle. Here is how our app-level build.gradle looks like.

Our project-level build.gradle file is also important. Here it is.

4. What we want to achieve is to get an image from device storage, display and analyze it, write the extracted text in a TextView. Therefore our layout will be simple. There will be an ImageView, a Button and a TextView. Here is our activity_main.xml.

5. Now get images with an implicit intent using getImage method. ButtonAddImage’s OnClickListener triggers getImage method and the result falls into onActivityResult. In onActivityResult we receive data as Uri and get our bitmap to be analyzed here using this uri as shown below.

6. We got our bitmap and set it on our ImageView. After that we can create MLTextAnalyzer. MLTextAnalyzer can be created with custom settings or with default settings and also you can make local/remote selection. Here we have a local MLTextAnalyzer with custom settings. We can call this method in onCreate.

7. We can analyze an image with MLTextAnalyzer in two ways: synchronously and asynchronously. In this example we used asynchronous mode. asnycAnalyzeText method takes in Bitmap, converts Bitmap to MLFrame, MLTextAnalyzer consumes this MLFrame and produces an output.

8. When we no longer need our MLTextAnalyzer, we should stop it to release recognition resources. I did this in onDestroy method.

9. Let’s bring the pieces together. Here is the whole code of MainActivity. We used our asyncAnalyzeText method right after we got the image in onActivityResult.

10. We came to the end of implementation part. Let’s test our app with a few pictures.

11. It works, great! Hope you have finished your project without errors, too. In this article we achieved recognition of text in static images. We can also recognize text from images on the cloud and from camera frames. There are many other functions Huawei ML Kit offers to developers. We will cover them all one by one. If you have any questions, please ask through the link below. Happy coding!

--

--