|
using System.Linq; |
|
using System.Threading.Tasks; |
|
|
|
using EPiServer; |
|
using EPiServer.Core; |
|
using EPiServer.DataAccess; |
|
using EPiServer.Logging; |
|
|
|
using Episerver.Playground10.Models.Media; |
|
|
|
using EPiServer.Security; |
|
using EPiServer.ServiceLocation; |
|
|
|
using Microsoft.ProjectOxford.Vision; |
|
using Microsoft.ProjectOxford.Vision.Contract; |
|
|
|
public class ImageHelper |
|
{ |
|
private readonly IContentRepository contentRepository; |
|
|
|
private readonly ILogger log = LogManager.GetLogger(); |
|
|
|
private readonly string subscriptionKey = "your_key"; |
|
|
|
public ImageHelper(IContentRepository contentRepository) |
|
{ |
|
this.contentRepository = contentRepository; |
|
} |
|
|
|
public ImageHelper() |
|
{ |
|
this.contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>(); |
|
} |
|
|
|
public async Task TagImagesAsync(ImageFile imageFile) |
|
{ |
|
AnalysisResult analysisResult = await this.UploadAndAnalyzeImageAsync(imageFile).ConfigureAwait(false); |
|
|
|
this.ProcessAnalysisResult(analysisResult, imageFile); |
|
} |
|
|
|
private void ProcessAnalysisResult(AnalysisResult result, ImageFile imageFile) |
|
{ |
|
if (result == null) |
|
{ |
|
return; |
|
} |
|
|
|
ImageFile clone = imageFile.CreateWritableClone() as ImageFile; |
|
|
|
if (clone == null) |
|
{ |
|
return; |
|
} |
|
|
|
|
|
if (result.ImageType != null) |
|
{ |
|
string clipArtType; |
|
switch (result.ImageType.ClipArtType) |
|
{ |
|
case 0: |
|
clipArtType = "Non-clipart"; |
|
break; |
|
case 1: |
|
clipArtType = "Ambiguous"; |
|
break; |
|
case 2: |
|
clipArtType = "Normal clipart"; |
|
break; |
|
case 3: |
|
clipArtType = "Good clipart"; |
|
break; |
|
default: |
|
clipArtType = "Unknown"; |
|
break; |
|
} |
|
|
|
clone.ClipArtType = clipArtType; |
|
|
|
string lineDrawingType; |
|
switch (result.ImageType.LineDrawingType) |
|
{ |
|
case 0: |
|
lineDrawingType = "Non-lineDrawing"; |
|
break; |
|
case 1: |
|
lineDrawingType = "LineDrawing"; |
|
break; |
|
default: |
|
lineDrawingType = "Unknown"; |
|
break; |
|
} |
|
|
|
clone.LineDrawingType = lineDrawingType; |
|
} |
|
|
|
if (result.Adult != null) |
|
{ |
|
clone.IsAdultContent = result.Adult.IsAdultContent; |
|
clone.IsRacyContent = result.Adult.IsRacyContent; |
|
} |
|
|
|
if (result.Categories != null && result.Categories.Length > 0) |
|
{ |
|
clone.ImageCategories = result.Categories.Select(c => c.Name).ToArray(); |
|
} |
|
|
|
if (result.Color != null) |
|
{ |
|
clone.AccentColor = result.Color.AccentColor; |
|
clone.DominantColorBackground = result.Color.DominantColorBackground; |
|
clone.DominantColorForeground = result.Color.DominantColorForeground; |
|
clone.IsBwImg = result.Color.IsBWImg; |
|
|
|
if (result.Color.DominantColors != null && result.Color.DominantColors.Length > 0) |
|
{ |
|
clone.DominantColors = result.Color.DominantColors; |
|
} |
|
} |
|
|
|
if ((clone.Tags == null || clone.Tags.Length == 0) && result.Tags != null) |
|
{ |
|
clone.Tags = result.Tags.Where(t => t.Confidence > 0.5).Select(t => t.Name).ToArray(); |
|
} |
|
|
|
if (result.Description != null) |
|
{ |
|
clone.Caption = result.Description.Captions.OrderByDescending(c => c.Confidence).FirstOrDefault()?.Text; |
|
|
|
if (clone.Tags == null || clone.Tags.Length == 0) |
|
{ |
|
clone.Tags = result.Description.Tags; |
|
} |
|
} |
|
|
|
this.contentRepository.Save( |
|
clone, |
|
SaveAction.Publish | SaveAction.ForceCurrentVersion, |
|
AccessLevel.NoAccess); |
|
} |
|
|
|
private async Task<AnalysisResult> UploadAndAnalyzeImageAsync(ImageData imageData) |
|
{ |
|
VisionServiceClient visionServiceClient = new VisionServiceClient(this.subscriptionKey); |
|
|
|
this.log.Information("VisionServiceClient is created"); |
|
|
|
this.log.Information("Calling VisionServiceClient.AnalyzeImageAsync()..."); |
|
|
|
VisualFeature[] visualFeatures = |
|
{ |
|
VisualFeature.Adult, VisualFeature.Categories, VisualFeature.Color, |
|
VisualFeature.Description, VisualFeature.ImageType, |
|
VisualFeature.Tags |
|
}; |
|
|
|
return |
|
await visionServiceClient.AnalyzeImageAsync(imageData.BinaryData.OpenRead(), visualFeatures).ConfigureAwait(false); |
|
|
|
} |
|
} |