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));
        }
Esempio n. 2
0
        private static void ExecuteInitialization(RioValleyChiliDataContext context, IDataContextInitializationAdapter <RioValleyChiliDataContext> initializationAdapter, string[] args)
        {
            ContextsHelper.ConsoleOutputSettings();
            RVCDataLoadLoggerGate.RVCDataLoadLogger = RVCDataLoadLogger.GetDataLoadLogger(ExtractArgs(args, "-LogFolder "));

            if (context.Database.Exists())
            {
                Console.WriteLine("Preserving old data.");
                PreservedData.ObtainData(context);

                Console.WriteLine("Deleting existing database.");
                context.Database.Delete();
            }

            Console.WriteLine("Initializing database.");

            var stopwatch = new Stopwatch();

            stopwatch.Start();
            DataLoadResult.Success = true;
            initializationAdapter.InitializeDataContext(ref context);
            new RVCDataLoadResultObtainer().SetDataLoadResult(new RVCDataLoadResultObtainer.LoadResult
            {
                Success         = DataLoadResult.Success,
                RanToCompletion = true,
                TimeStamp       = DateTime.Now
            });
            stopwatch.Stop();

            Console.WriteLine("\n****************************************");
            Console.WriteLine("DATA LOAD COMPLETE");
            Console.WriteLine("Success: {0}", DataLoadResult.Success);
            Console.WriteLine("Total Run Time: {0:g}", stopwatch.Elapsed);
            Console.WriteLine("****************************************\n");

            if (RVCDataLoadLoggerGate.RVCDataLoadLogger != null)
            {
                RVCDataLoadLoggerGate.RVCDataLoadLogger.WriteLogSummary();
            }
        }
        public JsonResult PostImageWithBox([FromBody] PreservedData preservedData)
        {
            //Only the correct bounding box identifier will be returned
            string tableId = preservedData.BoundingBoxIdentifiers[0].Id;

            List <Block> allBlocks = preservedData.Blocks;

            preservedData.Blocks = preservedData.FilterToChildren(tableId);

            //Construct Table object
            Block[,] table = preservedData.ConstructTable(tableId);


            //Now, analyze the contexts of the cells to find the correct columns.
            int[] assetIndex = preservedData.FindIndex(table, allBlocks, new List <string>()
            {
                "asset", "holding", "assets", "holdings"
            });
            int[] quantityIndex = preservedData.FindIndex(table, allBlocks, new List <string>()
            {
                "quantity", "amount"
            });
            int[] valueIndex = preservedData.FindIndex(table, allBlocks, new List <string>()
            {
                "value"
            });

            Console.Out.WriteLine("Index " + assetIndex[0]);
            Console.Out.WriteLine("Columns" + table.GetLength(0));
            Console.Out.WriteLine("Rows" + table.GetLength(1));
            //GLHF
            List <AssetInput> returnData = new List <AssetInput>();

            if (assetIndex[0] != -1)
            {
                for (int row = 0; row < table.GetLength(1); row++)
                {
                    Console.Out.WriteLine("Row " + row);
                    int quantity;
                    try
                    {
                        //Trims decimal to whole number
                        string quantityString = preservedData.GetAllTextFromCell(table[quantityIndex[0], row], allBlocks);
                        quantityString = quantityString.Substring(0, quantityString.IndexOf('.'));
                        Console.Out.WriteLine(quantityString);
                        quantity = int.Parse(quantityString);

                        returnData.Add(new AssetInput(
                                           new AssetIdentifier(
                                               "0",
                                               //NEEDS A MANNER OF LOOKUP FOR SYMBOL TO PROPERLY ADD ASSET
                                               preservedData.GetAllTextFromCell(table[assetIndex[0], row], allBlocks),
                                               "",
                                               preservedData.GetAllTextFromCell(table[assetIndex[0], row], allBlocks)),
                                           quantity
                                           ));
                    }
                    catch
                    {
                        Console.Out.WriteLine("Not An Asset");
                    }


                    Console.Out.WriteLine(returnData.Count);
                }

                return(Json(returnData));
            }

            return(Json(null));
        }