How to use HMS ML Kit — Document Skew Correction Feature ?

İsmail Oğuzhan Ay
Huawei Developers
Published in
4 min readNov 30, 2020

--

Hello everyone, today we will talk about document skew correction feature of ML Kit. HMS grows up everyday and it comes lots of new feature about the ML Kit.

What is the Document Skew Correction ?

The document skew correction service can automatically identify the location of a document in an image and adjust the shooting angle to the angle facing the document, even if the document is tilted. In addition, you can perform document skew correction at custom boundary points.

  • This service is widely used in daily life. If a paper document needs to be backed up in an electronic version but the document in the image is tilted, you can use this service to correct the document position.
  • Similarly, when recording a card, you can take a photo of the front side of the card without directly facing the card.
  • In addition, when the road signs on both sides of the road cannot be accurately identified due to the tilted position during the trip, you can use this service to take photos of the front side of the road signs to facilitate travel.

Before the starting implementation, some important points

  • To achieve better effects, ensure that the camera faces the document, that the document occupies most of the image, and that the boundaries of the document are in the viewfinder.
  • In addition, the best correction effect is achieved when the shooting angle is within 30 degrees. If the shooting angle is greater than 30 degrees, the document boundaries must be clear enough to ensure better effects.
  • JPG, JPEG, and PNG images are supported.
  • It is recommended that the image size be within the range of 320 x 320 px to 1920 x 1920 px.

Development Process

We need to follow some steps for the integration of Huawei ML Kit.

  1. We need to register as a developer account in AppGallery Connect.
  2. We create an application and enable ML Kit from AppGallery Connect.
  3. After the configuration in AppGallery Connect, let’s integrate the Huawei ML Kit into our demo application.

a. We need to get agconnect-services.json file for configurations from AppGallery Connect. Then, we add it into our application project level under the app folder.

b. After that, we need to add dependencies into gradle files.

Lastly, we add the metadata into AndroidManifest.xml for document skew correction.

Let’s start the coding !

We have an activity which name is DocumentSkewCorrectionActivity. It’s layout has an button for getting document image from device storage. It sends the image to document skew correction sdk for more correct position of our document image. Let’s check it.

We create a button which is for selecting document image from storage. Firstly, we check required permissions. Then, we start the event with the button. For example, we select a use case with select an image from storage. After the getting required permissions, we called startActivityForResult() method. In onActivityResult() method, we get the image from storage as a bitmap, then we called performDocumentSkewCorrection() method with bitmap. We should need detect the points then correct that points for corrected image.

At the performDocumentSkewCorrection method, we set the settings with MLDocumentSkewCorrectionAnalyzerSetting(). After the set the settings, we create the analyzer with settings value. Then, we set the frame with bitmap. We create a task for document detection MLDocumentSkewDetectResult. The task result has the positions of document image. When we got the positions, we create a mutable list for coordinates. After that, we create a MLDocumentSkewCorrectionCoordinateInput object with point coordinates which name is coordinateData. So, we detect the points. Now, we send the points for detection. We create a new task for document correction MLDocumentSkewCorrectionResult. We have listener functions for success and failure. In addOnSuccessListener, we set the document image with corrected document.

Finally, we completed demo application about the usage of document skew correction with Huawei ML Kit. I hope this article will be useful for implementation and use case usages.

Thank You ! ..

For more details, please check these useful links :

--

--