예제 #1
0
        private async Task <double?> VerticalLabelPriceSearch(RecognitionResult recognitionResult, Metadata imageMetadata, string[] patterns)
        {
            Dictionary <string, string> rawExpenses = new Dictionary <string, string>();

            List <TextRectangle> rectangles = recognitionResult.Lines
                                              .Select((line) => RectangleUtils.FromPointsArray(line))
                                              .ToList();

            // Zone 0
            var map        = new RectanglesZoneMap(rectangles, imageMetadata);
            var priceRegex = new Regex(@"^\s*(\W\D){0,1}\s*[\d,]+\.[\d]{2}\s*$", RegexOptions.IgnoreCase);

            bool found     = false;
            int  startingX = -1;
            int  startingY = -1;

            for (int i = 0; i < map.VerticalZoneCount; i++)
            {
                for (int k = 0; k < map.HorizontalZoneCount; k++)
                {
                    if (TestForPriceLabel(map.MapMatrix[i, k], patterns))
                    {
                        found     = true;
                        startingX = k;
                        startingY = i;
                        break;
                    }
                }
            }

            if (found)
            {
                int offsetX = startingX + 4;
                if (offsetX >= map.HorizontalZoneCount)
                {
                    offsetX = map.HorizontalZoneCount - 1;
                }

                for (int cY = startingY; cY < map.VerticalZoneCount; cY++)
                {
                    for (int cX = startingX; cX < offsetX; cX++)
                    {
                        var cleanStr = map.MapMatrix[cY, cX].Trim().Replace(" ", "");
                        if (priceRegex.IsMatch(cleanStr))
                        {
                            try {
                                var value = double.Parse(cleanStr, System.Globalization.NumberStyles.Currency, CultureInfo.GetCultureInfo("si-LK"));
                                return(value);
                            } catch (Exception e) {
                                Debug.Print(e.StackTrace);
                                break;
                            }
                        }
                    }
                }
            }

            return(null);
        }
예제 #2
0
        private async Task <DateTime?> VerticalLabelDateSearch(RecognitionResult recognitionResult, Metadata imageMetadata, string[] patterns)
        {
            Dictionary <string, string> rawExpenses = new Dictionary <string, string>();

            List <TextRectangle> rectangles = recognitionResult.Lines
                                              .Select((line) => RectangleUtils.FromPointsArray(line))
                                              .ToList();

            // Zone 0
            var map       = new RectanglesZoneMap(rectangles, imageMetadata);
            var dateRegex = new Regex(@"^\s*\d{2}\/\d{2}\/\d{4}\s*$", RegexOptions.IgnoreCase);

            bool found     = false;
            int  startingX = -1;
            int  startingY = -1;

            for (int i = 0; i < map.VerticalZoneCount; i++)
            {
                for (int k = 0; k < map.HorizontalZoneCount; k++)
                {
                    if (TestForPriceLabel(map.MapMatrix[i, k], patterns))
                    {
                        found     = true;
                        startingX = k;
                        startingY = i;
                        break;
                    }
                }
            }

            if (found)
            {
                int offsetX = startingX + 4;
                if (offsetX >= map.HorizontalZoneCount)
                {
                    offsetX = map.HorizontalZoneCount - 1;
                }

                for (int cY = startingY; cY < map.VerticalZoneCount; cY++)
                {
                    for (int cX = startingX; cX < offsetX; cX++)
                    {
                        var cleanStr = map.MapMatrix[cY, cX].Trim().Replace(" ", "");
                        if (dateRegex.IsMatch(cleanStr))
                        {
                            try {
                                var value = Convert.ToDateTime(cleanStr);
                                return(value);
                            } catch (Exception e) {
                                Debug.Print(e.StackTrace);
                                break;
                            }
                        }
                    }
                }
            }

            return(null);
        }