private async Task <GoogleOcrResult> DoOcr(Stream imageAsStream, FileFormatEnum fileFormatEnum, DateTime start)
        {
            try
            {
                var preprocessedResult = _ocrPreProcessing.AjustOrientationAndSize(imageAsStream, fileFormatEnum);
                using (var stream = preprocessedResult.ImageFileStream)
                {
                    var builder = new ImageAnnotatorClientBuilder {
                        JsonCredentials = File.ReadAllText(_configurations.CredentialsJsonFile)
                    };
                    var client = await builder.BuildAsync();

                    var img = await Image.FromStreamAsync(stream);

                    var textAnnotations = await client.DetectTextAsync(img);

                    var rawGoogleOcrResult = RawGoogleOcrResult.CreateFrom(textAnnotations.ToList());

                    var content = _googleOcrParser.Execute(rawGoogleOcrResult, preprocessedResult.NewImageHeight,
                                                           preprocessedResult.NewImageWidth);
                    return(GoogleOcrResult.CreateSuccessResult(DateTime.Now.Subtract(start), content, rawGoogleOcrResult));
                }
            }
            catch (Exception e)
            {
                return(GoogleOcrResult.CreateErrorResult(DateTime.Now.Subtract(start), e));
            }
        }
示例#2
0
        private async Task <AzureOcrResult> DoOcr(Stream imageAsStream, FileFormatEnum fileFormatEnum, DateTime start)
        {
            try
            {
                var preprocessedResult = _ocrPreProcessing.AjustOrientationAndSize(imageAsStream, fileFormatEnum);
                using (var stream = preprocessedResult.ImageFileStream)
                {
                    ValidateImageProportions(preprocessedResult.NewImageHeight, preprocessedResult.NewImageWidth);
                    var rawAzureOcrResult = await _azureVisionApi.Execute(stream);

                    var content = _azureOcrParser.Execute(rawAzureOcrResult, preprocessedResult.NewImageHeight,
                                                          preprocessedResult.NewImageWidth);
                    return(AzureOcrResult.CreateSuccesResult(DateTime.Now.Subtract(start), content, rawAzureOcrResult));
                }
            }
            catch (Exception e)
            {
                return(AzureOcrResult.CreateErrorResult(DateTime.Now.Subtract(start), e));
            }
        }
示例#3
0
        private async Task <GoogleOcrResult> DoOcr(Stream imageAsStream, FileFormatEnum fileFormatEnum, DateTime start)
        {
            try
            {
                var preprocessedResult = _ocrPreProcessing.AjustOrientationAndSize(imageAsStream, fileFormatEnum);
                using (var stream = preprocessedResult.ImageFileStream)
                {
                    using (var service = VisionService())
                    {
                        var entries = await service.RecognizeTextAsync(stream);

                        var rawGoogleOcrResult = RawGoogleOcrResult.CreateFrom(entries);
                        var content            = _googleOcrParser.Execute(rawGoogleOcrResult, preprocessedResult.NewImageHeight,
                                                                          preprocessedResult.NewImageWidth);
                        return(GoogleOcrResult.CreateSuccessResult(DateTime.Now.Subtract(start), content, rawGoogleOcrResult));
                    }
                }
            }
            catch (Exception e)
            {
                return(GoogleOcrResult.CreateErrorResult(DateTime.Now.Subtract(start), e));
            }
        }