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));
            }
        }
        /// <summary>
        /// Async method to load the image, call google's vision API and format the result into an easy to use format.
        /// Prior to calling the API, this method will compress the image if to large for the API service, and will rotate it according to the EXIF orientation.
        /// </summary>
        /// <param name="filePath">File path</param>
        /// <param name="fileFormatEnum">The image format. Needed for compression.</param>
        /// <returns></returns>
        public async Task <GoogleOcrResult> OcrImage(string filePath, FileFormatEnum fileFormatEnum)
        {
            var start = DateTime.Now;

            try
            {
                if (!File.Exists(filePath))
                {
                    throw new FileNotFoundException("", filePath);
                }
                using (var stream = File.OpenRead(filePath))
                {
                    return(await DoOcr(stream, fileFormatEnum, start));
                }
            }
            catch (Exception e)
            {
                return(GoogleOcrResult.CreateErrorResult(DateTime.Now.Subtract(start), e));
            }
        }
Ejemplo n.º 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));
            }
        }