A union of curiosity and data science

Knowledgebase and brain dump of a database engineer


Microsoft Cognitive Services Playground - Computer Vision VS2015

Starting out with MS cognitive services vision API. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.ProjectOxford.Vision;
using Microsoft.ProjectOxford.Vision.Contract;
using System.Threading.Tasks;
using System.IO;



namespace cognotest
{
    public partial class _default : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterAsyncTask(new PageAsyncTask(getTagsForImageUpload));
        }


        private async Task getTagsForImageUpload()
        {
            //set image path. 
            string myFilePath = @"<your local image path>";

            //instantiate vision client, getTags by passing in file. 
            VisionServiceClient visionClient = new VisionServiceClient("<your service api key>");
            AnalysisResult ar = await visionClient.GetTagsAsync(File.OpenRead(myFilePath));


            //write the filesystem image to the browser. 
            FileStream file = File.OpenRead(myFilePath);
            byte[] buffer = new byte[file.Length];
            file.Read(buffer, 0, buffer.Length);
            file.Close();
            Response.Write("<img src =\"data:image/jpeg;base64," + Convert.ToBase64String(buffer) + "\" /> <br />");


            //get the categories
            foreach (var i in ar.Tags)
            {
                Response.Write("Tag: " + i.Name + ",    Confidence: " + i.Confidence + " <br />");
            }//end foreach

        }
    }
}



 

 

 

Result:

 

To implement the code above , follow the steps below. 

https://www.microsoft.com/cognitive-services/en-us/sign-up

Click - Let's Go

 

Allow MS cognitive services to access your info. 

Request new trials - check all that apply. 

Product Name - Description

Academic - Preview 10,000 transactions per month, 3 per second for interpret, 1 per second for evaluate, 6 per minute for calcHistogram.
Computer Vision - Preview 5,000 transactions per month, 20 per minute.
Emotion - Preview 30,000 transactions per month, 20 per minute.
Entity Linking - Preview 1000 transactions per day, 10KB text limit.
Face - Preview 30,000 transactions per month, 20 per minute.
Linguistic Analysis - Preview 5,000 transactions per month, 2 per second.
Speaker Recognition - Preview 10,000 transactions per month, 20 per minute.
Speech - Preview 5,000 transactions per month, 20 per minute for each feature for a total of 60 per minute.
Video - Preview getResult: 1,200 transactions per month, 1 per minute. getOperation: 12,000 transactions per month, 5 per minute Each feature has 100MB file limit; 300 transactions per month per feature, 1 per minute.
WebLM - Preview 100,000 transactions per month, 1,000 per minute.
Recommendations - Preview 10,000 transactions per month.
Text Analytics - Preview 5,000 transactions per month.
Bing Autosuggest - Free 10,000 transactions per month, 10 per second.
Bing Search - Free Across all Bing Search APIs (Web, Image, Video, News): 1,000 transactions per month, 5 per second.
Bing Spell Check - Free 5,000 transactions per month, 7 per minute.

 

Make sure to check "Computer Vision". Once you hit submit you will be provided with API keys and redirected here: https://www.microsoft.com/cognitive-services/en-us/subscriptions

 

 

Let's get some vision api hello world stuff going. 

Crack open Visual Studio 2015.

Start a new Project. Ours will be an empty web application

Right click the solution and select manage NuGet packages for solution

Search for : microsoft.projectoxford.vision

Select your project and hit install. 

Add a new web form to the project. name it whatever you like. 

Set your form as the start page. 

Add the Async="true" directive to the page.

 

Find an image on your machine.
The image for the API must be smaller than 4MB and it' must be a JPEG, PNG, GIF or BMP.
Lindsey Stirling will be my image for this test. C:\Data\2016-09-07_2128.png
Use the image path and key you've acquired in the code at the top of the page. 
Run it.