public async Task <CustomVisionResponse> AnalyzeAsync(string imageUrl, IFormFile file, Guid projectId, string iterationPublishedName, CustomVisionProjectType projectType) { // Custom vision if (!string.IsNullOrWhiteSpace(imageUrl)) { var imageAnalysis = CustomVisionAnalyzeImageByUrlAsync(imageUrl, projectId, iterationPublishedName, projectType); var imageInfo = ImageInfoProcessor.GetImageInfoFromImageUrlAsync(imageUrl, _httpClientFactory); // Combine var task = Task.WhenAll(imageAnalysis, imageInfo); try { await task; return(new CustomVisionResponse { ImageInfo = imageInfo.Result, Predictions = imageAnalysis.Result.Predictions.ToList() }); } catch (CustomVisionErrorException ex) { var exceptionMessage = ex.Response.Content; var parsedJson = JToken.Parse(exceptionMessage); if (ex.Response.StatusCode == System.Net.HttpStatusCode.BadRequest) { return(new CustomVisionResponse { ApiRequestErrorMessage = $"Bad request thrown by the underlying API from Microsoft:", ApiRequestErrorContent = parsedJson.ToString(Formatting.Indented) }); } else { return(new CustomVisionResponse { OtherErrorMessage = $"Error thrown by the underlying API from Microsoft:", OtherErrorContent = parsedJson.ToString(Formatting.Indented) }); } } } else { using (var analyzeStream = new MemoryStream()) using (var outputStream = new MemoryStream()) { // Get initial value await file.CopyToAsync(analyzeStream); analyzeStream.Seek(0, SeekOrigin.Begin); await analyzeStream.CopyToAsync(outputStream); // Reset the stream for consumption analyzeStream.Seek(0, SeekOrigin.Begin); outputStream.Seek(0, SeekOrigin.Begin); try { var imageAnalysis = await CustomVisionAnalyzeImageByStreamAsync(analyzeStream, projectId, iterationPublishedName, projectType); var imageInfo = ImageInfoProcessor.GetImageInfoFromStream(outputStream, file.ContentType); return(new CustomVisionResponse { ImageInfo = imageInfo, Predictions = imageAnalysis.Predictions.ToList() }); } catch (CustomVisionErrorException ex) { var exceptionMessage = ex.Response.Content; var parsedJson = JToken.Parse(exceptionMessage); if (ex.Response.StatusCode == System.Net.HttpStatusCode.BadRequest) { return(new CustomVisionResponse { ApiRequestErrorMessage = $"Bad request thrown by the underlying API from Microsoft:", ApiRequestErrorContent = parsedJson.ToString(Formatting.Indented) }); } else { return(new CustomVisionResponse { OtherErrorMessage = $"Error thrown by the underlying API from Microsoft:", OtherErrorContent = parsedJson.ToString(Formatting.Indented) }); } } } } }
public async Task <FaceAnalyzeResponse> AnalyzeAsync(string imageUrl, IFormFile file, FaceDetectionModel detectionModel, bool enableIdentification, FaceRecognitionModel recognitionModel, FaceIdentificationGroupType identificationGroupType, string identificationGroupId) { // Face if (!string.IsNullOrWhiteSpace(imageUrl)) { var face = FaceProcessByUrlAsync(imageUrl, detectionModel, enableIdentification, recognitionModel, identificationGroupType, identificationGroupId); var imageInfo = ImageInfoProcessor.GetImageInfoFromImageUrlAsync(imageUrl, _httpClientFactory); // Combine var task = Task.WhenAll(face, imageInfo); try { await task; return(new FaceAnalyzeResponse { ImageInfo = imageInfo.Result, FaceResult = face.Result }); } catch (APIErrorException ex) { var exceptionMessage = ex.Response.Content; var parsedJson = JToken.Parse(exceptionMessage); if (ex.Response.StatusCode == System.Net.HttpStatusCode.BadRequest) { return(new FaceAnalyzeResponse { ApiRequestErrorMessage = $"Bad request thrown by the underlying API from Microsoft:", ApiRequestErrorContent = parsedJson.ToString(Formatting.Indented) }); } else { return(new FaceAnalyzeResponse { OtherErrorMessage = $"Error thrown by the underlying API from Microsoft:", OtherErrorContent = parsedJson.ToString(Formatting.Indented) }); } } } else { using (var analyzeStream = new MemoryStream()) using (var outputStream = new MemoryStream()) { // Get initial value await file.CopyToAsync(analyzeStream); analyzeStream.Seek(0, SeekOrigin.Begin); await analyzeStream.CopyToAsync(outputStream); // Reset the stream for consumption analyzeStream.Seek(0, SeekOrigin.Begin); outputStream.Seek(0, SeekOrigin.Begin); try { var face = await FaceProcessByStreamAsync(analyzeStream, detectionModel, enableIdentification, recognitionModel, identificationGroupType, identificationGroupId); var imageInfo = ImageInfoProcessor.GetImageInfoFromStream(outputStream, file.ContentType); return(new FaceAnalyzeResponse { ImageInfo = imageInfo, FaceResult = face }); } catch (APIErrorException ex) { var exceptionMessage = ex.Response.Content; var parsedJson = JToken.Parse(exceptionMessage); if (ex.Response.StatusCode == System.Net.HttpStatusCode.BadRequest) { return(new FaceAnalyzeResponse { ApiRequestErrorMessage = $"Bad request thrown by the underlying API from Microsoft:", ApiRequestErrorContent = parsedJson.ToString(Formatting.Indented) }); } else { return(new FaceAnalyzeResponse { OtherErrorMessage = $"Error thrown by the underlying API from Microsoft:", OtherErrorContent = parsedJson.ToString(Formatting.Indented) }); } } } } }