Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
In this quickstart, you will analyze both a local and a remote image to extract visual features using the Computer Vision client library for C#. If you wish, you can download the code in this guide as a complete sample app from the Cognitive Services Csharp Vision repo on GitHub.
Prerequisites
- A Computer Vision subscription key. You can follow the instructions in Create a Cognitive Services account to subscribe to Computer Vision and get your key.
- Any edition of Visual Studio 2015 or 2017.
- The Microsoft.Azure.CognitiveServices.Vision.ComputerVision client library NuGet package. It isn't necessary to download the package. Installation instructions are provided below.
Create and run the sample application
To run the sample, do the following steps:
Create a new Visual C# Console App in Visual Studio.
Install the Computer Vision client library NuGet package.
- On the menu, click Tools, select NuGet Package Manager, then Manage NuGet Packages for Solution.
- Click the Browse tab, and in the Search box type "Microsoft.Azure.CognitiveServices.Vision.ComputerVision".
- Select Microsoft.Azure.CognitiveServices.Vision.ComputerVision when it displays, then click the checkbox next to your project name, and Install.
Replace the contents of Program.cs with the following code. The
AnalyzeImageAsync
andAnalyzeImageInStreamAsync
methods wrap the Analyze Image REST API for remote and local images, respectively.using Microsoft.Azure.CognitiveServices.Vision.ComputerVision; using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models; using System; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; namespace ImageAnalyze { class Program { // subscriptionKey = "0123456789abcdef0123456789ABCDEF" private const string subscriptionKey = "<SubscriptionKey>"; // localImagePath = @"C:\Documents\LocalImage.jpg" private const string localImagePath = @"<LocalImage>"; private const string remoteImageUrl = "https://upload.wikimedia.org/wikipedia/commons/3/3c/Shaki_waterfall.jpg"; // Specify the features to return private static readonly List<VisualFeatureTypes> features = new List<VisualFeatureTypes>() { VisualFeatureTypes.Categories, VisualFeatureTypes.Description, VisualFeatureTypes.Faces, VisualFeatureTypes.ImageType, VisualFeatureTypes.Tags }; static void Main(string[] args) { ComputerVisionClient computerVision = new ComputerVisionClient( new ApiKeyServiceClientCredentials(subscriptionKey), new System.Net.Http.DelegatingHandler[] { }); computerVision.Endpoint = "https://api.cognitive.azure.cn"; Console.WriteLine("Images being analyzed ..."); var t1 = AnalyzeRemoteAsync(computerVision, remoteImageUrl); var t2 = AnalyzeLocalAsync(computerVision, localImagePath); Task.WhenAll(t1, t2).Wait(5000); Console.WriteLine("Press ENTER to exit"); Console.ReadLine(); } // Analyze a remote image private static async Task AnalyzeRemoteAsync( ComputerVisionClient computerVision, string imageUrl) { if (!Uri.IsWellFormedUriString(imageUrl, UriKind.Absolute)) { Console.WriteLine( "\nInvalid remoteImageUrl:\n{0} \n", imageUrl); return; } ImageAnalysis analysis = await computerVision.AnalyzeImageAsync(imageUrl, features); DisplayResults(analysis, imageUrl); } // Analyze a local image private static async Task AnalyzeLocalAsync( ComputerVisionClient computerVision, string imagePath) { if (!File.Exists(imagePath)) { Console.WriteLine( "\nUnable to open or read localImagePath:\n{0} \n", imagePath); return; } using (Stream imageStream = File.OpenRead(imagePath)) { ImageAnalysis analysis = await computerVision.AnalyzeImageInStreamAsync( imageStream, features); DisplayResults(analysis, imagePath); } } // Display the most relevant caption for the image private static void DisplayResults(ImageAnalysis analysis, string imageUri) { Console.WriteLine(imageUri); if (analysis.Description.Captions.Count != 0) { Console.WriteLine(analysis.Description.Captions[0].Text + "\n"); } else { Console.WriteLine("No description generated."); } } } }
Replace
<Subscription Key>
with your valid subscription key.Change
computerVision.Endpoint
to the Azure region associated with your subscription keys, if necessary.Replace
<LocalImage>
with the path and file name of a local image.Optionally, set
remoteImageUrl
to a different image URL.Run the program.
Examine the response
A successful response displays the most relevant caption for each image. You can change the DisplayResults
method to output different image data. See the AnalyzeLocalAsync method to learn more.
https://upload.wikimedia.org/wikipedia/commons/3/3c/Shaki_waterfall.jpg
a large waterfall over a rocky cliff
Next steps
Explore the Computer Vision APIs used to analyze an image, detect celebrities and landmarks, create a thumbnail, and extract printed and handwritten text.