예제 #1
0
        public override PageRect OcrAnalyze(Stream stream)
        {
            var imageStream = new MemoryStream();

            stream.CopyTo(imageStream);
            imageStream.Position = 0;
            stream.Position      = 0;

            var createAsyncTask = BitmapDecoder.CreateAsync(imageStream.AsRandomAccessStream()).AsTask();

            createAsyncTask.Wait();
            var bitmapDecoder = createAsyncTask.Result;

            var getSoftwareBitmapAsyncTask = bitmapDecoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8,
                                                                                  BitmapAlphaMode.Premultiplied).AsTask();

            getSoftwareBitmapAsyncTask.Wait();
            SoftwareBitmap softwareBitmap = getSoftwareBitmapAsyncTask.Result;

            var memoryStream = new MemoryStream();

            stream.CopyTo(memoryStream);
            stream.Position       = 0;
            memoryStream.Position = 0;

            var client = new AmazonTextractClient(AWSAccessKeyId, AWSSecretKey, RegionEndpoint.USEast1);
            var detectDocumentTextTask = client.DetectDocumentTextAsync(new Amazon.Textract.Model.DetectDocumentTextRequest
            {
                Document = new Amazon.Textract.Model.Document {
                    Bytes = memoryStream
                }
            });

            detectDocumentTextTask.Wait();

            var pageRect = new PageRect {
                Height    = softwareBitmap.PixelHeight,
                Width     = softwareBitmap.PixelWidth,
                LineTexts = detectDocumentTextTask.Result.Blocks.Where(block => block.BlockType == BlockType.LINE).Select(block =>
                {
                    return(new LineText
                    {
                        Text = block.Text,
                        X = (int)(softwareBitmap.PixelWidth * block.Geometry.BoundingBox.Left),
                        Y = (int)(softwareBitmap.PixelHeight * block.Geometry.BoundingBox.Top)
                    });
                }).ToList()
            };

            return(pageRect);
        }
예제 #2
0
        private static async Task <string> ReadText(string textUrl)
        {
            using var client = new AmazonTextractClient();

            var document = new Document
            {
                S3Object = new Amazon.Textract.Model.S3Object
                {
                    Bucket = S3BucketName,
                    Name   = textUrl
                }
            };
            var request = new DetectDocumentTextRequest
            {
                Document = document
            };

            var response = await client.DetectDocumentTextAsync(request);

            return(GetTextFromBlocks(response.Blocks));
        }
예제 #3
0
 private async Task <DetectDocumentTextResponse> UploadImageForRekognition(AmazonTextractClient objRekClient, DetectDocumentTextRequest objDetectText)
 {
     return(await objRekClient.DetectDocumentTextAsync(objDetectText));
 }