Пример #1
0
        public static ComputerVisionViewModel Analyzed(ComputerVisionAnalyzeRequest request, ComputerVisionAnalyzeResponse response)
        {
            if (!string.IsNullOrWhiteSpace(response.ApiRequestErrorMessage) ||
                !string.IsNullOrWhiteSpace(response.ApiRequestErrorContent) ||
                !string.IsNullOrWhiteSpace(response.OtherErrorMessage) ||
                !string.IsNullOrWhiteSpace(response.OtherErrorContent))
            {
                return(new ComputerVisionViewModel
                {
                    IsAnalyzed = false,

                    ComputerVisionAnalyzeRequest = request,
                    ComputerVisionAnalyzeResponse = null,

                    ApiRequestErrorMessage = response.ApiRequestErrorMessage,
                    ApiRequestErrorContent = response.ApiRequestErrorContent,
                    OtherErrorMessage = response.OtherErrorMessage,
                    OtherErrorContent = response.OtherErrorContent
                });
            }

            return(new ComputerVisionViewModel
            {
                IsAnalyzed = true,

                ComputerVisionAnalyzeRequest = request,
                ComputerVisionAnalyzeResponse = response
            });
        }
        public async Task <ActionResult <ComputerVisionViewModel> > ComputerVision([FromForm] ComputerVisionAnalyzeRequest request)
        {
            var errorContent = "";

            if (string.IsNullOrWhiteSpace(request.ComputerVisionSubscriptionKey))
            {
                errorContent += $"Missing or invalid Computer Vision Subscription Key (see 'Azure Settings' tab){Environment.NewLine}";
            }

            if (string.IsNullOrWhiteSpace(request.ComputerVisionEndpoint))
            {
                errorContent += $"Missing or invalid Computer Vision Endpoint (see 'Azure Settings' tab){Environment.NewLine}";
            }

            if (string.IsNullOrWhiteSpace(request.ImageUrl) && (request.File == null || !_allowedFileContentType.Contains(request.File.ContentType)))
            {
                errorContent += $"Missing or invalid ImageUrl / no file provided";
            }

            if (!string.IsNullOrWhiteSpace(errorContent))
            {
                return(View(ComputerVisionViewModel.Analyzed(request,
                                                             new ComputerVisionAnalyzeResponse
                {
                    OtherErrorMessage = "Request not processed due to the following error(s):",
                    OtherErrorContent = errorContent
                })));
            }

            Track("Vision_ComputerVision");

            var imageAnalyzer = new ImageComputerVisionAnalyzer(request.ComputerVisionSubscriptionKey, request.ComputerVisionEndpoint, _httpClientFactory);
            var analyzeResult = await imageAnalyzer.AnalyzeAsync(request.ImageUrl, request.File, request.ImageAnalysisLanguage, request.ImageOcrLanguage, request.ImageReadLanguage);

            return(View(ComputerVisionViewModel.Analyzed(request, analyzeResult)));
        }