[Huawei Developers] Freemind R:
Intro

Questionnaires are useful when you want to collect specific information for the purposes of market research. But how can you convert the large amounts of data you collect from questionnaires into electronic documents? One very effective tool is ML Kit's form recognition service. This guide will show you how to integrate this service, so you can easily input and convert data from forms.

Applicable Scenarios

ML Kit's form recognition service uses AI to recognize the images you input and return information about a form’s structure (including rows, columns, and coordinates of cells) and form text in both Chinese and English (including punctuation). This service can be widely applied in everyday work scenarios. For example, if you’ve collected a lot of paper questionnaires, you can quickly convert them into electronic documents. This is cheaper and requires less time and effort than typing them up manually.

Precautions

· Forms such as questionnaires can be recognized.

· Images containing more than one form cannot be recognized, and the form header and footer information cannot be obtained.

· For the best results, try to adhere to the following conditions:

Development Procedure

1. Preparations

To find detailed information about the preparations you need to make, please refer to Development Process.

Here, we'll just look at the most important steps.

1.1 Configure the Maven repository address in the project-level build.gradle file.

buildscript {

repositories {

google()

jcenter()

maven {url 'https://developer.huawei.com/repo/'}

}

dependencies {

...

classpath 'com.huawei.agconnect:agcp:1.4.1.300'

}

}

allprojects {

repositories {

google()

jcenter()

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-formrecognition:2.0.4.300'

// Import the form recognition model package.

implementation 'com.huawei.hms:ml-computer-vision-formrecognition-model:2.0.4.300'

}

1.4 Add the following statements to the AndroidManifest.xml file so the machine learning model can update automatically:

<meta-data

android:name="com.huawei.hms.ml.DEPENDENCY"

android:value= "fr"/>

1.5 Apply for camera permissions.

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

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

2. Code Development

2.1 Create a form recognition analyzer.

MLFormRecognitionAnalyzerSetting setting = new MLFormRecognitionAnalyzerSetting.Factory().create();

MLFormRecognitionAnalyzer analyzer = MLFormRecognitionAnalyzerFactory.getInstance().getFormRecognitionAnalyzer(setting);

2.2 Create an MLFrame object by using android.graphics.Bitmap which will enable the analyzer to recognize forms. Only JPG, JPEG, and PNG images are supported. We recommend that the image size be within a range of 960 x 960 px to 1920 x 1920 px.

MLFrame mlFrame = MLFrame.fromBitmap(bitmap);

2.3 Call the asynchronous method asyncAnalyseFrame or the synchronous method analyseFrame to start the form recognition. (For details about the data structure definition of JsonObject, please refer to JsonObject Data Structure Definition.)

// Call the asynchronous method asyncAnalyseFrame.

Task<JsonObject> recognizeTask = analyzer.asyncAnalyseFrame(mlFrame);

recognizeTask.addOnSuccessListener(new OnSuccessListener<JsonObject>() {

@Override

public void onSuccess(JsonObject recognizeResult) {

// Recognition success.

}

}).addOnFailureListener(new OnFailureListener() {

@Override

public void onFailure(Exception e) {

// Recognition failure.

}

});

// Call the synchronous method analyseFrame.

SparseArray<JsonObject> recognizeResult = analyzer.analyseFrame(mlFrame);

if (recognizeResult != null && recognizeResult.get(0).get("retCode").getAsInt() == MLFormRecognitionConstant.SUCCESS) {

// Recognition success.

} else {

// Recognition failure.

}

2.4 Stop the analyzer and release the recognition resources when the recognition finishes.

if (analyzer != null) {

analyzer.stop();

}

Summary

ML Kit's form recognition service enables you to recognize forms in images. It’s particularly useful for tasks like collecting questionnaire data because it is quicker, cheaper, and requires less effort than typing up questionnaires manually.

Learn More

For more information, please visit HUAWEI Developers.

For detailed instructions, please visit Development Guide.

You can join the HMS Core developer discussion on Reddit.

You can download the demo and sample code from GitHub.

To solve integration problems, please go to Stack Overflow.