Exemplo n.º 1
0
        public IActionResult Post()
        {
            AmazonTextractClient      objTextExtract = new AmazonTextractClient("AKIA23XZGIB6BEUBA5HM", "ePmW8ZsldWdQP3hmillAOMLu+A5b4ePgeBV3+/O5", Amazon.RegionEndpoint.USEast1);
            DetectDocumentTextRequest request        = new DetectDocumentTextRequest();

            byte[] fileData = null;

            using (var ms = new MemoryStream())
            {
                HttpContext.Request.Form.Files[0].CopyTo(ms);
                fileData = ms.ToArray();
                string s = Convert.ToBase64String(fileData);
                // act on the Base64 data
            }
            request.Document = new Document
            {
                Bytes = new MemoryStream(fileData)
            };

            var task = UploadImageForRekognition(objTextExtract, request);

            task.Wait();
            var result = task.Result;

            return(Ok(result));
        }
Exemplo n.º 2
0
        public static void Get_kv_map(string LocalEmploymentFile)
        {
            var readFile = File.ReadAllBytes(LocalEmploymentFile);

            MemoryStream         stream     = new MemoryStream(readFile);
            AmazonTextractClient abcdclient = new AmazonTextractClient();

            AnalyzeDocumentRequest analyzeDocumentRequest = new AnalyzeDocumentRequest
            {
                Document = new Document
                {
                    Bytes = stream
                },
                FeatureTypes = new List <string>
                {
                    FeatureType.FORMS
                }
            };
            var analyzeDocumentResponse = abcdclient.AnalyzeDocument(analyzeDocumentRequest);

            //Get the text blocks
            List <Block> blocks = analyzeDocumentResponse.Blocks;

            //get key and value maps
            List <Block> key_map   = new List <Block>();
            List <Block> value_map = new List <Block>();
            List <Block> block_map = new List <Block>();

            foreach (Block block in blocks)
            {
                var block_id = block.Id;
                block_map.Add(block);
                if (block.BlockType == BlockType.KEY_VALUE_SET)
                {
                    if (block.EntityTypes.Contains("KEY"))
                    {
                        key_map.Add(block);
                    }
                    else
                    {
                        value_map.Add(block);
                    }
                }
            }

            //Get Key Value relationship
            var getKeyValueRelationship = Get_kv_relationship(key_map, value_map, block_map);

            foreach (KeyValuePair <string, string> kvp in getKeyValueRelationship)
            {
                Console.WriteLine(" {0} : {1}", kvp.Key, kvp.Value);
            }
        }
Exemplo n.º 3
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);
        }
        protected IAmazonTextract CreateClient(AWSCredentials credentials, RegionEndpoint region)
        {
            var config = new AmazonTextractConfig {
                RegionEndpoint = region
            };

            Amazon.PowerShell.Utils.Common.PopulateConfig(this, config);
            this.CustomizeClientConfig(config);
            var client = new AmazonTextractClient(credentials, config);

            client.BeforeRequestEvent += RequestEventHandler;
            client.AfterResponseEvent += ResponseEventHandler;
            return(client);
        }
Exemplo n.º 5
0
        public override async Task RunCommand(object sender)
        {
            var engine     = (IAutomationEngineInstance)sender;
            var vAccessKey = (string)await v_AccessKey.EvaluateCode(engine);

            var vSecretKey = (string)await v_SecretKey.EvaluateCode(engine);

            var          ocrRequest = new DetectDocumentTextRequest();
            MemoryStream memStream  = new MemoryStream();

            if (v_ImageType == "Filepath")
            {
                string vFilePath = (string)await v_FilePath.EvaluateCode(engine);

                FileStream image = File.OpenRead(vFilePath);
                image.CopyTo(memStream);
            }
            else
            {
                Bitmap vBitmap = (Bitmap)await v_Bitmap.EvaluateCode(engine);

                vBitmap.Save(memStream, ImageFormat.Jpeg);
            }
            ocrRequest.Document       = new Document();
            ocrRequest.Document.Bytes = memStream;
            AmazonTextractConfig config = new AmazonTextractConfig();

            config.RegionEndpoint = Amazon.RegionEndpoint.GetBySystemName(v_AWSRegion);
            AmazonTextractClient       client  = new AmazonTextractClient(vAccessKey, vSecretKey, config);
            DetectDocumentTextResponse results = client.DetectDocumentText(ocrRequest);
            string readText = "";

            if (results.HttpStatusCode == System.Net.HttpStatusCode.OK)
            {
                foreach (Block block in results.Blocks)
                {
                    if (block.BlockType.Value == "LINE")
                    {
                        readText += block.Text + "\n";
                    }
                }
            }
            else
            {
                throw new Exception("Call to AWS textract failed");
            }
            readText.SetVariableValue(engine, v_OutputUserVariableName);
        }
