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.

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 is 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 on your Machine. If you don’t have it on 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.

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

Once you logged in to the Azure Portal, then you need to search for 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 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, to 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 one. 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 whether 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 issues. 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 and 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 Visual Studio 2019. Let’s start creating the console App (.NET Core).

Open 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 issues. 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 to your Program.cs file as below.

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 to 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 an Image Using Azure Cognitive Services Azure extracts text from an image by following the above steps.

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, and Create Azure Cognitive Service using Azure Portal. Hope you have enjoyed this article !!!