Azure Computer Vision Image Recognition using C# .NET
Post by: syed hussain in All Azure Cognitive Services C#
Summary
The code below uses Azure Cognitive Services Vision to classify images accordingly. The images are classified with a confidence score.
Microsoft provides three Vision APIs:
Service Name | Service Description |
---|---|
Computer Vision | The Computer Vision service provides you with access to advanced cognitive algorithms for processing images and returning information. |
Custom Vision | The Custom Vision Service lets you build, deploy, and improve your own image classifiers. An image classifier is an AI service that applies labels to images, based on their visual characteristics. |
Face | The Face service provides access to advanced face algorithms, enabling face attribute detection and recognition. |
This architecture will use Computer Vision. Azure Custom Vision is an image recognition service that lets you build, deploy, and improve your own image identifier models. An image identifier applies labels to images, according to their detected visual characteristics.
Architecture
- Azure Computer Vision.
- Azure Blob Storage.
The code below requires a Computer Vision resource created. Once created save the Key and the Endpoint details.
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models;
using Serilog;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading.Tasks;
namespace ComputerVision
{
public class Process
{
// Subscription Key & Endpoint
private static readonly string subscriptionKey = "{KEY}";
private static readonly string endpoint = "{ENDPOINT}";
// Run Process
public static void Run(string filename)
{
string imageUrl = $"https://{BLOBSTORAGE}.blob.core.windows.net/{container}/{filename}";
// Create a client
ComputerVisionClient client = Authenticate(endpoint, subscriptionKey);
// Analyze an image to get features and other properties.
AnalyzeImageUrl(client, imageUrl).Wait();
}
public static ComputerVisionClient Authenticate(string endpoint, string key)
{
ComputerVisionClient client =
new ComputerVisionClient(new ApiKeyServiceClientCredentials(key))
{ Endpoint = endpoint };
return client;
}
public static async Task<ImageAnalysisViewModel> AnalyzeImageUrl(ComputerVisionClient client, string imageUrl)
{
try
{
// Download image locally
using (WebClient webClient = new WebClient())
{
webClient.DownloadFile(imageUrl, "image.png");
}
// Create the Vision Taxonomy
List<VisualFeatureTypes?> features = new List<VisualFeatureTypes?>() {
VisualFeatureTypes.Categories, VisualFeatureTypes.Description,
VisualFeatureTypes.Faces, VisualFeatureTypes.ImageType,
VisualFeatureTypes.Tags, VisualFeatureTypes.Adult,
VisualFeatureTypes.Color, VisualFeatureTypes.Brands,
VisualFeatureTypes.Objects
};
ImageAnalysis results;
// Process the downloaded file
using (Stream imageStream = File.OpenRead("image.png"))
{
results = await client.AnalyzeImageInStreamAsync(imageStream, visualFeatures: features);
imageStream.Close();
}
// Create a new Image Analysis Model
ImageAnalysisViewModel imageAnalysis = new ImageAnalysisViewModel
{
ImageAnalysisResult = results
};
// Search and apply appropriate tags
foreach (var result in imageAnalysis.ImageAnalysisResult.Tags)
{
string tag = result.Name;
string hint = result.Hint;
string confScore = result.Confidence.ToString();
}
return imageAnalysis;
}
catch (Exception e)
{
Log.Logger.Error($"{e}");
}
return null;
}
public class ImageAnalysisViewModel
{
public ImageAnalysis ImageAnalysisResult { get; set; }
}
}
}
GitHub code is available here: https://github.com/sy-huss/Azure-Function-ImageProcessor