Exemplo n.º 6
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));
        }
Exemplo n.º 7
0
 public TextractHandler()
 {
     this.textractClient = new AmazonTextractClient();
     this.s3Client       = new AmazonS3Client();
 }
        public async Task <Harvest> Recognize(string filePathName)
        {
            Logger.LogDebug(">> Recognize");

            var client = new AmazonTextractClient(
                _configuration.GetSection("AccessKey").Value,
                _configuration.GetSection("SecretAccessKey").Value,
                Amazon.RegionEndpoint.GetBySystemName(_configuration.GetSection("Region").Value));

            Amazon.Textract.Model.Document MyDocument;

            int imageWidth  = 0;
            int imageHeight = 0;

            using (Image image = Image.FromFile(filePathName))
            {
                imageWidth  = image.Width >= 2560 ? 2560 : image.Width;
                imageHeight = image.Height > 1080 ? 1080 : image.Height;

                #region Negative

                /*
                 *
                 * Bitmap newBitmap = new Bitmap(image.Width, image.Height);
                 *
                 * Graphics graphics = Graphics.FromImage(newBitmap);
                 *
                 * ColorMatrix colorMatrix = new ColorMatrix(
                 * new float[][]
                 * {
                 * new float[] {.3f, .3f, .3f, 0, 0},
                 * new float[] {.59f, .59f, .59f, 0, 0},
                 * new float[] {.11f, .11f, .11f, 0, 0},
                 * new float[] {0, 0, 0, 1, 0},
                 * new float[] {0, 0, 0, 0, 1}
                 * });
                 *
                 * ImageAttributes attributes = new ImageAttributes();
                 * attributes.SetColorMatrix(colorMatrix);
                 *
                 * graphics.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height),
                 * 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes);
                 *
                 * graphics.Dispose();
                 */

                #endregion

                using (MemoryStream memoryStream = new MemoryStream())
                {
                    image.Save(memoryStream, image.RawFormat);

                    MyDocument = new Amazon.Textract.Model.Document()
                    {
                        Bytes = memoryStream
                    };
                }
            }

            var request = new AnalyzeDocumentRequest()
            {
                Document = MyDocument, FeatureTypes = new List <string>()
                {
                    "TABLES"
                }
            };
            var result = await client.AnalyzeDocumentAsync(request);

            var specie  = "";
            var furType = "";

            float trophyRating = 0;
            float quickKill    = 0;
            float integrity    = 0;
            float score        = 0;

            bool ocrPaseFailure = false;

            if (result.HttpStatusCode == System.Net.HttpStatusCode.OK)
            {
                try
                {
                    Logger.LogTrace(System.Text.Json.JsonSerializer.Serialize(result));

                    float specieMinLeft = (100f + ((imageWidth - 1920) / 2)) / imageWidth;
                    float specieMinTop  = (150f + ((imageHeight - 1080) / 2)) / imageHeight;
                    float specieMaxTop  = (230f + ((imageHeight - 1080) / 2)) / imageHeight;

                    float furTypeMinLeft = (300f + ((imageWidth - 1920) / 2)) / imageWidth;
                    float furTypeMinTop  = (295f + ((imageHeight - 1080) / 2)) / imageHeight;

                    float trophyRatingMinLeft = (300f + ((imageWidth - 1920) / 2)) / imageWidth;
                    float trophyRatingMinTop  = (500f + ((imageHeight - 1080) / 2)) / imageHeight;

                    float quickKillMinLeft = (300f + ((imageWidth - 1920) / 2)) / imageWidth;
                    float quickKillMinTop  = (550f + ((imageHeight - 1080) / 2)) / imageHeight;

                    float integrityMinLeft = (300f + ((imageWidth - 1920) / 2)) / imageWidth;
                    float integrityMinTop  = (585f + ((imageHeight - 1080) / 2)) / imageHeight;

                    float scoreMinLeft  = (475f + ((imageWidth - 1920) / 2)) / imageWidth;
                    float scoreMinTop   = (680f + ((imageHeight - 1080) / 2)) / imageHeight;
                    float scoreMaxRight = (600f + ((imageWidth - 1920) / 2)) / imageWidth;

                    try
                    {
                        specie = result.Blocks.FindAll(b => b.BlockType.Value == "LINE" &&
                                                       ((b.Geometry.BoundingBox.Left + b.Geometry.BoundingBox.Width) < scoreMaxRight)).Find(b =>
                                                                                                                                            b.Geometry.BoundingBox.Left > specieMinLeft &&
                                                                                                                                            b.Geometry.BoundingBox.Top > specieMinTop &&
                                                                                                                                            specieMaxTop > b.Geometry.BoundingBox.Top
                                                                                                                                            ).Text;
                    }
                    catch (Exception e)
                    {
                        ocrPaseFailure = true;

                        Logger.LogError(e);
                    }

                    try
                    {
                        furType = result.Blocks.FindAll(b => b.BlockType.Value == "LINE" &&
                                                        ((b.Geometry.BoundingBox.Left + b.Geometry.BoundingBox.Width) < scoreMaxRight)).Find(b =>
                                                                                                                                             (b.Geometry.BoundingBox.Left > furTypeMinLeft) &&
                                                                                                                                             (b.Geometry.BoundingBox.Top > furTypeMinTop)
                                                                                                                                             ).Text;
                    }
                    catch (Exception e)
                    {
                        ocrPaseFailure = true;

                        Logger.LogError(e);
                    }

                    try
                    {
                        trophyRating = float.Parse(result.Blocks.FindAll(b => b.BlockType.Value == "LINE" &&
                                                                         ((b.Geometry.BoundingBox.Left + b.Geometry.BoundingBox.Width) < scoreMaxRight)).Find(b =>
                                                                                                                                                              (b.Geometry.BoundingBox.Left > trophyRatingMinLeft) &&
                                                                                                                                                              (b.Geometry.BoundingBox.Top > trophyRatingMinTop)
                                                                                                                                                              ).Text);
                    }
                    catch (Exception e)
                    {
                        ocrPaseFailure = true;

                        Logger.LogError(e);
                    }

                    try
                    {
                        quickKill = float.Parse(result.Blocks.FindAll(b => b.BlockType.Value == "LINE" &&
                                                                      ((b.Geometry.BoundingBox.Left + b.Geometry.BoundingBox.Width) < scoreMaxRight)).Find(b =>
                                                                                                                                                           (b.Geometry.BoundingBox.Left > quickKillMinLeft) &&
                                                                                                                                                           (b.Geometry.BoundingBox.Top > quickKillMinTop)
                                                                                                                                                           ).Text.Replace("%", ""));
                    }
                    catch (Exception e)
                    {
                        ocrPaseFailure = true;

                        Logger.LogError(e);
                    }

                    try
                    {
                        integrity = float.Parse(result.Blocks.FindAll(b => b.BlockType.Value == "LINE" &&
                                                                      ((b.Geometry.BoundingBox.Left + b.Geometry.BoundingBox.Width) < scoreMaxRight)).Find(b =>
                                                                                                                                                           (b.Geometry.BoundingBox.Left > integrityMinLeft) &&
                                                                                                                                                           (b.Geometry.BoundingBox.Top > integrityMinTop)
                                                                                                                                                           ).Text.Replace("%", ""));
                    }
                    catch (Exception e)
                    {
                        ocrPaseFailure = true;

                        Logger.LogError(e);
                    }

                    try
                    {
                        score = float.Parse(result.Blocks.FindAll(b => b.BlockType.Value == "LINE" &&
                                                                  ((b.Geometry.BoundingBox.Left + b.Geometry.BoundingBox.Width) < scoreMaxRight)).Find(b =>
                                                                                                                                                       (b.Geometry.BoundingBox.Left > scoreMinLeft) &&
                                                                                                                                                       (b.Geometry.BoundingBox.Top > scoreMinTop)).Text);
                    }
                    catch (Exception e)
                    {
                        ocrPaseFailure = true;

                        Logger.LogError(e);
                    }
                }
                catch (Exception e)
                {
                    ocrPaseFailure = true;

                    Logger.LogError(e);
                }
            }

            return(new Harvest()
            {
                FurType = furType, IntegrityBonus = integrity, QuickKillBonus = quickKill, Score = score, Specie = specie, TrophyRating = trophyRating, RequiresCheck = ocrPaseFailure
            });
        }
Exemplo n.º 9
0
 private async Task <DetectDocumentTextResponse> UploadImageForRekognition(AmazonTextractClient objRekClient, DetectDocumentTextRequest objDetectText)
 {
     return(await objRekClient.DetectDocumentTextAsync(objDetectText));
 }