Image detection using custom vision AI

Custom vision AI is an interesting way to detect images. Just upload your image, train and then evaluate. To know more, please visit :
1. A subscription keys. To get a subscription key go to this link: Obtaining Subscription Keys.
2. Need an IDE for example: Visual Studio 2017
Step to create a sample application:
3. Open visual studio and create a console application
4. Replace content of the Program.cs file by the following code.

using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace CSPredictionSample
    static class Program
        static void Main()
            Console.Write("Enter image file path: ");
            string imageFilePath = @"D:\Projects\Github\ms-cognitive\CustomVision\Images\Test\Mahedee.JPG";


            //Predict uploaded image

            Console.WriteLine("\n\n\nHit ENTER to exit...");

        //Convert image as byte stream
        static byte[] GetImageAsByteArray(string imageFilePath)
            FileStream fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read);
            BinaryReader binaryReader = new BinaryReader(fileStream);
            return binaryReader.ReadBytes((int)fileStream.Length);

        static async Task MakePredictionRequest(string imageFilePath)
            var client = new HttpClient();

            //To get prediction key, url and content type, please go to
            //Project -> Performance -> Prediction URL
            //Request headers - replace this example key with your valid subscription key.

            client.DefaultRequestHeaders.Add("Prediction-Key", "Enter your Subscription key here");
            //client.DefaultRequestHeaders.Add("Prediction-Key", "71c950638c3c4f34be320cc952174ba7");

            // Prediction URL - replace this example URL with your valid prediction URL.
            string url = "";
            //string url = "";

            HttpResponseMessage response;

            // Request body. Try this sample with a locally stored image.
            byte[] byteData = GetImageAsByteArray(imageFilePath);

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

5. Replace your subscription key
6. Replace your image path
Now you will see the following output of the given image.

Leave a Reply

Your email address will not be published. Required fields are marked *