How to straighten a document image with Java using deep learning

Digitizing documents is crucial in today’s world, but achieving quality results can be tedious. Today, we will be looking at how to straighten out an image of a document so that it both looks more professional, and also functions better with optical character recognition (OCR). Let’s jump right in.

Our initial step will be to set up our library through Jitpack. So, add these two snippets to your Maven POM file:

Repository

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

Dependency

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v2.75</version>
</dependency>
</dependencies>

With that done, we can call preprocessingUnrotateAdvanced:

// Import classes://import com.cloudmersive.client.invoker.ApiClient;//import com.cloudmersive.client.invoker.ApiException;//import com.cloudmersive.client.invoker.Configuration;//import com.cloudmersive.client.invoker.auth.*;//import com.cloudmersive.client.PreprocessingApi;ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: ApikeyApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");Apikey.setApiKey("YOUR API KEY");// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.setApiKeyPrefix("Token");PreprocessingApi apiInstance = new PreprocessingApi();File imageFile = new File("/path/to/file"); // File | Image file to perform OCR on.  Common file formats such as PNG, JPEG are supported.try {byte[] result = apiInstance.preprocessingUnrotateAdvanced(imageFile);System.out.println(result);} catch (ApiException e) {System.err.println("Exception when calling PreprocessingApi#preprocessingUnrotateAdvanced");e.printStackTrace();}

And that’s it! We’re already done. This particular function uses Deep Learning to provide a great level of accuracy. If you would prefer speed, you may instead use preprocessingUnrotate.

Image for post
Image for post

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store