Cognitive Services: Custom Vision API (Part 2) – Create a web application to get predictions with the classification model

In part 1 of this article, I have created the image classification model for the application using the Custom Vision web interface. Now let’s consume the API and predict results.

I’m going to create an ASP.NET core 2.1 MVC web application which can predict the type of the dog for a given image URL or image.  Before that, for your references my environment configuration as below,

  • Visual Studio Enterprise 2017, version 15.8.6
  • Target framework: .NET core 2.1

Step 1 – Create a project

Create a new project with ASP.Net core web application template. Clean and rebuild the project.

CustomVisionPrediction_7

Step 2 – Install NuGet packages

Add below Custom Vision Nuget Packages to your application via NuGet Package Manager console.

For Prediction: Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction

CustomVisionPrediction_3

For Training: Microsoft.Azure.CognitiveServices.Vision.CustomVision.Training

CustomVisionPrediction_2

Step 3 – Project Id, Prediction key, Default iteration and Azure

Again login into your project in custom vision and go to settings to obtain your Project Id, Training key and Prediction key. Those keys are help to obtain results from the API.

CustomVisionPrediction_6

If you don’t have any Azure account associated with your outlook account, there are some limitation as below,

  • Only 10000 predictions can be made until reset.
  • Images can tag 50 times only.
  • 10 training iterations only.
  • Only 5000 images allowed.

Do not forget to mark any iteration as Default Iteration, Otherwise, you are not able to get any prediction result and end up with a Not found exception when trying to call for results. To make the iteration default, go to the Performance tab and select the iteration and click Make Default.

CustomVisionPrediction_8

Step 3 –  Pass image URL to predict

Create a static class called Configuration to store the project keys and other configuration details.

using System; namespace CustomVisionSample1.Models {     public static class Configuration     {         // Base url of the endpoint to connect to Custom Vision API         public static string CustomVisionEndpoint { get; set; } = "https://southcentralus.api.cognitive.microsoft.com";         // Project key of the Custom Vision project         public static Guid ProjectKey { get; set; } = Guid.Parse("<Your project key>");         // Prediction key of the image classification model         public static string PredictKey { get; set; } = "<Your prediction key>";         // Training key to train the model         public static string TrainKey { get; set; } = "<Your training key>";     } }

Then create a new class PredictVision and add below code there.

using Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction;
using Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.Models;
using System.IO; namespace CustomVisionSample1.Models {  public class PredictVision    {      private CustomVisionPredictionClient customVisionPredictionClient { get; set; }      public PredictVision(){
         // create a production client with prediction key and endpoint          customVisionPredictionClient = new CustomVisionPredictionClient(){               ApiKey = Configuration.PredictKey,               Endpoint = Configuration.CustomVisionEndpoint          };       }      // Predict results for the given image url      public ImagePrediction PredictByUrl(string imagePath) {          if (string.IsNullOrWhiteSpace(imagePath)) return null;
          // call PredictImageUrl() method with the project id and the url
          // of the image which you want to classify.          return customVisionPredictionClient.PredictImageUrl(Configuration.ProjectKey, new ImageUrl() { Url = imagePath });      }       // Predict results for the given image           public ImagePrediction PredictByImage(Stream imageStream){           if (imageStream == null) return null;
           // call PredictImage() method with project id and the image stream
           // to classify according to your custom vision model           return customVisionPredictionClient.PredictImage(Configuration.ProjectKey, imageStream);       }     } }

CustomVisionPrediction_10

Now you are ready to pass URL of an image or stream of an image to your classification model in custom vision. Finally, you will return the results that consist of the probability for all the tags for the given image.  Among those probabilities, you can choose the highest value to mark it as the result.

Next article I’m going to talk about how we can extend this same application to train and add more images with the tags for more accuracy of our model.

Happy coding! Cheers!

 

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s