How To Extract Text from Image Using Azure Cognitive Services

How To Extract Text from Image Using Azure Cognitive Services

In this Azure tutorial, we will discuss How To Extract Text from Image Using Azure Cognitive Services, Azure extract text from image Along with this, we will also discuss a few other topics like Azure Cognitive Services Read Text From Images, Create Azure Cognitive Service using Azure Portal, Creating Console App (.NET Core) Visual Studio 2019 and we will also discuss Azure Cognitive Services OCR.

How To Extract Text from Image Using Azure Cognitive Services? We will perform the below steps as part of this functionality

  • We will create an Azure Cognitive Service in the Azure Portal and we will copy the key and endpoint details.
  • The next thing is, we will create a Console App( .NET Core) using Visual Studio 2019 and will implement the functionality.

How To Extract Text from Image Using Azure Cognitive Services

Well here, we will discuss a very important example, where we will discuss How To Extract Bill Details From An Image Using Azure Cognitive Services.

In the real-time scenarios, we will come across many scenarios where instead of manually reading the bill, we will have to read the bill details from an image and we need to maybe auto-populate these details in a form. Nowadays in every case, we are not doing anything manually. We want to implement everywhere automation. We want to automate each and every process nowadays.

To deal with this type of scenario, Microsoft helps us to provide Azure Cognitive Service OCR. OCR’s meaning is Optical Character Recognition.

Azure Cognitive Services Read Text From Images

So As we know using the Azure Cognitive Service, A developer can easily implement the AI feature without any expertise on the AI and ML areas. The developer’s dream becomes true. A developer can easily implement the AI features by just calling the Azure Cognitive Service API. Very less effort involved and lots of time developers can save to achieve such an excellent functionality i.e the AI.

Here as part of this functionality development, we will try to extract the date and the bill details from the bill image.

So, Before starting the actual development, we should know the Prerequisites needed for this development activity.

Prerequisites

  • You must have a valid Azure Subscription or a Valid Azure Account. If you don’t have till now, create an Azure Free Account now.
  • Visual Studio 2019 needs to be installed in your Machine. If you don’t have it in your local machine or dev machine, Install Visual Studio 2019 now.

Create Azure Cognitive Service using Azure Portal

Well here we will discuss How to Create Azure Cognitive Service using Azure Portal.

