andruhovski icon

Aspose+MS-AI

andruhovski | PRO | 05/31/20 09:57:56 PM UTC | 0 ⭐ | 2004 👁️ | Never ⏰ | []
C# |

3.59 KB

|

None

|

0 👍

/

0 👎

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Drawing.Imaging;
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 Aspose.Pdf;
 
namespace Aspose.PDF.Demo.ImageClassification
{
    class Program
    {
        private static readonly HashSet<string> Tags = new HashSet<string>();
        private static readonly StringCollection FileNames = new StringCollection();
        static readonly string SubscriptionKey = "";
        static readonly string Endpoint ="https://aspose-pdf-demo.cognitiveservices.azure.com/";
        private static readonly License License = new Aspose.Pdf.License();
        static void Main()
        {
            License.SetLicense(@"");
            ExtractImages();
            CollectImageTags();
            SaveMetaData();
        }
 
        private static void SaveMetaData()
        {
            var document = new Aspose.Pdf.Document(@"c:\tmp\parserdemo.pdf");
            _ = new DocumentInfo(document)
            {
                Keywords = string.Join("; ", Tags)
            };
            document.Save(@"c:\tmp\parserdemo.pdf");
        }
 
        private static void CollectImageTags()
        {
            // Create a client
            ComputerVisionClient client = Authenticate(Endpoint, SubscriptionKey);
            foreach (var fileName in FileNames)
            {
                
                var imageTags = AnalyzeImageUrl(client, fileName).Result.Tags;
                foreach (var imageTag in imageTags)
                {
                    if (imageTag.Confidence > 0.9)
                        Tags.Add(imageTag.Name);
                }
            }
        }
 
        private static void ExtractImages()
        {
            var document = new Aspose.Pdf.Document(@"c:\tmp\parserdemo.pdf");
            var abs = new ImagePlacementAbsorber();
            document.Pages.Accept(abs);
            var imageCount = 0;
            Directory.CreateDirectory("C:\\tmp\\extracted_images\\");
            foreach (var imagePlacement in abs.ImagePlacements)
            {
                var xImage = imagePlacement.Image;
                var fileName = $@"C:\\tmp\\extracted_images\\image_{imageCount++}.jpg";
                var stream =
                    new FileStream(fileName,
                        FileMode.Create);
                xImage.Save(stream, ImageFormat.Jpeg);
                stream.Close();
                FileNames.Add(fileName);
            }
        }
 
        public static ComputerVisionClient Authenticate(string endpoint, string key)
        {
            var client =
                new ComputerVisionClient(new ApiKeyServiceClientCredentials(key))
                    {Endpoint = endpoint};
            return client;
        }
 
        public static async Task<TagResult> AnalyzeImageUrl(ComputerVisionClient client, string imageUrl)
        {
            Console.WriteLine("----------------------------------------------------------");
            Console.WriteLine("ANALYZE IMAGE - URL");
            Console.WriteLine();
 
            Console.WriteLine($"Analyzing the image {Path.GetFileName(imageUrl)}...");
            Console.WriteLine();
            // Analyze the URL image 
            return await client.TagImageInStreamAsync(
                File.OpenRead(imageUrl ?? throw new ArgumentNullException(nameof(imageUrl))));
        }
    }
}

Comments