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:





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

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

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