Microsoft Cognitive Api on Azure
Cognitive Services
Last time I talked about one way you can sign up and use Microsoft’s Cognitive services. I also learned that you can use the azure portal to get started as well. To get started you will need an Azure account and will go to portal.azure.com.
Start by clicking the new button on the top left. Then click on Intelligence + Analytics and then on the right click Cognitive Services Api Then you need to fill out a few things and click Create. I like to pin stuff to the dashboard so I can easily find it again. To use the Api we will need to know what our Api key is. This can be found by clicking Keys. Since I will be using C# I used this Quick Start guide to get started.
Last time I used a Nuget package that made it really easy to use the Computer Vision Api. This time I decided to use HttpClient
similar to what the quick start guide shows you.
I made a little WPF app that makes use of the Vision api. It asks you for an image and then posts that data as binary.
var filePicker = new OpenFileDialog();
filePicker.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";
var result = filePicker.ShowDialog();
var imageFile = filePicker.FileName;
if (!File.Exists(imageFile)) return;
var binary = GetBytesFromPath(imageFile);
using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "yourAPIKey");
var requestParameters = "visualFeatures=Description&language=en";
var uri = "https://westus.api.cognitive.microsoft.com/vision/v1.0/analyze?" + requestParameters;
using (var content = new ByteArrayContent(binary))
{
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
var response = await httpClient.PostAsync(uri, content);
var json = await response.Content.ReadAsStringAsync();
txtResults.Text = json;
}
}
In the requestParameters
I say visualFeatures=Description. You can learn more about the other options that you can use here.
In Azure I can take a look at how many calls I have made and errors that happened which is cool.
Hopefully I made it clear how easy it is to set this up in Azure. I know I only scratched the surface on what you all can do with the Cognitive Services.
Have you used Microsoft’s Cognitive Services? If so what did you think of them?