Azure OCR or other Azure Cognitive Function To Read Text From PDF

Viewed 939

I have a project where I must read PDF from URLs or Blobs, and Extract the Text from them to use then Azure Cognitive Indexing / Search/ I am following the Examples using Computer Vision and only able to parse and extract text from Image Files. I have looked around and I see that there is some mention of this Capability, but it is very sparse, there is no Github example I can find that does PDF documents.

Any suggestions or pointers on where to look. I know Amazon has Textetract but my client is Azure-based, and I don't really want to use Syncfusion tools if I can help it.

so I have tried the following. Validation is just a warpper class I was trying to simplify the return of the object,

  • Photos Work, _ Read Text from URl Works if its a photo based .png, jpg but no PDF.

Your help is greatly appreciated

using System;
using System;
using System.Collections.Generic;
using System.Text;
using System;
using System.Collections.Generic;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models;

using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Threading;
using System.Linq;
namespace ConsoleApp1
{
   class Program
   {
       // Uto see blood warning uncomment this url 
       //// private const string ANALYZE_URL_IMAGE = "https://th.bing.com/th/id/R8d171effb0b5705e35cd71970cdac2db?rik=f1NV8StS9DAgaQ&riu=http%3a%2f%2f4.bp.blogspot.com%2f-T9xYlHTPknQ%2fT2tH6Z6kgPI%2fAAAAAAAAA2k%2fjiC0bzdwZes%2fs1600%2f526054_10100251268413224_34308675_43923934_1717611278_n.jpg&ehk=%2b6xMJU4ck0rkUSqr3VpADdO5MASYJ03WNGMUKGPU1iA%3d&risl=&pid=ImgRaw";

      
       /// <summary>
       ///  to see very cool party use this url 
       /// </summary>
       private const string ANALYZE_URL_IMAGE = "https://birthdaywisheszone.com/wp-content/uploads/2018/10/how-to-throw-a-bachelor-party.jpeg";
       
       // URL For OCR Test
       
       private const string READ_TEXT_URL_IMAGE = "https://th.bing.com/th/id/OIP.eWqa4Ch11Ie2OcvNPO8IHQHaMi?w=192&h=325&c=7&o=5&pid=1.7"; (if i put a PDF link in here it says multiple bad requests errors (not discriptitve), but with this or any normal picture link it is fine )

       public static ImageAnalysis Results1;

       


       static string subscriptionKey = "*********8";
       static string endpoint = "*********";
        

       // it gives a description, if its adult, ages of people in the photo, racy or not, it can take a score as well. 

       /// <summary>
       ///  just google any photo and past link
       /// </summary>
       /// <param name="args"></param>
       
       
       
       
       
       static void Main(string[] args)
       {// Create a client
           ComputerVisionClient client = Authenticate(endpoint, subscriptionKey);
           // Analyze an image to get features and other properties.
            AnalyzeImageUrl(client, ANALYZE_URL_IMAGE).Wait();

           // Extract text (OCR) from a URL image using the Read API
           //ReadFileUrl(client, READ_TEXT_URL_IMAGE).Wait();


           var Image = DetectImageObject(Results1);


          
           
           var Faces = DetectFacesInImage(Results1);
           
           var Adult = DetectAdultImages(Results1);
         
          var Brands =  DetectBrandsObject(Results1);
         
          var Gore =  DetectGoreImages(Results1);

           var Racy = DetectRacyImages(Results1);
           

           if(Image.isImage == true)
           {
               Console.WriteLine("this is the Description of the Image");
               foreach(var i in  Image.Description)
               {
                   Console.WriteLine(i.Text);
               }
           }

           if (Faces.isFaces == true)
           {
               Console.WriteLine("There are Adults in this Photos");
               foreach (var i in Faces.FaceDescriptions)
               {
                   Console.WriteLine(i.Age + " who is " + i.Gender );
               }
           }



           if (Adult.isAdult == true)
           {
               Console.WriteLine("There are Adults in this Photos");
               foreach (var i in Adult.Description)
               {
                   Console.WriteLine(i.Text);
               }
           }
           if (Brands.isBrands == true)
           {
               Console.WriteLine("There are Brands in this Photos");
               foreach (var i in Brands.ListOFBrands)
               {
                   Console.WriteLine(i.Name);
               }
           }

           if (Gore.isGore == true)
           {
               Console.WriteLine("######################## Blood in PHOTO ###########");
               
               
           }
           if (Racy.isRacy == true)
           {
               Console.WriteLine("######################## racy in photos ###########");


           }

           Console.WriteLine("text paper");




           ReadFileUrl(client, READ_TEXT_URL_IMAGE).Wait();


       }





       /*
* AUTHENTICATE
* Creates a Computer Vision client used by each example.
*/
       public static ComputerVisionClient Authenticate(string endpoint, string key)
       {
           ComputerVisionClient client =
             new ComputerVisionClient(new ApiKeyServiceClientCredentials(key))
             { Endpoint = endpoint };
           return client;
       }

