Integrate Microsoft Azure Cognitive Services Custom Vision Prediction API in C#
- Create a new project in Visual Studio. For this example, choose a Console App (.NET Core) to clearly understand the integration without any UI complexities.
- Install the necessary NuGet packages required for making HTTP requests. Use the HttpClient class for this purpose. You can install these using the Package Manager Console:
Install-Package Microsoft.Extensions.Configuration
Install-Package Microsoft.Extensions.Configuration.Json
- Include namespaces in your Program.cs file. At the beginning of the file, ensure you have the proper namespaces:
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
- Load Configuration to handle sensitive information like API endpoints and keys externally:
//appsettings.json
{
"PredictionEndpoint": "your_prediction_endpoint",
"PredictionKey": "your_prediction_key",
"ProjectId": "your_project_id",
"PublishedName": "your_published_name"
}
//In your Program.cs
IConfiguration config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();
- Create a method that communicates with the Custom Vision Prediction API:
static async Task PredictImageUrlAsync(IConfiguration config, string imageUrl)
{
var client = new HttpClient();
var endpoint = config["PredictionEndpoint"];
var predictionKey = config["PredictionKey"];
var projectId = config["ProjectId"];
var publishedName = config["PublishedName"];
client.DefaultRequestHeaders.Add("Prediction-Key", predictionKey);
string url = $"{endpoint}/{projectId}/url?iterationId={publishedName}";
var content = new StringContent($"{{\"Url\": \"{imageUrl}\"}}", System.Text.Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync(url, content);
if (response.IsSuccessStatusCode)
{
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
else
{
Console.WriteLine($"Error: {response.StatusCode}");
}
}
- Implement the Main method to invoke image prediction:
async Task Main(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();
string imageUrl = "https://example.com/image.jpg"; // Replace with the actual image URL
await PredictImageUrlAsync(config, imageUrl);
}
- Run the application to predict the given image URL using Azure Custom Vision Prediction API.
Handling Responses
- To properly handle responses, consider deserializing the JSON response into C# objects. Use libraries such as Newtonsoft.Json:
Install-Package Newtonsoft.Json
// Add using directives
using Newtonsoft.Json;
- Create a class structure to match the JSON response structure and use JsonConvert.DeserializeObject to parse it:
public class Prediction
{
public string TagName { get; set; }
public double Probability { get; set; }
}
public class PredictionResult
{
public List<Prediction> Predictions { get; set; }
}
static async Task ProcessResponseAsync(HttpResponseMessage response)
{
if(response.IsSuccessStatusCode)
{
var responseData = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<PredictionResult>(responseData);
foreach(var prediction in result.Predictions)
{
Console.WriteLine($"{prediction.TagName}: {prediction.Probability:P2}");
}
}
else
{
Console.WriteLine($"Error: {response.StatusCode}");
}
}