Azure OCR或其他Azure认知功能从PDF中读取文本

vm0i2vca  于 2022-11-17  发布在  其他
关注(0)|答案(2)|浏览(280)

我有一个项目,我必须从URL或Blob中读取PDF,并从中提取文本以使用Azure认知索引/搜索/我正在使用计算机视觉的示例,只能从图像文件中解析和提取文本。我环顾四周,我看到有一些提到这个功能,但它非常稀疏,没有Github的例子,我可以找到做PDF文档。
我知道亚马逊有Textetract,但我的客户是基于Azure的,如果我能帮上忙,我真的不想使用Syncfusion工具。
因此我尝试了以下方法:验证只是一个warpper类我试图简化对象的返回,

  • 照片工作,_如果是基于照片的.png,jpg但没有PDF,则从URL读取文本。

非常感谢您的帮助

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();
       }

   }
}
fdbelqdn

fdbelqdn1#

Computer Vision Read API是Azure的最新OCR技术,可将大型图像和多页文档作为输入进行处理,并提取荷兰语、英语、法语、德语、意大利语、葡萄牙语和西班牙语的打印文本。它还支持英语、数字、和多页PDF文档中提取文本。它经过优化,可从文本量大的图像以及混合语言的多页PDF文档中提取文本。它支持在同一图像或文档中同时检测打印文本和手写文本(仅适用于英语)。
这是计算机视觉中的doc版本更新。

mum43rcc

mum43rcc2#

如果你想使用Azure Blob的PDF文件,那么-〉在Azure Portal上提供您的PDF文件的生成SAS令牌,并将其作为URL。
使用此链接可遵循规则C# code to read a pdf file
对我来说,它工作得很好,我能够逐行阅读pdf的每一页

相关问题