       /* 
* ANALYZE IMAGE - URL IMAGE
* Analyze URL image. Extracts captions, categories, tags, objects, faces, racy/adult content,
* brands, celebrities, landmarks, color scheme, and image types.
*/
       public static async Task AnalyzeImageUrl(ComputerVisionClient client, string imageUrl)
       {
           Console.WriteLine("----------------------------------------------------------");
           Console.WriteLine("ANALYZE IMAGE - URL");
           Console.WriteLine();

           // Creating a list that defines the features to be extracted from the image. 

           List<VisualFeatureTypes?> features = new List<VisualFeatureTypes?>()
   {
       VisualFeatureTypes.Categories, VisualFeatureTypes.Description,
       VisualFeatureTypes.Faces, VisualFeatureTypes.ImageType,
       VisualFeatureTypes.Tags, VisualFeatureTypes.Adult,
       VisualFeatureTypes.Color, VisualFeatureTypes.Brands,
       VisualFeatureTypes.Objects,
   };







           Console.WriteLine($"Analyzing the image {Path.GetFileName(imageUrl)}...");
           Console.WriteLine();
           // Analyze the URL image 
          ImageAnalysis  Results = await client.AnalyzeImageAsync(imageUrl, features);











           Results1 = Results;


       }


       public static ValuationResults DetectImageObject(ImageAnalysis Results)
       {
           ValuationResults valuation = new ValuationResults();

           if (Results.Description.Captions.Count > 0)
           {
               valuation.isImage = true;
               valuation.Description = Results.Description.Captions.ToList();
           }



          

           return valuation;
       }

       public static ValuationResults DetectBrandsObject(ImageAnalysis Results)
       {
           ValuationResults valuation = new ValuationResults();

          if(Results.Brands.Count > 0)
           {
               valuation.isBrands = true;
               valuation.ListOFBrands = Results.Brands.ToList();
           }

           

           return valuation;
       }


       public static ValuationResults DetectFacesInImage(ImageAnalysis Results)
       {
           ValuationResults valuation = new ValuationResults();

          

           if (Results.Faces.Count > 0)
           {
               valuation.isFaces = true;
               valuation.isFacesCount = Results.Faces.Count;
               valuation.FaceDescriptions = Results.Faces.ToList();
                
               

               

               foreach (var face in Results.Faces)
               {
                   Console.WriteLine($"A {face.Gender} of age {face.Age} at location {face.FaceRectangle.Left}, " +
                     $"{face.FaceRectangle.Left}, {face.FaceRectangle.Top + face.FaceRectangle.Width}, " +
                     $"{face.FaceRectangle.Top + face.FaceRectangle.Height}");
               }
           }
           else
           {
               
           }

           return valuation;
       }

       public static ValuationResults DetectAdultImages(ImageAnalysis results)
       {
           // Adult or racy content, if any.

           ValuationResults valuation = new ValuationResults();

           if (results.Adult.IsAdultContent)
           {
               valuation.isAdult = true;
               valuation.isAdultScore = results.Adult.AdultScore;

           }
           else
           {
               valuation.isAdult = false;
               valuation.isAdultScore = results.Adult.AdultScore;
           }
           return valuation;

       }
       public static ValuationResults DetectRacyImages(ImageAnalysis results)
       {
           // Adult or racy content, if any.

           ValuationResults valuation = new ValuationResults();

           if (results.Adult.IsRacyContent)
           {
               valuation.isRacy = true;
              

           }
           else
           {
              
           }

           return valuation;
       }

       public static ValuationResults DetectGoreImages(ImageAnalysis results)
       {
           // Adult or racy content, if any.

           ValuationResults valuation = new ValuationResults();


           if (results.Adult.IsGoryContent)
           {
               valuation.isGore = true;
               valuation.isGoreScore = results.Adult.GoreScore;

           }

           else
           {
               valuation.isGore = false;
               valuation.isGoreScore = results.Adult.GoreScore;
           }


           return valuation;
       }

       public static async Task ReadFileUrl(ComputerVisionClient client, string urlFile)
       {
           Console.WriteLine("----------------------------------------------------------");
           Console.WriteLine("READ FILE FROM URL");
           Console.WriteLine();

           // Read text from URL
           var textHeaders = await client.ReadAsync(urlFile, language: "en");
           // After the request, get the operation location (operation ID)
           string operationLocation = textHeaders.OperationLocation;
           Thread.Sleep(2000);

           // Retrieve the URI where the extracted text will be stored from the Operation-Location header.
           // We only need the ID and not the full URL
           const int numberOfCharsInOperationId = 36;
           string operationId = operationLocation.Substring(operationLocation.Length - numberOfCharsInOperationId);

           // Extract the text
           ReadOperationResult results;
           Console.WriteLine($"Extracting text from URL file {Path.GetFileName(urlFile)}...");
           Console.WriteLine();
           do
           {
               results = await client.GetReadResultAsync(Guid.Parse(operationId));
           }
           while ((results.Status == OperationStatusCodes.Running ||
               results.Status == OperationStatusCodes.NotStarted));

           // Display the found text.
           Console.WriteLine();
           var textUrlFileResults = results.AnalyzeResult.ReadResults;
           foreach (ReadResult page in textUrlFileResults)
           {
               foreach (Line line in page.Lines)
               {
                   Console.WriteLine(line.Text);
               }
           }
           Console.WriteLine();
       }

   }
}
1 Answers

The Computer Vision Read API is Azure's latest OCR technology that handles large images and multi-page documents as inputs and extracts printed text in Dutch, English, French, German, Italian, Portuguese, and Spanish. It also includes support for handwritten OCR in English, digits, and currency symbols from images and multi-page PDF documents. It's optimized to extract text from text-heavy images and multi-page PDF documents with mixed languages. It supports detecting both printed and handwritten text in the same image or document (for English only).

Here is the doc for release update in computer vision.

Related