[Huawei Developers] Freemind R:
Introduction

In the previous post, we looked at how to use HUAWEI ML Kit’s text recognition capability. With this capability, users just need to provide an image, and your app will automatically recognize all of the key information. But what if the image is skew? Can we still get all the same key information? Yes, we can! ML Kit’s document skew correction capability automatically recognizes the position of the document, corrects the shooting angle, and lets users customize the edge points. So even if the document is skew, your users can still get all of the information they need.

[IMG="https://miro.medium.com/max/700/0*RBJX6_KekqFStSUr.jpg"]https://miro.medium.com/max/700/0*RBJX6_KekqFStSUr.jpg[/IMG]

Application Scenarios

Document skew correction is useful in a huge range of situations. For example, if your camera is at an angle when you shoot a paper document, the resulting image may be difficult to read. You can use document skew correction to adjust the document to the correct angle, which makes it easier to read.

[IMG="https://miro.medium.com/max/700/0*1wNi7zarEZBiBGRk.jpg"]https://miro.medium.com/max/700/0*1wNi7zarEZBiBGRk.jpg[/IMG]

Document skew correction also works for cards.

[IMG="https://miro.medium.com/max/700/0*u4OlXswAL5MKxNGk.jpg"]https://miro.medium.com/max/700/0*u4OlXswAL5MKxNGk.jpg[/IMG]

And if you’re traveling, you can use it to straighten out pictures of road signs that you’ve taken from an angle.

[IMG="https://miro.medium.com/max/700/0*ZWHigTGGh71z2Rj8.jpg"]https://miro.medium.com/max/700/0*ZWHigTGGh71z2Rj8.jpg[/IMG]

Isn’t it convenient? Now, I will show you how to quickly integrate this capability.

Document Correction Development

1. Preparations

You can find detailed information about the preparations you need to make on the HUAWEI Developers-Development Process.

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

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 Configure SDK Dependencies in the App-Level build.gradle File

dependencies{
// Import the base SDK.
implementation 'com.huawei.hms:ml-computer-vision-documentskew:2.0.2.300'
// Import the document detection/correction model package.
implementation 'com.huawei.hms:ml-computer-vision-documentskew-model:2.0.2.300'
}

1.3 Add Configurations to the File Header

apply plugin: ‘com.huawei.agconnect’
apply plugin: ‘com.android.application’

1.4 Add these 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= "dsc"/>

1.5 Apply for Camera Permission and Local Image Reading Permission

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

2 Code Development

2.1 Create a Text Box Detection/Correction Analyzer

MLDocumentSkewCorrectionAnalyzerSetting setting = new MLDocumentSkewCorrectionAnalyzerSetting.Factory().create();
MLDocumentSkewCorrectionAnalyzer analyzer = MLDocumentSkewCorrectionAnalyzerFactory.getInstance().getDocumentSkewCorrectionAnalyzer(setting);

2.2 Create an MLFrame Object by Using android.graphics.Bitmap for the Analyzer to Detect Images

JPG, JPEG, and PNG images are supported. It is recommended that you limit the image size to between 320 x 320 px and 1920 x 1920 px.

MLFrame frame = MLFrame.fromBitmap(bitmap);

2.3 Call the asyncDocumentSkewDetect Asynchronous Method or analyseFrame Synchronous Method to Detect the Text Box.

When the return code is MLDocumentSkewCorrectionConstant.SUCCESS, the coordinates of the four verticals of the text box are returned. These coordinates are relative to the coordinates of the input image. If the coordinates are inconsistent with those of the device, you need to convert them. Otherwise, the returned data will be meaningless.

// Call the asyncDocumentSkewDetect asynchronous method.
Task<MLDocumentSkewDetectResult> detectTask = analyzer.asyncDocumentSkewDetect(mlFrame);
detectTask.addOnSuccessListener(new OnSuccessListener<MLDocumentSkewDetectResult>() {
@Override
public void onSuccess(MLDocumentSkewDetectResult detectResult) {
// Detection success.
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
// Detection failure.
}
})
// Call the analyseFrame synchronous method.
SparseArray<MLDocumentSkewDetectResult> detect = analyzer.analyseFrame(mlFrame);
if (detect != null && detect.get(0).getResultCode() == MLDocumentSkewCorrectionConstant.SUCCESS) {
// Detection success.
} else {
// Detection failure.
}

2.4 Obtain the Coordinate Data of the Four Verticals in the Text Box Once the Detection is Successful

Use the upper left vertex as the starting point, and add the upper left vertex, upper right vertex, lower right vertex, and lower left vertex to the list (List<Point>). Finally, create an MLDocumentSkewCorrectionCoordinateInput object.

If the synchronous method analyseFrame is called, the detection results will be obtained first, as you can see in the following figure. (If the asynchronous method asyncDocumentSkewDetect is called, skip this step.)

MLDocumentSkewDetectResult detectResult = detect.get(0)；

Obtain the coordinate data for the four verticals of the text box and create an MLDocumentSkewCorrectionCoordinateInput object.

Point leftTop = detectResult.getLeftTopPosition();
Point rightTop = detectResult.getRightTopPosition();
Point leftBottom = detectResult.getLeftBottomPosition();
Point rightBottom = detectResult.getRightBottomPosition();
List<Point> coordinates = new ArrayList<>();
coordinates.add(leftTop);
coordinates.add(rightTop);
coordinates.add(rightBottom);
coordinates.add(leftBottom);
MLDocumentSkewCorrectionCoordinateInput coordinateData = new MLDocumentSkewCorrectionCoordinateInput(coordinates);

2.5 Call the asyncDocumentSkewCorrect Asynchronous Method or syncDocumentSkewCorrect Synchronous Method to Correct the Text Box

// Call the asyncDocumentSkewCorrect asynchronous method.
Task<MLDocumentSkewCorrectionResult> correctionTask = analyzer.asyncDocumentSkewCorrect(mlFrame, coordinateData);
correctionTask.addOnSuccessListener(new OnSuccessListener<MLDocumentSkewCorrectionResult>() {
@Override
public void onSuccess(MLDocumentSkewCorrectionResult refineResult) {
// Detection success.
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
// Detection failure.
}
});
// Call the syncDocumentSkewCorrect synchronous method.
SparseArray<MLDocumentSkewCorrectionResult> correct= analyzer.syncDocumentSkewCorrect(mlFrame, coordinateData);
if (correct != null && correct.get(0).getResultCode() == MLDocumentSkewCorrectionConstant.SUCCESS) {
// Correction success.
} else {
// Correction failure.
}

2.6 Stop the Analyzer to Release Detection Resources Once the Detection is Complete

if (analyzer != null) {
analyzer.stop();
}