Introduction
Microsoft Cognitive Services API offers a wide range of APIs and algorithms around which you can build your applications. The algorithms include facial recognition, speech API, Computer Vision, spelling app, Emotion API, Video API, Linguistics API etc. The APIs are based on five main categories, vision, speech, search, language, and knowledge.
Using these APIs, you can bring powerful AI into your apps. The whole idea of cognitive services is being able to integrate powerful AI and machine learning.
Getting started
- You need an Azure subscription.
- Your subscription should be enabled for cognitive services.
- Of course, Visual Studio.
We will basically create a cognitive service API in Azure, then use Visual Studio to leverage the best of the API.
Let’s Begin
Select Data + Analytics, then Cognitive Services APIs (Preview), then supply details for the Cognitive service.
Provide a generic account name.
Choose your subscription.
Then, choose the API Type.
There are many APIs available. I am choosing Computer Vision API in the demo.
Then, choose your location, pricing tier, Resource Group.
Agre to the legal terms and click Create.
Once you click Create, just wait for a few minutes and your Cognitive service will be ready to serve your application. After the service is ready, you have to go to https://www.microsoft.com/cognitive-services and click on My Account.
From here, you can access many keys for many other services too. Click on show and copy the key.
Now, let’s move on to Visual Studio and start writing code. The following code is based on the original documentation from computer Vision API.
First, create a filepicker and write the following code for the click event of the button.
- private void button1_Click(object sender, EventArgs e)
- {
- DialogResult result = openFileDialog1.ShowDialog();
- if (result == DialogResult.OK)
- {
- string file = openFileDialog1.FileName;
- try {
- Vision(file);
- } catch (IOException) {}
- }
Then, create the function called vision and pass the file link to the function.
- private async void test(string filename)
- {
- var visionClient = new VisionServiceClient("your subscription key copied earlier");
- AnalysisResult analysisResult;
- var features = new VisualFeature[] {
- VisualFeature.Tags, VisualFeature.Description
- };
- var fs = new FileStream(@filename, FileMode.Open);
- analysisResult = await visionClient.AnalyzeImageAsync(fs, features);
- fs.Dispose();
- pictureBox1.ImageLocation = filename;
- string json = JsonConvert.SerializeObject(analysisResult);
- textBox1.Text = json;
- }
The final product should be able to upload a file and download the object returned by Vision API, and it should look something like below.
This is a very basic demo of Vision API but the possibilities are unlimited. I request that you explore more around Vision API. There is a lot you can do with Vision API and other cognitive services.