Ejemplo n.º 1
0
 public static Task <TResult> AnalyzeAsync <TResult>(this IRef <Content> contentRef,
                                                     Func <ImageAnalysis, double?, TResult> onAnalyzed,
                                                     Func <TResult> onNotFound = default)
 {
     return(AppSettings.CognitiveServices.ComputerVisionSubscriptionKey.ConfigurationString(
                subscriptionKey =>
     {
         return AppSettings.CognitiveServices.ComputerVisionEndpoint.ConfigurationUri(
             async endpointUri =>
         {
             using (var computerVision = new ComputerVisionClient(
                        new ApiKeyServiceClientCredentials(subscriptionKey),
                        new System.Net.Http.DelegatingHandler[] { }))
             {
                 return await await contentRef.LoadStreamAsync(
                     async(imageStream, contentType) =>
                 {
                     var widthMultiplier = default(double?);
                     if (imageStream.Length > 4000000)
                     {
                         var image = System.Drawing.Image.FromStream(imageStream);
                         var newImageStream = new MemoryStream();
                         widthMultiplier = Math.Sqrt(4000000.0 / imageStream.Length);
                         image
                         .ResizeImage(
                             (int)(image.Width * widthMultiplier),
                             (int)(image.Height * widthMultiplier))
                         .Save(newImageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                         newImageStream.Position = 0;
                         imageStream = newImageStream;
                     }
                     computerVision.Endpoint = endpointUri.OriginalString;
                     var featuresToSearchFor = VisualFeatureTypes.Categories.AsArray()
                                               .Append(VisualFeatureTypes.Description)
                                               .Append(VisualFeatureTypes.ImageType)
                                               .Append(VisualFeatureTypes.Objects)
                                               .Append(VisualFeatureTypes.Tags)
                                               .Append(VisualFeatureTypes.Brands)
                                               .ToList();
                     try
                     {
                         var analysis = await computerVision.AnalyzeImageInStreamAsync(
                             imageStream, featuresToSearchFor);
                         return onAnalyzed(analysis, widthMultiplier);
                     }
                     catch (ComputerVisionErrorException ex)
                     {
                         throw ex;
                     }
                 },
                     () =>
                 {
                     if (onNotFound.IsDefaultOrNull())
                     {
                         throw new ResourceNotFoundException();
                     }
                     return onNotFound().AsTask();
                 });
             }
         });
     }));
 }