How to straighten a document image with Java using deep learning

Cloudmersive
2 min readJan 25, 2020

--

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.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet