Introduction

In our previous article we learned
how to Analyze an Image Using Computer Vision API With ASP.Net Core & C#. In this article we are going to learn how to extract printed text also known as optical character recognition (OCR) from an image using one of the important Cognitive Services API
called as Computer Vision API. So we need a valid subscription key for accessing this feature in an image.

Optical Character Recognition (OCR)

Optical Character Recognition (OCR) detects text in an image and extracts the recognized characters into a machine-usable character stream.

Prerequisites

Subscription Key Free Trial

If you don’t have Microsoft Azure Subscription and want to test the Computer Vision API because it requires a valid Subscription key for processing the image information. Don’t worry !! Microsoft gives a 7 day trial Subscription Key (
Click here ). We can use that Subscription key for testing purposes. If you sign up using the Computer Vision free trial, then your subscription keys are valid for the westcentral region (https://westcentralus.api.cognitive.microsoft.com ).

Requirements

These are the major requirements mentioned in the Microsoft docs.

Supported input methods: Raw image binary in the form of an application/octet stream or image URL.

Supported image formats: JPEG, PNG, GIF, BMP.

Image file size: Less than 4 MB.

Image dimension: Greater than 50 x 50 pixels.

Computer Vision API

First, we need to log into the Azure Portal with our Azure credentials. Then we need to create an Azure Computer Vision Subscription Key in the Azure portal.

Click on "Create a resource" on the left side menu and it will open an "Azure Marketplace". There, we can see the list of services. Click "AI + Machine Learning" then click on the "Computer Vision".

Resource group : We can create a new resource group or choose from an existing one.

Now click on the "OCRApp" in dashboard page and it will redirect to the details page of
OCRApp ( "Overview" ). Here, we can see the Manage Key ( Subscription key details ) & Endpoint details. Click on the Show access keys links and it will redirect to another page.

We can use any of the subscription keys or regenerate the given key for getting image information using Computer Vision API.

Endpoint

As we mentioned above the location is the same for all the free trial Subscription Keys. In Azure we can choose available locations while creating a Computer Vision API. We have used the following endpoint in our code.

"https://westus.api.cognitive.microsoft.com/vision/v1.0/ocr"

View Model

The following model will contain the API image response information.

usingSystem.Collections.Generic;

namespaceOCRApp.Models

{

publicclass
Word

{

publicstring
boundingBox { get;
set; }

publicstring
text { get;
set; }

}

publicclass
Line

{

publicstring
boundingBox { get;
set; }

publicList<Word> words { get;
set; }

}

publicclass
Region

{

publicstring
boundingBox { get;
set; }

publicList<Line> lines { get;
set; }

}

publicclass
ImageInfoViewModel

{

publicstring
language { get;
set; }

publicstring
orientation { get;
set; }

publicint
textAngle { get;
set; }

publicList<Region> regions { get;
set; }

}

}

Request URL

We can add additional parameters or request parameters ( optional ) in our API "endPoint" and it will provide more information for the given image.

detectOrientation

Vision API Service

The following code will process and generate image information using Computer Vision API and its response is mapped into the “ImageInfoViewModel”. We can add the valid Computer Vision API Subscription Key into the following code.

usingNewtonsoft.Json;

usingOCRApp.Models;

usingSystem;

usingSystem.Collections.Generic;

usingSystem.IO;

usingSystem.Net.Http;

usingSystem.Net.Http.Headers;

usingSystem.Threading.Tasks;

namespaceOCRApp.Business_Layer

{

publicclass
VisionApiService

{

// Replace <Subscription Key> with your valid subscription key.

conststring
subscriptionKey = "<Subscription Key>";

// You must use the same region in your REST call as you used to

// get your subscription keys. The paid subscription keys you will get

Download

Output

Reference

Summary

From this article we have learned Optical Character Recognition (OCR) from an image using One of the important Cognitive Services API ( Computer Vision API ). I hope this article is useful for all Azure Cognitive Services API beginners.