private async Task <VisionHandwritingModel> SubmitRequest(VisionHandwritingRequest request) { Stopwatch sw = new Stopwatch(); //ocr/language=unk&detectOrientation=true string uri = $"{request.Url}/recognizeText?handwriting={request.Handwriting.ToString()}"; ServiceResultModel requestResult = null; if (request.IsUrlImageSource) { _log.LogTrace($"Submitting Vision Handwriting Request"); var urlRequest = new VisionUrlRequest { Url = request.ImageUrl }; var requestContent = JsonConvert.SerializeObject(urlRequest); var content = new StringContent(requestContent); sw.Start(); requestResult = await this._config.Client.PostAsync(uri, request.Key, content, ReturnType.String); sw.Stop(); _log.LogMetric("VisionHandwritingDurationMillisecond", sw.ElapsedMilliseconds); } else { using (ByteArrayContent content = new ByteArrayContent(request.ImageBytes)) { requestResult = await this._config.Client.PostAsync(uri, request.Key, content, ReturnType.String); } } if (requestResult.HttpStatusCode == (int)System.Net.HttpStatusCode.Accepted) { var operationLocation = string.Empty; operationLocation = requestResult.Headers.GetValues("Operation-Location").FirstOrDefault(); _log.LogTrace($"Handwriting Request Async Operation Url (Polling) : {operationLocation}"); VisionHandwritingModel result = await CheckForResult(operationLocation, request); return(result); } else if (requestResult.HttpStatusCode == (int)System.Net.HttpStatusCode.BadRequest) { VisionErrorModel error = JsonConvert.DeserializeObject <VisionErrorModel>(requestResult.Contents); var message = string.Format(VisionExceptionMessages.CognitiveServicesException, error.Code, error.Message); _log.LogWarning(message); throw new Exception(message); } else { var message = string.Format(VisionExceptionMessages.CognitiveServicesException, requestResult.HttpStatusCode, requestResult.Contents); _log.LogError(message); throw new Exception(message); } }
private async Task <VisionDescribeModel> SubmitRequest(VisionDescribeRequest request) { Stopwatch sw = new Stopwatch(); string uri = $"{request.Url}/describe?maxCandidates={request.MaxCandidates}"; ServiceResultModel requestResult = null; if (request.IsUrlImageSource) { _log.LogTrace($"Submitting Vision Describe Request"); var urlRequest = new VisionUrlRequest { Url = request.ImageUrl }; var requestContent = JsonConvert.SerializeObject(urlRequest); var content = new StringContent(requestContent); sw.Start(); requestResult = await this._config.Client.PostAsync(uri, request.Key, content, ReturnType.String); sw.Stop(); _log.LogMetric("VisionRequestDurationMillisecond", sw.ElapsedMilliseconds); } else { using (ByteArrayContent content = new ByteArrayContent(request.ImageBytes)) { requestResult = await this._config.Client.PostAsync(uri, request.Key, content, ReturnType.String); } } if (requestResult.HttpStatusCode == (int)System.Net.HttpStatusCode.OK) { _log.LogTrace($"Describe Request Results: {requestResult.Contents}"); VisionDescribeModel result = JsonConvert.DeserializeObject <VisionDescribeModel>(requestResult.Contents); return(result); } else if (requestResult.HttpStatusCode == (int)System.Net.HttpStatusCode.BadRequest) { VisionErrorModel error = JsonConvert.DeserializeObject <VisionErrorModel>(requestResult.Contents); var message = string.Format(VisionExceptionMessages.CognitiveServicesException, error.Code, error.Message); _log.LogWarning(message); throw new Exception(message); } else { var message = string.Format(VisionExceptionMessages.CognitiveServicesException, requestResult.HttpStatusCode, requestResult.Contents); _log.LogError(message); throw new Exception(message); } }
private async Task <Byte[]> SubmitRequest(VisionThumbnailRequest request) { Stopwatch sw = new Stopwatch(); string uri = $"{request.Url}/generateThumbnail?width={request.Width}&height={request.Height}&smartCropping={request.SmartCropping.ToString()}"; ServiceResultModel requestResult = null; if (request.IsUrlImageSource) { _log.LogTrace($"Submitting Vision Thumbnail Request"); var urlRequest = new VisionUrlRequest { Url = request.ImageUrl }; var requestContent = JsonConvert.SerializeObject(urlRequest); var content = new StringContent(requestContent); sw.Start(); requestResult = await this._config.Client.PostAsync(uri, request.Key, content, ReturnType.Binary); sw.Stop(); _log.LogMetric("VisionRequestDurationMillisecond", sw.ElapsedMilliseconds); } else { using (ByteArrayContent content = new ByteArrayContent(request.ImageBytes)) { requestResult = await this._config.Client.PostAsync(uri, request.Key, content, ReturnType.Binary); } } if (requestResult.HttpStatusCode == (int)System.Net.HttpStatusCode.OK) { _log.LogTrace($"Thumbnail Request Results"); byte[] fileResult = requestResult.Binary; return(fileResult); } else if (requestResult.HttpStatusCode == (int)System.Net.HttpStatusCode.BadRequest) { VisionErrorModel error = JsonConvert.DeserializeObject <VisionErrorModel>(requestResult.Contents); var message = string.Format(VisionExceptionMessages.CognitiveServicesException, error.Code, error.Message); _log.LogWarning(message); throw new Exception(message); } else { var message = string.Format(VisionExceptionMessages.CognitiveServicesException, requestResult.HttpStatusCode, requestResult.Contents); _log.LogError(message); throw new Exception(message); } }