Login to the Azure Portal (https://portal.azure.com/)

Once you logged in to the Azure Portal, then you need to search for the Cognitive Services. Then click on the search result as shown below

Extract Text from Image Using Azure Cognitive Services

One more way to get the same options, From the left navigation, you can also click on the + Create a resource and search for the Cognitive Services. Then click on the search result as shown below

How do I Extract Text from Image Using Azure Cognitive Services

The next step is, click on the Create button as highlighted in the below window.

Steps to Extract Text from Image Using Azure Cognitive Services

Provide the below details, On the Create Cognitive services window

  • Subscription: You need to Choose the valid subscription that you want to use here.
  • Resource Group: For this option, You need to choose your existing Resource Group if you have. If you don’t have an existing Resource Group then you can create a new Resource Group by clicking on the Create new link.
  • Region: Provide the Region for your Azure Cognitive Services.
  • Name: Here, Provide a unique name for your Azure Cognitive Services.
  • Pricing tier: Select the Pricing tier based on your business need. You can click on the View full pricing details link to verify the pricing details for your reference.
  • Select the below highlighted two checkboxes to accept the terms and conditions.

Finally click on the Review + Create button as highlighted below.

Create Azure Cognitive Service using Azure Portal

Now it will check all the information entered by you is correct or not. Once it will find all the details are correct, It will show you Validation Passed. Now you can able to see the Create button is enabled. Click on the Create button as highlighted below to create the Cognitive Service.

How to Create Azure Cognitive Service using Azure Portal

Now you can able to see your deployment is completed successfully without any issue. Click on the Go to Resource button to navigate to the Azure Cognitive service, that you have created now.

 How to Create Azure Cognitive Service Azure Portal

Once you will click on the Go to resource button, you can able to see the new Cognitive service.

On the Cognitive service page, click on the keys and Endpoint option from the left navigation. Now you can able to see the Key1, ENDPOINT value, keep both the value and keep it with you as we are going to use those values in our code in the next steps. Click on the copy button as highlighted to copy those values.

Azure Cognitive Services OCR

Creating Console App (.NET Core) Visual Studio 2019

Now the next step is to create the console App (.NET Core) using Visual Studio 2019. Assuming that you have already installed the Visual Studio 2019. Let’s start creating the console App (.NET Core).

Open the Visual Studio 2019 and click on the Create a new Project button

Select the Console App (.NET Core) as the project template and then click on the Next button.

Azure Cognitive Services OCR tutorial

Provide a name for the Project and then click provide the location where you want to save your project on the configure your new project window and then click on the Create button.

How To Extract Image Details Using Azure Cognitive Service

Now the Console App (.NET Core) project will get created successfully without any issue. Once the project will get created successfully, Now the next step is to add a NuGet package.

Right click on the Project name —–> Click on the Manage NuGet Packages option as shown below

How To Extract Bill Details From An Image Using Azure Cognitive Service

Now search for Microsoft.Azure.CognitiveServices.Vision.ComputerVision Nuget package and then you need to select the search result and then click on the Install button and then click on the I accept button to install the Nuget Package as shown below

Extract Image Details Azure Cognitive Service

.Now if you will check the projectname.csproj file, it should look like below

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.CognitiveServices.Vision.ComputerVision" Version="5.0.0" />
  </ItemGroup>

</Project>

Here is my bill that I have saved in my local machine in C:\Users\Bijay\Desktop\VM\Bill\mybill2.jpg path.

Extract Image Details Azure Cognitive Service Visual Studio 2019

Now, we need to do the code changes for the Program.cs file. So add the code in your Program.cs file as beow.

using Microsoft.Azure.CognitiveServices.Vision.ComputerVision;
using System;
using System.IO;

namespace mydemoapp
{
    class Program
    {
         // put your Cognitive Service Key1 value
        static string Key = "12c9e662d55547779ce9785985932118";
        // put your Cognitive Service URL
        static string url = "https://mynewcognitiveservices91.cognitiveservices.azure.com/";
        
            static void Main(string[] args)
            {
                var client = new ComputerVisionClient(new ApiKeyServiceClientCredentials(Key));
                client.Endpoint = url;

                var myfile = File.OpenRead(@"C:\Users\Bijay\Desktop\VM\Bill\mybill2.jpg");
                var result = client.RecognizePrintedTextInStreamAsync(false, myfile);
                 result.Wait();

                var rst = result.Result;
                foreach (var r in rst.Regions)
                {
                    foreach (var t in r.Lines)
                    {
                        foreach (var w in t.Words)
                        {
                            var coordinates = w.BoundingBox.Split(',');
                            Console.WriteLine($"Found word {w.Text} at" +
                            $" X {coordinates[0]} and Y {coordinates[1]}");
                        }
                    }
                }
            }
        }
    }

You can see it here

Extract Image Details by using Azure Cognitive Service

After you have added the code in the program.cs file and replace the key and URL value with the key and endpoint URL value of your Azure Cognitive Service that you have created in the Azure Portal and already copied it and kept it with you in the above steps.

Now let’s run the project and see what’s happening. So press F5 to run the project and you can able to see we got the expected output like below.

How to Create Cognitive Service in Azure Portal

So this is How To Extract Text from Image Using Azure Cognitive Services Azure extract text from image by following the above steps.

Azure Cognitive Services OCR

Well here we will discuss, a very important topic i.e Azure Cognitive Services OCR.

OCR means the Optical Character Recognition (OCR) that actually helps you to detect the different text in an image and then extracts the different recognized characters into a machine-usable format. The Computer Vision API of Azure includes the Optical Character Recognition (OCR) capabilities.

In the real-time scenarios, when you have the requirement that users will just upload an image and you need to read all the text values from the image, and then you need to fill a form from the different values that you got it from the image. In this type of scenario, the Azure Cognitive Services OCR really helps the developer a lot, We all know that this is not an easy job but with the help of the API, it is really easy to achieve this type of functionality.

While working with the OCR, on the successful scenario, you will get the output as expected but incase of failure, you might get the messages like below

  • InvalidImageFormat (Input data is not a valid image.)
  • InvalidImageSize (Input image is too large.)
  • NotSupportedImage
  • NotSupportedLanguage (Specified language is not supported.)
  • InvalidImageUrl (Image URL is badly formatted or not accessible.)

Or, You might get the error code as 500 and the issue can be as below

  • InternalServerError
  • FailedToProcess (Failed to process the image.)
  • Timeout (Image processing time out.)

Remember, in the case of the Azure Cognitive Services OCR, Below are the input requirements, which is must

  • The file format must be JPEG, PNG, BMP, PDF, and TIFF
  • It supports up to 2000 pages in the case of the PDF and TIFF files are processed. One more thing is for the free tier, only the first two pages will be processed.
  • Remember that it is 50 MB and in the case of the free tier, it is 4 MB. The dimension must be within 50 x 50 pixels to 10000 x 10000 pixels.
  • At max 17 x 17 inches dimensions must be for the PDF and the size will be like A3 paper size or smaller.

The API supports reading the text from the image, text from a document, Text from the handwritten documents (Only English language is supported as of now), Text from the printed documents, Text from the Mixed language documents.

These are a few key points on the Azure Cognitive Services OCR that we should know while working with the implementation of Azure Cognitive Services OCR. We will see an excellent example of the Implementation of Azure Cognitive Services OCR.

As part of the implementation of Azure Cognitive Services OCR, We have discussed above the example How To Extract Image Details Using Azure Cognitive Service. Where we were able to read the text from a computer-generated bill.

So this is what all about the Azure Cognitive Services OCR and we saw the implementation on How To Extract Image Details Using Azure Cognitive Service.

You may also like following the below Articles:

Wrapping Up

Well, in this article we discussed How To Extract Text from Image Using Azure Cognitive Services, Azure extract text from image, Azure Cognitive Services Read Text From Images, Create Azure Cognitive Service using Azure Portal, Creating Console App (.NET Core) Visual Studio 2019 and we also discussed Azure Cognitive Services OCR. Hope you have enjoyed this article !!!

Leave a Comment