Computer Vision AI In Simple Steps

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

  1. You need an Azure subscription.
  2. Your subscription should be enabled for cognitive services.
  3. 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.
  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.         DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.  
  4.         if (result == DialogResult.OK) // Test result.  
  5.         {  
  6.             string file = openFileDialog1.FileName;  
  7.             try {  
  8.                 Vision(file);  
  9.             } catch (IOException) {}  
  10.         }  
Then, create the function called vision and pass the file link to the function.
  1. private async void test(string filename)  
  2. {  
  3.     var visionClient = new VisionServiceClient("your subscription key copied earlier"); //Note this is not azure subscription key.  
  4.     AnalysisResult analysisResult;  
  5.     var features = new VisualFeature[] {  
  6.         VisualFeature.Tags, VisualFeature.Description  
  7.     };  
  8.     var fs = new FileStream(@filename, FileMode.Open);  
  9.     analysisResult = await visionClient.AnalyzeImageAsync(fs, features);  
  10.     fs.Dispose(); // Dispose the fs to use it again in picture box.  
  11.     pictureBox1.ImageLocation = filename;  
  12.     string json = JsonConvert.SerializeObject(analysisResult); //serelize the onject to json.  
  13.     textBox1.Text = json; //show json in textbox.  
  14. }  
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.