
Hi!
On my Custom Vision samples, I usually send an image to a CustomVision.ai HTTP Endpoint, and I process the Json result. The results are very easy to understand, however, I created a C# converter class to help with the Custom Vision results.
To create this class I navigate: http://json2csharp.com/, and paste a sample result and make some changes on the result. The output and useful class is this one:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Globalization; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Converters; | |
// created with https://app.quicktype.io/#l=cs&r= | |
namespace DistractedDriverDetectionShared | |
{ | |
public partial class CustomVisionEstimation | |
{ | |
[JsonProperty("created")] | |
public DateTimeOffset Created { get; set; } | |
[JsonProperty("id")] | |
public string Id { get; set; } | |
[JsonProperty("iteration")] | |
public string Iteration { get; set; } | |
[JsonProperty("predictions")] | |
public Prediction[] Predictions { get; set; } | |
[JsonProperty("project")] | |
public string Project { get; set; } | |
} | |
public partial class Prediction | |
{ | |
[JsonProperty("boundingBox")] | |
public object BoundingBox { get; set; } | |
[JsonProperty("probability")] | |
public double Probability { get; set; } | |
[JsonProperty("tagId")] | |
public string TagId { get; set; } | |
[JsonProperty("tagName")] | |
public string TagName { get; set; } | |
} | |
public partial class CustomVisionEstimation | |
{ | |
public static CustomVisionEstimation FromJson(string json) => JsonConvert.DeserializeObject<CustomVisionEstimation>(json, Converter.Settings); | |
} | |
public static class Serialize | |
{ | |
public static string ToJson(this CustomVisionEstimation self) => JsonConvert.SerializeObject(self, Converter.Settings); | |
} | |
internal static class Converter | |
{ | |
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings | |
{ | |
MetadataPropertyHandling = MetadataPropertyHandling.Ignore, | |
DateParseHandling = DateParseHandling.None, | |
Converters = | |
{ | |
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal } | |
}, | |
}; | |
} | |
} |
It’s a very simple class, and the best way to describe it, is to show an usage scenario
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// open file as byte array | |
var byteData = GetImageAsByteArray(imageFilePath); | |
// prediction | |
var client = new HttpClient(); | |
using var content = new ByteArrayContent(byteData); | |
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); | |
var response = await client.PostAsync(Url, content); | |
var jsonResponse = await response.Content.ReadAsStringAsync(); | |
// Display results | |
var est = CustomVisionEstimation.FromJson(jsonResponse); | |
var prediction = est.Predictions.OrderByDescending(x => x.Probability).FirstOrDefault(); | |
Console.WriteLine(@$"{prediction.TagName} – {prediction.Probability}"); |
The main remarks points are
- Lines 1 to 9, open a local file, create a HTTP client and make the HTTP Post request
- Line 12, convert the json response (string) to a C# object and then get the best prediction
- Where the best prediction is a single Linq code sorting the predictions by probability and selecting the 1t one.

Easy and amazing!
Happy Coding!
Greetings @ Burlington
El Bruno
References
- Custom Vision, CustomVision.ai
- Json2CSharp, http://json2csharp.com/
Advertisement
2 comments