Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tag your images by using the Microsoft Computer Vision API.

Tag images after uploading

Tag your images by using the Microsoft Computer Vision API.

Read my blog here

Powered by ReSharper image

using System.ComponentModel.DataAnnotations;
using Episerver.Playground10.Models.Properties;
using EPiServer.Core;
using EPiServer.DataAnnotations;
using EPiServer.Framework.DataAnnotations;
using EPiServer.Web;
[ContentType(GUID = "0A89E464-56D4-449F-AEA8-2BF774AB8730")]
[MediaDescriptor(ExtensionString = "jpg,jpeg,jpe,ico,gif,bmp,png")]
public class ImageFile : ImageData
{
public virtual string AccentColor { get; set; }
[UIHint(UIHint.LongString)]
public virtual string Caption { get; set; }
public virtual string ClipArtType { get; set; }
public virtual string Copyright { get; set; }
public virtual string DominantColorBackground { get; set; }
public virtual string DominantColorForeground { get; set; }
[BackingType(typeof(PropertyStringList))]
public virtual string[] DominantColors { get; set; }
[BackingType(typeof(PropertyStringList))]
public virtual string[] ImageCategories { get; set; }
public virtual bool IsAdultContent { get; set; }
public virtual bool IsBwImg { get; set; }
public virtual bool IsRacyContent { get; set; }
public virtual string LineDrawingType { get; set; }
[BackingType(typeof(PropertyStringList))]
public virtual string[] Tags { get; set; }
}
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);
}
}
using Episerver.Playground10.Helpers;
using Episerver.Playground10.Models.Media;
using EPiServer;
using EPiServer.Core;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.Logging;
using EPiServer.ServiceLocation;
[InitializableModule]
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class ImageTaggingInitializationModule : IInitializableModule
{
private static readonly ILogger Logger = LogManager.GetLogger();
private static bool initialized;
private Injected<IContentEvents> ContentEvents { get; set; }
public void Initialize(InitializationEngine context)
{
if (context == null)
{
return;
}
if (initialized)
{
return;
}
Logger.Information("[ImageTagging] Initializing image tagging functionality.");
this.ContentEvents.Service.PublishedContent += OnPublishedContent;
initialized = true;
Logger.Information("[ImageTagging] Image tagging initialized.");
}
public void Uninitialize(InitializationEngine context)
{
if (context == null)
{
return;
}
if (!initialized)
{
return;
}
Logger.Information("[ImageTagging] Uninitializing image tagging functionality.");
this.ContentEvents.Service.PublishedContent -= OnPublishedContent;
Logger.Information("[ImageTagging] Image tagging uninitialized.");
}
private static void OnPublishedContent(object sender, ContentEventArgs contentEventArgs)
{
if (contentEventArgs == null)
{
return;
}
ImageFile contentData = contentEventArgs.Content as ImageFile;
if (contentData == null)
{
return;
}
ImageHelper imageHelper = new ImageHelper();
#pragma warning disable 4014
imageHelper.TagImagesAsync(contentData);
#pragma warning restore 4014
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment