Skip to content

Instantly share code, notes, and snippets.

@elbruno
Created January 20, 2019 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elbruno/f562382e0b9d9e4cca4a189bc08951e8 to your computer and use it in GitHub Desktop.
Save elbruno/f562382e0b9d9e4cca4a189bc08951e8 to your computer and use it in GitHub Desktop.
CVConsoleApp.cs
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace CustomVisionMarvelConsole01
{
static class Program
{
static void Main()
{
MakePredictionRequest("IMG01.jpg").Wait();
Console.ReadLine();
}
static async Task MakePredictionRequest(string imageFilePath)
{
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Prediction-Key", "<Custom Vision Prediction Key>");
var url = "https://southcentralus.api.cognitive.microsoft.com/customvision/v2.0/Prediction/<Custom Vision AppKey>/image?iterationId=<Custom Vision IterationId>";
var byteData = GetImageAsByteArray(imageFilePath);
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();
var prettyJson = JToken.Parse(jsonResponse).ToString(Formatting.Indented);
Console.WriteLine(prettyJson);
}
}
static byte[] GetImageAsByteArray(string imageFilePath)
{
var fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read);
var binaryReader = new BinaryReader(fileStream);
return binaryReader.ReadBytes((int)fileStream.Length);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment