public JsonResult PostImage([FromForm] IFormFile body)
        {
            byte[] imageBytes;
            string newFileName = new TimeSpan(DateTime.Now.Ticks).TotalMilliseconds.ToString();

            newFileName = newFileName.Substring(0, newFileName.IndexOf('.')) + ".png";
            Image page = null;

            using (var memoryStream = new MemoryStream())
            {
                body.CopyTo(memoryStream);
                page = Image.FromStream(memoryStream);
                OCR.S3Upload.Upload.UploadDocument("pdfidentify", newFileName, memoryStream, RegionEndpoint.USEast2);
            }
            Thread.Sleep(5000);


            Console.Out.WriteLine(newFileName);
            TextractResponse response = Analyze.AnalyzeFile(newFileName).Result;


            //Dictionary of every block indexed by ID
            Dictionary <string, Block> allBlocks = new Dictionary <string, Block>();

            foreach (var block in response.Blocks)
            {
                allBlocks.Add(block.Id, block);
            }


            //for each table block, save all children
            //for each child, save all children
            int          i            = 1;
            List <Block> targetBlocks = new List <Block>();
            List <BoundingBoxIdentifier> boundingBoxes = new List <BoundingBoxIdentifier>();

            foreach (var table in response.FilterType("TABLE"))
            {
                Analyze.GetChildrenRecursive(allBlocks, table, targetBlocks);

                BoundingBox boundingBox = table.Geometry.BoundingBox;
                //Convert relative location to absolute pixels
                BoundingBoxIdentifier identifier = new BoundingBoxIdentifier((int)(page.Width * boundingBox.Left),
                                                                             (int)(page.Height * boundingBox.Top),
                                                                             (int)(page.Width * boundingBox.Width),
                                                                             (int)(page.Height * boundingBox.Height),
                                                                             i++.ToString(),
                                                                             table.Id);

                boundingBoxes.Add(identifier);
            }

            PreservedData preservedData = new PreservedData(targetBlocks, boundingBoxes);


            return(Json(preservedData));
        }
Пример #2
0
        public static VisualData UploadActions(IFormFile imageFile)
        {
            //Create Image from File
            Image image = CreateImage(imageFile);


            //Upload DataStream to S3
            using (MemoryStream memoryStream = new MemoryStream())
            {
                image.Save(memoryStream, ImageFormat.Png);
                PDFIdentify.S3Upload.Upload.UploadDocument("pdfidentify", "112233.png", memoryStream, RegionEndpoint.USEast2);
            }

            //Run Textract Operations
            //Preserve tables bounding boxes & response.blocks
            TextractResponse response = Analyze.AnalyzeFile("112233.png", "https://*****:*****@"D:\OCR\PDFIdentifyC\Python\test.json"));*/


            List <BoundingBoxIdentifier> bBoxIdens = new List <BoundingBoxIdentifier>();

            foreach (var table in response.FilterType("TABLE"))
            {
                //Convert relative coordinate values to abs pixel values
                bBoxIdens.Add(CreateBBox(table, image));
            }


            //Draw on Image
            var visualData = new VisualData
            {
                Colors = DrawBbOnImage(image, bBoxIdens), DataUrl = GetImageDataUrl(image), TextractResponse = response
            };

            //Return Image DataURL & Options for Dropdown Menu
            return(visualData);
        }