コード例 #1
0
ファイル: WebClassificator.cs プロジェクト: mrjslau/TOP2018
        /// <inheritdoc cref="ClassifyImage" />
        /// <summary>Same as <see cref="ClassifyImage"/> but performed asynchronously.</summary>
        public async Task <Dictionary <string, float> > ClassifyImageAsync(byte[] image)
        {
            var projectId     = Guid.Parse(ConfigurationManager.AppSettings.Get("projectId"));
            var predictionKey = ConfigurationManager.AppSettings.Get("predictionKey");

            // Form request
            var client = new HttpClient();

            // Request headers
            client.DefaultRequestHeaders.Add("Prediction-Key", predictionKey);

            // Request parameters
            var uri =
                $"https://southcentralus.api.cognitive.microsoft.com/customvision/v2.0/Prediction/{projectId}/image";

            var multipartFormDataContent = GetMultipartFormDataContent(image);

            // Execute request
            var response = await client.PostAsync(uri, multipartFormDataContent);

            response.EnsureSuccessStatusCode();

            var results = CustomVisionPredictionResponse.FromJson(await response.Content.ReadAsStringAsync());

            return(results.Predictions.ToDictionary(x => x.TagName, x => (float)x.Probability));
        }
コード例 #2
0
        /// <inheritdoc cref="ClassifyImage" />
        /// <summary>Same as <see cref="ClassifyImage"/> but performed asynchronously.</summary>
        public async Task <Dictionary <string, float> > ClassifyImageAsync(byte[] image)
        {
            var projectId     = Guid.Parse(ConfigurationManager.AppSettings["cvProjectId"]);
            var predictionKey = ConfigurationManager.AppSettings["cvPredictionKey"];
            var uri           = ConfigurationManager.AppSettings["cvRequestUri"];

            // Form request
            var client = new HttpClient();

            // Request headers
            client.DefaultRequestHeaders.Add("Prediction-Key", predictionKey);

            var multipartFormDataContent = GetMultipartFormDataContent(image);

            // Execute request
            var response = await client.PostAsync(uri, multipartFormDataContent);

            response.EnsureSuccessStatusCode();

            var results = CustomVisionPredictionResponse.FromJson(await response.Content.ReadAsStringAsync());

            return(results.Predictions.ToDictionary(x => x.TagName, x => (float)x.Probability));
        }