-1

The following web-page provides example code for using a predictive endpoint using c#

https://learn.microsoft.com/en-us/azure/cognitive-services/custom-vision-service/use-prediction-api

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

namespace CVSPredictionSample
{
    public static class Program
    {
        public static void Main()
        {
            Console.Write("Enter image file path: ");
            string imageFilePath = Console.ReadLine();

            MakePredictionRequest(imageFilePath).Wait();

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

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

            // Request headers - replace this example key with your valid Prediction-Key.
            client.DefaultRequestHeaders.Add("Prediction-Key", "<Your prediction key>");

            // Prediction URL - replace this example URL with your valid Prediction URL.
            string url = "<Your prediction 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/octet-stream");
                response = await client.PostAsync(url, content);
                Console.WriteLine(await response.Content.ReadAsStringAsync());
            }
        }

        private 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);
        }
    }
}

How can it be done using Python code ?

Many thanks

Robert
  • 33
  • 8

1 Answers1

0

A similar documentation exists for Python:

  • For classification projects here
  • For object detection projects here

See language selector in the page: example

Nicolas R
  • 13,812
  • 2
  • 28
  • 57
  • Thank you for helpfully pointing to other Microsoft web-pages, but unfortunately they only provide example code in C#. My question is ... how can it be done using Python ? Many thanks for your help with Python code. – Robert Jan 18 '21 at 15:11
  • 1
    Open the page, the first thing displayed is a language selector with Python available. Sorry but the link does not keep this selection – Nicolas R Jan 18 '21 at 15:14
  • Indeed ! I did not see that page and perhaps got lost in the many many other Microsoft documentation web pages. Thanks for helping on this. My question has been answered by your helpful reply. – Robert Jan 19 '21 at 15:04