Read text on an Image using Azure cognitive services

This article help you to understand how to read text on an image using C#.net . We can read the text on images in, two ways. The first one is Computer Vision and the second one is Natural Language Processing (NLP). Computer vision helps us to read the text and then Natural Language Processing is used to make sense of that identified text. In this article, we will discuss on Computer vision.First you need to understand what is the OCR in Azure Cognitive Services?  

"OCR (
Optical character recognition) is extracts text from images like printed text, hand writting text etc which is in readable format and also OCR helps to read content on documents like pdf, word etc" , to know about more information on OCR click here.

Prerequisites :

Before we start, we should have the following things in our hand.
1. A valid Azure Subscription
2. A good Code editor, I prefer VSCode or Visual Studio

Read Text on images using c#.net
:

  • First you need to create an Computer Vision resource in Azure cogntive service. 
  • Note down the Ocp-Apim-Subscription-Key and Computer Vision URL. 
  • Below method explains converting image into byte array.            

   public  byte[] GetImageAsByteArray(string imageFilePath)
   {
    using var fileStream=new FileStream(imageFilePath,FileMode.Open,FileAccess.Read);
    using var binaryReader= new BinaryReader(fileStream);            
    return binaryReader.ReadBytes((int)fileStream.Length);
   }

  • Below method explains read OCR data on image  

public async Task<List<string>> ReadOCRData(string image, ILogger log)
        {
            var lstExtractedText = new List<string>();
            try
            {
              var errors = new List<string>();
              string extractedResult = "No Text In Image";    
              var responeData = new OCRDataModel();
              using var client = new HttpClient();
               client.DefaultRequestHeaders.Add(
                  "Ocp-Apim-Subscription-Key""{Your OCR subscription key}"));
                // Above add your subscription key

              HttpResponseMessage response;
// convert Image to steram
              var byteData = GetImageAsByteArray(image);
              using (ByteArrayContent content = new ByteArrayContent(byteData))
               {
                  content.Headers.ContentType = new MediaTypeHeaderValue
("application/octet-stream");
                  response = await client.PostAsync("Your OCR URL", content);   //https://{Your OCR Name}.cognitiveservices.azure.com/vision/v3.2
/ocr?language=unk&detectOrientation=true
              }
             string result = await response.Content.ReadAsStringAsync();
             responeData = JsonConvert.DeserializeObject<OCRDataModel>(result);
             if (response.IsSuccessStatusCode)
              {
                if (!(result.Contains("\"orientation\":\"NotDetected\"")))
                 {
                   var linesCount = responeData.Regions[0].Lines.Count;
                    for (int i = 0; i < linesCount; i++)
                     {
                      var wordsCount = responeData.Regions[0].Lines[i].Words.Count;
                       for (int j = 0; j < wordsCount; j++)
                        {
                         extractedResult=responeData.Regions[0].Lines[i].Words[j].Text
" ";
                             lstExtractedText.Add(extractedResult);
                          }
                       }                      
                   }
                }
                client.Dispose();
            }
            catch (Exception ex)
            {
                
            }
            return lstExtractedText;
        }




Comments

Popular posts from this blog

Send Email Alerts when Azure VM is Shut down using PowerShell

Sample Chatbot flow diagram

Architecture diagram for upload & validate the videos in azure