Last active
January 12, 2020 18:01
-
-
Save chrisobriensp/4f35dc41940e65bfe9854b2be693b3ad to your computer and use it in GitHub Desktop.
Basic sample to call the Azure Cognitive Services Vision API to obtain tags, objects, a description and more from an image.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision; | |
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models; | |
using System.Threading.Tasks; | |
using System.IO; | |
namespace COB.Azure.ComputerVision | |
{ | |
class Program | |
{ | |
private static string SUBSCRIPTION_KEY = "3f51127f19b84396b55c376ef61fbfb9"; | |
private static string COGNITIVE_SERVICES_ENDPOINT = "https://uksouth.api.cognitive.microsoft.com/"; | |
private static string IMAGE_URL = "https://cobgenstorage.blob.core.windows.net/general/COB_Future_Decoded_2019.jpg"; | |
public static ComputerVisionClient Authenticate(string endpoint, string key) | |
{ | |
ComputerVisionClient client = new ComputerVisionClient(new ApiKeyServiceClientCredentials(key)) | |
{ | |
Endpoint = endpoint | |
}; | |
return client; | |
} | |
public static async Task AnalyzeImageUrl(ComputerVisionClient client, string imageUrl) | |
{ | |
Console.WriteLine("----------------------------------------------------------"); | |
Console.WriteLine("ANALYZE IMAGE - URL"); | |
Console.WriteLine(); | |
// specify all the types of analysis we want to perform.. | |
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 = await client.AnalyzeImageAsync(imageUrl, features); | |
// image tags and their confidence score.. | |
Console.WriteLine("Tags:"); | |
foreach (var tag in results.Tags) | |
{ | |
Console.WriteLine($"{tag.Name} {tag.Confidence}"); | |
} | |
Console.WriteLine(); | |
// objects detected.. | |
Console.WriteLine("Objects:"); | |
foreach (var obj in results.Objects) | |
{ | |
Console.WriteLine($"{obj.ObjectProperty} with confidence {obj.Confidence} at location {obj.Rectangle.X}, " + | |
$"{obj.Rectangle.X + obj.Rectangle.W}, {obj.Rectangle.Y}, {obj.Rectangle.Y + obj.Rectangle.H}"); | |
} | |
} | |
static async Task Main(string[] args) | |
{ | |
ComputerVisionClient client = Authenticate(COGNITIVE_SERVICES_ENDPOINT, SUBSCRIPTION_KEY); | |
Console.WriteLine($"Analyzing image {Path.GetFileName(IMAGE_URL)}..."); | |
Console.WriteLine(); | |
await AnalyzeImageUrl(client, IMAGE_URL); | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment