6

How to Implement the Automatic Bill Number Input Function Using HUAWEI ML Kit’s...

 3 years ago
source link: https://dzone.com/articles/how-to-implement-the-automatic-bill-number-input-f
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to Implement the Automatic Bill Number Input Function Using HUAWEI ML Kit’s Text Recognition

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.

Dec. 17, 20 · AI Zone · Tutorial

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.

14207600-26400650000004314082020090302334563733736665065437.png

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

14207603-26400650000004314082020090302361433939449391839226.jpg

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:

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

1.3: Configure SDK dependencies in the app-level build.gradle file.

xxxxxxxxxx
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.

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

1.5: Apply for the camera permission.

xxxxxxxxxx
<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.

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

2.2: Set the recognition result processor to bind with the analyzer.

xxxxxxxxxx
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.

xxxxxxxxxx
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.

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

2.5: Process the recognition result as required.

xxxxxxxxxx
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.

xxxxxxxxxx
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.

14207608-26400650000004314082020090306292400055571368771848.gif

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

14207609-26400650000004314082020090306295954161025855628621.gif

The GitHub source code can be found here.

Topics:
ai, tutorial, text recognition, natural language processing

Opinions expressed by DZone contributors are their own.


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK