public async Task <Celebrity> RecognizeCelebrityAsync(Stream imgStream) { VisionResult result = await this.MakePostRequest(CelebrityEndpoint, imgStream); if (result?.result?.celebrities?.Length > 0) { return(result?.result?.celebrities[0]); } return(null); }
public async Task <Landmark> RecognizeLandmarkAsync(Stream imgStream) { VisionResult result = await this.MakePostRequest(LandmarkEndpoint, imgStream); if (result?.result?.landmarks?.Length > 0) { return(result?.result?.landmarks[0]); } return(null); }
private async Task <VisionResult> MakePostRequest(string uri, Stream imageStream) { try { using (HttpClient httpClient = httpClientFactory.CreateClient()) { using (StreamContent streamContent = new StreamContent(imageStream)) { streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); using (var request = new HttpRequestMessage(HttpMethod.Post, uri)) { request.Content = streamContent; request.Headers.Add("Ocp-Apim-Subscription-Key", Key1); using (HttpResponseMessage response = await httpClient.SendAsync(request)) { if (response.IsSuccessStatusCode) { string resultString = await response.Content.ReadAsStringAsync(); VisionResult result = JsonConvert.DeserializeObject <VisionResult>(resultString); return(result); } else { } } } return(null); } } } catch (Exception ex) { return(null); } }