How to fix a skewed document photo for OCR in C# .NET Framework

Skewed photos are the bane of any proper optical character recognition. While easy to fix manually, a computer requires a great deal of training via Deep Learning to manage this effectively on its own. Don’t worry though, there’s a shortcut! Today we will be looking at how to employ an API to get up and running almost instantly.

First we will install the API Client that we need with the Package Manager console:

Install-Package Cloudmersive.APIClient.NET.OCR -Version 2.1.4

Next we must call PreprocessingUnskew using this bit of code:

using System;using System.Diagnostics;using Cloudmersive.APIClient.NET.OCR.Api;using Cloudmersive.APIClient.NET.OCR.Client;using Cloudmersive.APIClient.NET.OCR.Model;namespace Example{public class PreprocessingUnskewExample{public void main(){// Configure API key authorization: ApikeyConfiguration.Default.AddApiKey("Apikey", "YOUR_API_KEY");// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed// Configuration.Default.AddApiKeyPrefix("Apikey", "Bearer");var apiInstance = new PreprocessingApi();var imageFile = new System.IO.Stream(); // System.IO.Stream | Image file to perform OCR on.  Common file formats such as PNG, JPEG are supported.try{// Detect and unskew a photo of a documentbyte[] result = apiInstance.PreprocessingUnskew(imageFile);Debug.WriteLine(result);}catch (Exception e){Debug.Print("Exception when calling PreprocessingApi.PreprocessingUnskew: " + e.Message );}}}}

Done!

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