-1

I am currently attempting to use C# to submit an image to my Custom Vision Prediction API. I am new to C# and I am having some issues following the tutorial available on the Microsoft Azure website (https://learn.microsoft.com/en-us/azure/cognitive-services/custom-vision-service/use-prediction-api#next-steps).

This tutorial ends with the program returning a JSON-formatted string using this code:

byte[] byteData = GetImageAsByteArray(imageFilePath);

using (var content = new ByteArrayContent(byteData))
{
    content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
    response = await client.PostAsync(url, content);
    Console.WriteLine(await response.Content.ReadAsStringAsync());
}

With an example of the output being:

{
  "Id": "7796df8e-acbc-45fc-90b4-1b0c81b73639",
  "Project": "8622c779-471c-4b6e-842c-67a11deffd7b",
  "Iteration": "59ec199d-f3fb-443a-b708-4bca79e1b7f7",
  "Created": "2019-03-20T16:47:31.322Z",
  "Predictions": [
    {
      "TagId": "d9cb3fa5-1ff3-4e98-8d47-2ef42d7fb373",
      "TagName": "cat",
      "Probability": 1
    },
    {
      "TagId": "9a8d63fb-b6ed-4462-bcff-77ff72084d99",
      "TagName": "dog",
      "Probability": 0.1087869
    }
  ]
}

Is it possible to change the code so that it only returns the predictions part of the array? I am aware that there is going to be a solution, but as I mentioned earlier I am very new to C#, have never used JSON before and I haven't managed to find anything online to help solve my problem - but if someone already has, please let me know!

I hope you will be able to help me solve this problem!

Thanks,

RamblinRose
  • 4,883
  • 2
  • 21
  • 33
  • You would have to modify the server code. If your client code is C#, why not just pluck the `Predictions` value from the deserialized JSON response? – RamblinRose Mar 19 '20 at 10:41
  • 1
    Create class which represent the json structure, parse json to q object of the class and return prediction property of the object. Use NewtonSoft.Json to parse json to class object. – Chetan Mar 19 '20 at 10:43
  • Having a bit more context around this would really help. Presumably you are going to do something with the string that contains the `predictions`? What are you going to use that string for once you have it? Are you planning to parse it to pull out more information? Depending on what the overall goal is the answer may change. – steeveeet Mar 19 '20 at 11:03

1 Answers1

1

This example you show is just the client. Ideally you need to adapt the server code to just output what you require, as this is also more efficient.

But if you can't do this, then luckily for you JSON converters tend to ignore any data that they don't require. So then you can deserialize to a simplified class.

So define your classes as required. Such as;

public class Prediction
{
    public string TagId { get; set; }
    public string TagName { get; set; }
    public double Probability { get; set; }
}

public class RootObject
{
    public List<Prediction> Predictions { get; set; }
}

Then deserialize accordingly. Such as with Newtonsoft

RootObject rootObj = JsonConvert.DeserializeObject<RootObject>(json);
jason.kaisersmith
  • 8,712
  • 3
  • 29
  • 51