public async Task <IEnumerable <LabelConfidence> > Tags(byte[] image, ComputerVisionSettings settings) { using (var client = new HttpClient()) { var apiKey = settings.ComputerVisionAPIKey; var apiUri = settings.ComputerVisionUri; if (String.IsNullOrEmpty(apiKey) || String.IsNullOrEmpty(apiUri)) { logger.LogError("Please provide apiKey and URI to the Machine Learning WebService"); return(null); } client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", apiKey); string apiUriTag = $"{apiUri}/tag"; using (ByteArrayContent requestContent = new ByteArrayContent(image)) { requestContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); var response = await client.PostAsync(apiUriTag, requestContent); string contentString = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { logger.LogError($"The request failed with status code: {response.StatusCode}"); logger.LogDebug(response.Headers.ToString()); logger.LogDebug(contentString); throw new Exception("Error in Cognitive Services response"); } var visionApiResponse = JsonConvert.DeserializeObject <VisionApiResponse>(contentString); var query = visionApiResponse.tags.AsQueryable(); if (query.Any(t => t.confidence > settings.Threshold)) { query = query .Where(t => t.confidence > settings.Threshold) .OrderByDescending(t => t.confidence) .Take(settings.MaxLength); } else { // In case we don't find any element matching threshold, we match only first one query = query .OrderByDescending(t => t.confidence) .Take(1); } return(query.Select(t => new LabelConfidence() { Label = t.name, Probability = (float)t.confidence })); } } }
public ComputerVisionPrediction(IOptionsSnapshot <AppSettings> settings, IComputerVisionClient modelManagementClient) { this.computerVisionClient = modelManagementClient; //TODO: move these settings to setting file computerVisionSettings = new ComputerVisionSettings { ComputerVisionAPIKey = settings.Value.ComputerVision?.VisionAPIKey, ComputerVisionUri = settings.Value.ComputerVision?.VisionUri, Threshold = 0.85f, MaxLength = 5 }; }