SHARE
TWEET

Aspose+MS-AI

andruhovski May 31st, 2020 1,133 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.Drawing.Imaging;
  5. using Microsoft.Azure.CognitiveServices.Vision.ComputerVision;
  6. using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models;
  7. using System.Threading.Tasks;
  8. using System.IO;
  9. using Newtonsoft.Json;
  10. using Newtonsoft.Json.Linq;
  11. using Aspose.Pdf;
  12.  
  13. namespace Aspose.PDF.Demo.ImageClassification
  14. {
  15.     class Program
  16.     {
  17.         private static readonly HashSet<string> Tags = new HashSet<string>();
  18.         private static readonly StringCollection FileNames = new StringCollection();
  19.         static readonly string SubscriptionKey = "";
  20.         static readonly string Endpoint ="https://aspose-pdf-demo.cognitiveservices.azure.com/";
  21.         private static readonly License License = new Aspose.Pdf.License();
  22.         static void Main()
  23.         {
  24.             License.SetLicense(@"");
  25.             ExtractImages();
  26.             CollectImageTags();
  27.             SaveMetaData();
  28.         }
  29.  
  30.         private static void SaveMetaData()
  31.         {
  32.             var document = new Aspose.Pdf.Document(@"c:\tmp\parserdemo.pdf");
  33.             _ = new DocumentInfo(document)
  34.             {
  35.                 Keywords = string.Join("; ", Tags)
  36.             };
  37.             document.Save(@"c:\tmp\parserdemo.pdf");
  38.         }
  39.  
  40.         private static void CollectImageTags()
  41.         {
  42.             // Create a client
  43.             ComputerVisionClient client = Authenticate(Endpoint, SubscriptionKey);
  44.             foreach (var fileName in FileNames)
  45.             {
  46.                
  47.                 var imageTags = AnalyzeImageUrl(client, fileName).Result.Tags;
  48.                 foreach (var imageTag in imageTags)
  49.                 {
  50.                     if (imageTag.Confidence > 0.9)
  51.                         Tags.Add(imageTag.Name);
  52.                 }
  53.             }
  54.         }
  55.  
  56.         private static void ExtractImages()
  57.         {
  58.             var document = new Aspose.Pdf.Document(@"c:\tmp\parserdemo.pdf");
  59.             var abs = new ImagePlacementAbsorber();
  60.             document.Pages.Accept(abs);
  61.             var imageCount = 0;
  62.             Directory.CreateDirectory("C:\\tmp\\extracted_images\\");
  63.             foreach (var imagePlacement in abs.ImagePlacements)
  64.             {
  65.                 var xImage = imagePlacement.Image;
  66.                 var fileName = $@"C:\\tmp\\extracted_images\\image_{imageCount++}.jpg";
  67.                 var stream =
  68.                     new FileStream(fileName,
  69.                         FileMode.Create);
  70.                 xImage.Save(stream, ImageFormat.Jpeg);
  71.                 stream.Close();
  72.                 FileNames.Add(fileName);
  73.             }
  74.         }
  75.  
  76.         public static ComputerVisionClient Authenticate(string endpoint, string key)
  77.         {
  78.             var client =
  79.                 new ComputerVisionClient(new ApiKeyServiceClientCredentials(key))
  80.                     {Endpoint = endpoint};
  81.             return client;
  82.         }
  83.  
  84.         public static async Task<TagResult> AnalyzeImageUrl(ComputerVisionClient client, string imageUrl)
  85.         {
  86.             Console.WriteLine("----------------------------------------------------------");
  87.             Console.WriteLine("ANALYZE IMAGE - URL");
  88.             Console.WriteLine();
  89.  
  90.             Console.WriteLine($"Analyzing the image {Path.GetFileName(imageUrl)}...");
  91.             Console.WriteLine();
  92.             // Analyze the URL image
  93.             return await client.TagImageInStreamAsync(
  94.                 File.OpenRead(imageUrl ?? throw new ArgumentNullException(nameof(imageUrl))));
  95.         }
  96.     }
  97. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top