[Huawei Developers] Freemind R:
More information like this, you can visit HUAWEI Developer Forum​

Introduction

In the previous post, we looked at how to use HUAWEI ML Kit’s card recognition function to implement the card binding function. With this function, users only need to provide a photo of their card, and your app will automatically recognize all of the key information. That makes entering card information much easier, but can we do the same thing for bills or discount coupons? Of course we can! In this post, I will show you how to implement automatic input of bill numbers and discount codes using HUAWEI ML Kit’s text recognition function.

Application

Text recognition is useful in a huge range of situations. For example, if you scan the following bill, indicate that the bill service number starts with “NO.DE SERVICIO”, and limit the length to 12 characters, you can quickly get the bill service number “123456789123” using the text recognition.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20200903023345.63733736665065437270890671309953:50510921063722:2800:9DC13C32D2AB5908AAF9EB1359A95E524280E03C4E5641FCA935829C7B6778ED.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20200903023345.63733736665065437270890671309953:50510921063722:2800:9DC13C32D2AB5908AAF9EB1359A95E524280E03C4E5641FCA935829C7B6778ED.png[/IMG]

Similarly, if you scan the discount coupon below, start the discount code with “FAVE-” and limit the length to 4 characters, you’ll get the discount code “8329”, and can then complete the payment.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20200903023614.33939449391839226756113725493708:50510921063722:2800:1A6EC6E31C56F1ADACE0773A89721F10ADEE91F5545C3A27B87CB21E6DB91E27.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20200903023614.33939449391839226756113725493708:50510921063722:2800:1A6EC6E31C56F1ADACE0773A89721F10ADEE91F5545C3A27B87CB21E6DB91E27.jpg[/IMG]

Pretty useful, right? You can also customize the information you want your app to recognize.

Integrating Text Recognition

So, let’s look at how to process bill numbers and discount codes.

1. Preparations

You can find detailed information about the preparations you need to make on the HUAWEI Developer.

Here, we’ll just look at the most important procedures.

1.1 Configure the Maven Repository Address in the Project-Level build.gradle File

buildscript {
repositories {
...
maven {url 'https://developer.huawei.com/repo/'}
}
}
dependencies {
...
classpath 'com.huawei.agconnect:agcp:1.3.1.300'
}
allprojects {
repositories {
...
maven {url 'https://developer.huawei.com/repo/'}
}
}

1.2 Add Configurations to the File Header

Once you’ve integrated the SDK, add the following configuration to the file header:

apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'

1.3 Configure SDK Dependencies in the App-Level build.gradle File

dependencies {
// Import the base SDK.
implementation 'com.huawei.hms:ml-computer-vision-ocr:2.0.1.300'
// Import the Latin character recognition model package.
implementation 'com.huawei.hms:ml-computer-vision-ocr-latin-model:2.0.1.300'
// Import the Japanese and Korean character recognition model package.
implementation 'com.huawei.hms:ml-computer-vision-ocr-jk-model:2.0.1.300'
// Import the Chinese and English character recognition model package.
implementation 'com.huawei.hms:ml-computer-vision-ocr-cn-model:2.0.1.300'
}

1.4 Add these Statements to the AndroidManifest.xml File so the Machine Learning Model can Automatically Update

<manifest>
...
<meta-data
android:name="com.huawei.hms.ml.DEPENDENCY"
android:value="ocr" />
...
</manifest>

1.5 Apply for the Camera Permission

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

2. Code Development

2.1 Create an Analyzer

MLTextAnalyzer analyzer = new MLTextAnalyzer.Factory(context).setLanguage(type).create();

2.2 Set the Recognition Result Processor to Bind with the Analyzer

analyzer.setTransactor(new OcrDetectorProcessor());

2.3 Call the Synchronous API

Use the built-in LensEngine of the SDK to create an object, register the analyzer, and initialize camera parameters.

lensEngine = new LensEngine.Creator(context, analyzer)
.setLensType(LensEngine.BACK_LENS)
.applyDisplayDimension(width, height)
.applyFps(30.0f)
.enableAutomaticFocus(true)
.create();

2.4 Call the run Method to Start the Camera and Read the Camera Streams for the Recognition

try {
lensEngine.run(holder);
} catch (IOException e) {
// Exception handling logic.
Log.e("TAG", "e=" + e.getMessage());
}

2.5 Process the Recognition Result As Required

public class OcrDetectorProcessor implements MLAnalyzer.MLTransactor<MLText.Block> {
@Override
public void transactResult(MLAnalyzer.Result<MLText.Block> results) {
SparseArray<MLText.Block> items = results.getAnalyseList();
// Process the recognition result as required. Only the detection results are processed.
// Other detection-related APIs provided by ML Kit cannot be called.
…
}
@Override
public void destroy() {
// Callback method used to release resources when the detection ends.
}
}

2.6 Stop the Analyzer and Release the Detection Resources When the Detection Ends

if (analyzer != null) {
try {
analyzer.stop();
} catch (IOException e) {
// Exception handling.
}
}
if (lensEngine != null) {
lensEngine.release();
}

Demo Effect

And that’s it! Remember that you can expand this capability if you need to. Now, let’s look at how to scan travel bills.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20200903062924.00055571368771848276411178488495:50510921063722:2800:155BA651B0FBBECFD49183A11EAA3CEC1E31A4DDFACF1D4131C34C48C4C308A0.gif"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20200903062924.00055571368771848276411178488495:50510921063722:2800:155BA651B0FBBECFD49183A11EAA3CEC1E31A4DDFACF1D4131C34C48C4C308A0.gif[/IMG]

And here’s how to scan discount codes to quickly obtain online discounts and complete payments.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20200903062959.54161025855628621098506325985672:50510921063722:2800:708DC64B495F5A147CEB20AD0A8AE339256C3F4A78E71D0EB81F46D96D07BA50.gif"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20200903062959.54161025855628621098506325985672:50510921063722:2800:708DC64B495F5A147CEB20AD0A8AE339256C3F4A78E71D0EB81F46D96D07BA50.gif[/IMG]

Github Source Code

https://github.com/HMS-Core/hms-ml-demo/tree/master/Receipt-Text-Recognition