Ejemplo n.º 1
0
 public IEnumerable<RecognitionResult> RecognizeScaleEachVariation(BitmapSource bitmap, ZoneConfiguration config)
 {
     var bitmaps = BitmapGenerators.SelectMany(g => g.Generate(bitmap));
     var scaled = bitmaps.Select(ScaleIfEnabled);
     var recognitions = scaled.SelectMany(bmp => RecognizeCore(config, bmp));
     return recognitions;
 }
Ejemplo n.º 2
0
        public override IEnumerable<RecognitionResult> Recognize(BitmapSource bitmap, ZoneConfiguration config)
        {
            bitmap = ScaleIfEnabled(bitmap);

            var bitmaps = BitmapGenerators.SelectMany(g => g.Generate(bitmap));
            var recognitions = bitmaps.SelectMany(bmp => RecognizeCore(config, bmp));
            return recognitions;
        }
Ejemplo n.º 3
0
        public override IEnumerable <RecognitionResult> Recognize(IImage bitmap, ZoneConfiguration config)
        {
            bitmap = ScaleIfEnabled(bitmap);

            var bitmaps      = BitmapGenerators.SelectMany(g => g.Generate(bitmap));
            var recognitions = bitmaps.SelectMany(bmp => RecognizeCore(config, bmp));

            return(recognitions);
        }
Ejemplo n.º 4
0
        private static string GetLanguage(ZoneConfiguration zoneConfiguration)
        {
            var filterType = zoneConfiguration.TextualDataFilter.FilterType;

            if (filterType == FilterType.Alpha || filterType == FilterType.AlphaOnly)
            {
                return("ES");
            }

            return(null);
        }
Ejemplo n.º 5
0
        protected string ExtractBestTextCandidate(BitmapSource bitmap, ITextualDataFilter filter, Symbology symbology)
        {
            var bounds = new Rect(0, 0, bitmap.Width, bitmap.Height);
            var zoneConfiguration = new ZoneConfiguration {Bounds = bounds, TextualDataFilter = filter, Id = "", Symbology = symbology};

            var recognitions = Engine.Recognize(bitmap, zoneConfiguration);

            var selector = OpticalResultSelector.ChooseBest(recognitions, zoneConfiguration);

            return selector?.Text;
        }
Ejemplo n.º 6
0
        public IEnumerable <RecognitionResult> Recognize(IImage image, ZoneConfiguration config)
        {
            var decoder      = new BarcodeReader();
            var decodeResult = decoder.Decode(image.ToBgra().ToBitmap());

            if (decodeResult == null)
            {
                yield break;
            }

            yield return(new RecognitionResult(decodeResult.Text, 1D));
        }
Ejemplo n.º 7
0
 public void Batch()
 {
     foreach (var bitmap in Directory.EnumerateFiles("Batch").Select(GetBitmap))
     {
         var sut         = new MessagingToolkitZoneBasedBarcodeReader();
         var recognition = sut.Recognize(bitmap, ZoneConfiguration.FromSingleImage(bitmap, new AlphanumericStringFilter(), Symbology.Barcode));
         foreach (var recognitionResult in recognition)
         {
             output.WriteLine(recognitionResult.Text ?? "{Empty result}");
         }
     }
 }
Ejemplo n.º 8
0
        public void QrCode()
        {
            var bitmap = "Images\\QRCode.png".LoadColor();
            var alphanumericStringFilter = new AlphanumericStringFilter();
            var symbology = Symbology.Barcode;

            var result = ExtractBestTextCandidate(bitmap, alphanumericStringFilter, symbology);

            Engine.Recognize(bitmap, ZoneConfiguration.FromSingleImage(bitmap, alphanumericStringFilter, symbology));

            Assert.NotNull(result);
        }
Ejemplo n.º 9
0
        protected string ExtractBestTextCandidate(IImage bitmap, ITextualDataFilter filter, Symbology symbology)
        {
            var bounds            = new Rect(0, 0, bitmap.Width, bitmap.Height);
            var zoneConfiguration = new ZoneConfiguration {
                Bounds = bounds, TextualDataFilter = filter, Id = "", Symbology = symbology
            };

            var recognitions = Engine.Recognize(bitmap, zoneConfiguration);

            var selector = OpticalResultSelector.ChooseBest(recognitions, zoneConfiguration);

            return(selector?.Text);
        }
        public IEnumerable<RecognitionResult> Recognize(BitmapSource bitmap, ZoneConfiguration config)
        {
            var coreReadOptions = CoreReadOptions;

            var leadRect = new LogicalRectangle(0, 0, bitmap.PixelWidth, bitmap.PixelHeight, LogicalUnit.Pixel);

            var unitsOfWork = from strategy in BarcodeStrategies
                                   let filteredImage = Freeze(strategy.BitmapFilter.Apply(bitmap))
                                   select new { ImageType = strategy.ImageType, FilteredImage = filteredImage };

            var selectMany = unitsOfWork.SelectMany(u => GetText(leadRect, coreReadOptions, u.FilteredImage, u.ImageType));
            return selectMany.Select(s => new RecognitionResult(s, 1));            
        }
Ejemplo n.º 11
0
        public IEnumerable <RecognitionResult> Recognize(IImage bitmap, ZoneConfiguration config)
        {
            var coreReadOptions = CoreReadOptions;

            var leadRect = new LogicalRectangle(0, 0, bitmap.Width, bitmap.Height, LogicalUnit.Pixel);

            var unitsOfWork = from strategy in BarcodeStrategies
                              let filteredImage = strategy.BitmapFilter.Apply(bitmap)
                                                  select new { ImageType = strategy.ImageType, FilteredImage = filteredImage };

            var selectMany = unitsOfWork.SelectMany(u => GetText(leadRect, coreReadOptions, u.FilteredImage, u.ImageType));

            return(selectMany.Select(s => new RecognitionResult(s, 1)));
        }
Ejemplo n.º 12
0
        private IEnumerable <RecognitionResult> RecognizeCore(ZoneConfiguration config, IImage bmp)
        {
            using (var page = OcrEngine.CreatePage(bmp.FromImageToRasterImage(), OcrImageSharingMode.AutoDispose))
            {
                var ocrZone = CreateOcrZoneForField(bmp, config);
                page.Zones.Add(ocrZone);
                page.Recognize(null);
                var text = page.GetText(0);

                var confidence = GetConfidence(page);

                var filteredText = config.TextualDataFilter.GetBestMatchFromRaw(text);
                yield return(new RecognitionResult(filteredText, confidence));
            }
        }
Ejemplo n.º 13
0
        private IEnumerable<RecognitionResult> RecognizeCore(ZoneConfiguration config, BitmapSource bmp)
        {
            using (var page = OcrEngine.CreatePage(bmp.ToRasterImage(), OcrImageSharingMode.AutoDispose))
            {
                var ocrZone = CreateOcrZoneForField(bmp, config);
                page.Zones.Add(ocrZone);
                page.Recognize(null);
                var text = page.GetText(0);

                var confidence = GetConfidence(page);

                var filteredText = config.TextualDataFilter.GetBestMatchFromRaw(text);
                yield return new RecognitionResult(filteredText, confidence);
            }
        }
        public IEnumerable <RecognitionResult> Recognize(IImage bitmap, ZoneConfiguration config)
        {
            var binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BitmapSourceLuminanceSource(bitmap.ToGray().ToBitmapSource())));

            var recognizer = Task.Run(() => barcodeReader.Decode(binaryBitmap, new Dictionary <DecodeOptions, object>()))
                             .ToObservable();

            var result = recognizer
                         .Timeout(TimeSpan.FromSeconds(2))
                         .Catch <Result, TimeoutException>(arg => Observable.Return <Result>(null))
                         .Catch <Result, NotFoundException>(arg => Observable.Return <Result>(null));

            var text = result.ToTask().Result?.Text;

            yield return(new RecognitionResult(text, 1D));
        }
        public IEnumerable<RecognitionResult> Recognize(BitmapSource bitmap, ZoneConfiguration config)
        {
            var binaryBitmap  = new BinaryBitmap(new HybridBinarizer(new BitmapSourceLuminanceSource(bitmap)));

            var recognizer = Task.Run(() => barcodeReader.Decode(binaryBitmap, new Dictionary<DecodeOptions, object>()))
                .ToObservable();

            var result = recognizer
                .Timeout(TimeSpan.FromSeconds(2))
                .Catch<Result, TimeoutException>(arg => Observable.Return<Result>(null))
                .Catch<Result, NotFoundException>(arg => Observable.Return<Result>(null));

            var text = result.ToTask().Result?.Text;

            yield return new RecognitionResult(text, 1D);
        }
Ejemplo n.º 16
0
        private static OcrZoneType GetZoneType(ZoneConfiguration fieldType)
        {
            switch (fieldType.Symbology)
            {
            case Symbology.Text:

                return(OcrZoneType.Text);

            case Symbology.Barcode:

                return(OcrZoneType.Barcode);

            default:
                throw new ArgumentOutOfRangeException(nameof(fieldType), fieldType, "El tipo de zona de OCR no se puede asociar a un tipo de OcrZoneType");
            }
        }
Ejemplo n.º 17
0
        public static RecognitionConfiguration FromSingleImage(IImage bitmapSource, ITextualDataFilter dataFilter, Symbology symbology)
        {
            var bounds            = new Rect(0, 0, bitmapSource.Width, bitmapSource.Height);
            var zoneConfiguration = new ZoneConfiguration()
            {
                Bounds = bounds, TextualDataFilter = dataFilter, Id = "", Symbology = symbology
            };

            return(new RecognitionConfiguration()
            {
                Zones = new List <ZoneConfiguration>()
                {
                    zoneConfiguration
                }
            });
        }
Ejemplo n.º 18
0
        private OcrZone CreateOcrZoneForField(IImage bitmap, ZoneConfiguration zoneConfiguration)
        {
            var leadRect = new Rect(0, 0, bitmap.Width, bitmap.Height).ToLeadRectRect();

            var readZone = new OcrZone
            {
                Bounds           = new LogicalRectangle(leadRect),
                Name             = zoneConfiguration.Id,
                CharacterFilters = GetCharacterFilters(zoneConfiguration),
                Language         = GetLanguage(zoneConfiguration),
                ZoneType         = GetZoneType(zoneConfiguration),
                IsEngineZone     = false
            };

            return(readZone);
        }
Ejemplo n.º 19
0
        public IEnumerable <RecognitionResult> Recognize(IImage bitmap, ZoneConfiguration config)
        {
            string text;

            try
            {
                var bmp      = bitmap.ToGray().ToBitmap();
                var barcodes = barcodeReader.Read(bmp);
                text = barcodes.FirstOrDefault()?.Text;
            }
            catch
            {
                text = null;
            }

            yield return(new RecognitionResult(text, 1D));
        }
Ejemplo n.º 20
0
        public void Run(NewOptions options)
        {
            var domainConfigFile = $"{options.Domain}.auth.yml";
            var zoneConfigFile   = $"{options.Domain}.zone.yml";

            var domainConfig = new DomainConfiguration
            {
                ZoneOrigin  = options.Domain,
                DomainToken = "domain-token-here"
            };
            var zoneConfig = new ZoneConfiguration
            {
                ZoneOrigin = options.Domain
            };

            _parser.CreateConfiguration(domainConfigFile, domainConfig);
            _parser.CreateConfiguration(zoneConfigFile, zoneConfig);
        }
Ejemplo n.º 21
0
        private static OcrZoneCharacterFilters GetCharacterFilters(ZoneConfiguration filterType)
        {
            switch (filterType.TextualDataFilter.FilterType)
            {
            case FilterType.Digits:
                return(OcrZoneCharacterFilters.Digit);

            case FilterType.AlphaOnly:
                return(OcrZoneCharacterFilters.Uppercase | OcrZoneCharacterFilters.Lowercase);

            case FilterType.Alpha:
                return(OcrZoneCharacterFilters.All);

            case FilterType.Number:
                return(OcrZoneCharacterFilters.Numbers | OcrZoneCharacterFilters.Punctuation);

            default:
                return(OcrZoneCharacterFilters.All);
            }
        }
        public static List <MissionSpot> GetRandomPointSpotsFromTargets(ZoneConfiguration zoneConfig, bool collectFindRadius = false)
        {
            var pointTargets = _missionDataCache.GetAllMissionTargets.Where(t => t.Type == MissionTargetType.rnd_point && t.ZoneId == zoneConfig.Id).ToList();

            var result = new List <MissionSpot>(pointTargets.Count);

            foreach (var missionTarget in pointTargets)
            {
                var ms = new MissionSpot(MissionSpotType.randompoint, missionTarget.targetPosition, zoneConfig.Id);

                if (collectFindRadius)
                {
                    ms.SetFindRadius(missionTarget.FindRadius);
                }

                result.Add(ms);
            }

            return(result);
        }
Ejemplo n.º 23
0
        public static IZoneConfiguration Build(string zoneName)
        {
            if (zoneName == "StartingZone")
            {
                var startingZone = new ZoneConfiguration();
                startingZone.SetName(zoneName);
                var door = new Door(Direction.N, "White Door");
                startingZone.AddDoor(door);
                var overview = new Message("LOST IN SHOREDITCH.",
                                           "YOU ARE STANDING AT THE END OF BRICK LANE BEFORE A SMALL BRICK BUILDING CALLED THE OLD TRUMAN BREWERY. \r\nAROUND YOU IS A FOREST OF INDIAN RESTAURANTS. \r\nA SMALL STREAM OF CRAFTED BEER FLOWS OUT OF THE BUILDING AND DOWN A GULLY.");
                startingZone.AddLook(Direction.Unknown, overview);
                var northDescription = new Message("I CAN SEE A BRICK BUILDING WITH A SIGN SAYING \"TRUMAN BREWERY\" AND A WOODEN WHITE DOOR");
                startingZone.AddLook(Direction.N, northDescription);
                return(startingZone);
            }

            if (zoneName == "Truman Brewery Hall 2")
            {
                var trumanBreweryHallTwo = new ZoneConfiguration();
                trumanBreweryHallTwo.SetName(zoneName);
                var door = new Door(Direction.N, "White Door");
                trumanBreweryHallTwo.AddDoor(door);
                return(trumanBreweryHallTwo);
            }

            if (zoneName == "Truman Brewery Warehouse")
            {
                var trumanBreweryWarehouse = new ZoneConfiguration();
                trumanBreweryWarehouse.SetName(zoneName);
                var overview = new Message("Inside Truman Brewery's warehouse.",
                                           "You're inside a warehouse filled with rows of beer kegs. You smell the putrid odor of death.");
                trumanBreweryWarehouse.AddLook(Direction.Unknown, overview);
                return(trumanBreweryWarehouse);
            }

            var trumanBreweryHallOne = new ZoneConfiguration();

            trumanBreweryHallOne.SetName(zoneName);
            return(trumanBreweryHallOne);
        }
Ejemplo n.º 24
0
        public override IEnumerable <RecognitionResult> Recognize(IImage bitmap, ZoneConfiguration config)
        {
            bitmap = ScaleIfEnabled(bitmap);

            foreach (var inputBitmap in BitmapGenerators.SelectMany(generator => generator.Generate(bitmap)))
            {
                var bytes = ConvertToTiffByteArray(inputBitmap);

                SetVariablesAccordingToConfig(engine, config);

                using (var img = Pix.LoadTiffFromMemory(bytes))
                {
                    using (var page = engine.Process(img, PageSegMode.SingleBlock))
                    {
                        var text = config.TextualDataFilter.GetBestMatchFromRaw(page.GetText());

                        var confidence = page.GetMeanConfidence() * 0.9;
                        yield return(new RecognitionResult(text, confidence));
                    }
                }
            }
        }
Ejemplo n.º 25
0
        private static void SetVariablesAccordingToConfig(TesseractEngine engine, ZoneConfiguration barcodeConfig)
        {
            if (barcodeConfig.TextualDataFilter.FilterType == FilterType.Alpha)
            {
                engine.SetVariable("tessedit_char_whitelist", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-");
            }

            if (barcodeConfig.TextualDataFilter.FilterType == FilterType.AlphaOnly)
            {
                engine.SetVariable("tessedit_char_whitelist", "ABCDEFGHIJKLMNOPQRSTUVWXYZ-");
            }

            if (barcodeConfig.TextualDataFilter.FilterType == FilterType.Digits)
            {
                engine.SetVariable("tessedit_char_whitelist", "0123456789");
            }

            if (barcodeConfig.TextualDataFilter.FilterType == FilterType.Number)
            {
                engine.SetVariable("tessedit_char_whitelist", "0123456789,.");
            }
        }
Ejemplo n.º 26
0
        private static void SetVariablesAccordingToConfig(TesseractEngine engine, ZoneConfiguration barcodeConfig)
        {
            if (barcodeConfig.TextualDataFilter.FilterType == FilterType.Alpha)
            {
                engine.SetVariable("tessedit_char_whitelist", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-");
            }

            if (barcodeConfig.TextualDataFilter.FilterType == FilterType.AlphaOnly)
            {
                engine.SetVariable("tessedit_char_whitelist", "ABCDEFGHIJKLMNOPQRSTUVWXYZ-");
            }

            if (barcodeConfig.TextualDataFilter.FilterType == FilterType.Digits)
            {
                engine.SetVariable("tessedit_char_whitelist", "0123456789");
            }

            if (barcodeConfig.TextualDataFilter.FilterType == FilterType.Number)
            {
                engine.SetVariable("tessedit_char_whitelist", "0123456789,.");
            }
        }
Ejemplo n.º 27
0
        public static void Initialize(ZoneConfiguration zoneConfiguration)
        {
            FixturesLoader.zoneConf = zoneConfiguration;

            // Clear on call
            nifRows.Clear();
            fixtureRows.Clear();
            // Trees and TreeCluster are always the same, do not load on each progress
            //treeRows.Clear();
            //treeClusterRows.Clear();

            LoadCsvData();
            LoadPolygons();

            // Load the filenames in data/prerendered/objects
            string objectImageFileDirectory = string.Format("{0}\\data\\prerendered\\objects", System.Windows.Forms.Application.StartupPath);

            if (!Directory.Exists(objectImageFileDirectory))
            {
                Directory.CreateDirectory(objectImageFileDirectory);
            }
            nifObjectImages = Directory.GetFiles(objectImageFileDirectory).Select(f => Path.GetFileNameWithoutExtension(f).ToLower()).ToList();
        }
Ejemplo n.º 28
0
        public override IEnumerable<RecognitionResult> Recognize(BitmapSource bitmap, ZoneConfiguration config)
        {
            bitmap = ScaleIfEnabled(bitmap);

            foreach (var inputBitmap in BitmapGenerators.SelectMany(generator => generator.Generate(bitmap)))
            {
                var bytes = ConvertToTiffByteArray(inputBitmap);

                SetVariablesAccordingToConfig(engine, config);

                using (var img = Pix.LoadTiffFromMemory(bytes))
                {
                    using (var page = engine.Process(img, PageSegMode.SingleBlock))
                    {
                        var text = config.TextualDataFilter.GetBestMatchFromRaw(page.GetText());

                        var confidence = page.GetMeanConfidence() * 0.9;
                        yield return new RecognitionResult(text, confidence);
                    }
                }

            }
        }
Ejemplo n.º 29
0
        public static void Initialize(ZoneConfiguration zoneConfiguration)
        {
            FixturesLoader.zoneConf = zoneConfiguration;

            // Clear on call
            nifRows.Clear();
            fixtureRows.Clear();
            // Trees and TreeCluster are always the same, do not load on each progress
            //treeRows.Clear();
            //treeClusterRows.Clear();

            LoadCsvData();
            LoadPolygons();

            // Load the filenames in data/prerendered/objects
            string objectImageFileDirectory = string.Format("{0}\\data\\prerendered\\objects", System.Windows.Forms.Application.StartupPath);
            if (!Directory.Exists(objectImageFileDirectory)) Directory.CreateDirectory(objectImageFileDirectory);
            nifObjectImages = Directory.GetFiles(objectImageFileDirectory).Select(f => Path.GetFileNameWithoutExtension(f).ToLower()).ToList();
        }
Ejemplo n.º 30
0
        private OcrZone CreateOcrZoneForField(BitmapSource bitmap, ZoneConfiguration zoneConfiguration)
        {
            var leadRect = new Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight).ToLeadRectRect();

            var readZone = new OcrZone
            {
                Bounds = new LogicalRectangle(leadRect),
                Name = zoneConfiguration.Id,
                CharacterFilters = GetCharacterFilters(zoneConfiguration),
                Language = GetLanguage(zoneConfiguration),
                ZoneType = GetZoneType(zoneConfiguration),
                IsEngineZone = false
            };

            return readZone;
        }
Ejemplo n.º 31
0
 public RecognizedZone(BitmapSource bitmap, ZoneConfiguration zoneConfiguration, RecognitionResult recognitionResult)
 {
     ZoneConfig = zoneConfiguration;
     Bitmap = bitmap;
     RecognitionResult = recognitionResult;
 }
Ejemplo n.º 32
0
        private static OcrZoneType GetZoneType(ZoneConfiguration fieldType)
        {
            switch (fieldType.Symbology)
            {
                case Symbology.Text:

                    return OcrZoneType.Text;

                case Symbology.Barcode:

                    return OcrZoneType.Barcode;

                default:
                    throw new ArgumentOutOfRangeException(nameof(fieldType), fieldType, "El tipo de zona de OCR no se puede asociar a un tipo de OcrZoneType");
            }
        }
Ejemplo n.º 33
0
 private static OcrZoneCharacterFilters GetCharacterFilters(ZoneConfiguration filterType)
 {
     switch (filterType.TextualDataFilter.FilterType)
     {
         case FilterType.Digits:
             return OcrZoneCharacterFilters.Digit;
         case FilterType.AlphaOnly:
             return OcrZoneCharacterFilters.Uppercase | OcrZoneCharacterFilters.Lowercase;
         case FilterType.Alpha:
             return OcrZoneCharacterFilters.All;
         case FilterType.Number:
             return OcrZoneCharacterFilters.Numbers | OcrZoneCharacterFilters.Punctuation;
         default:
             return OcrZoneCharacterFilters.All;
     }
 }
Ejemplo n.º 34
0
        public IEnumerable <RecognitionResult> RecognizeScaleEachVariation(IImage bitmap, ZoneConfiguration config)
        {
            var bitmaps      = BitmapGenerators.SelectMany(g => g.Generate(bitmap));
            var scaled       = bitmaps.Select(ScaleIfEnabled);
            var recognitions = scaled.SelectMany(bmp => RecognizeCore(config, bmp));

            return(recognitions);
        }
Ejemplo n.º 35
0
 public RecognizedZone(IImage bitmap, ZoneConfiguration zoneConfiguration, RecognitionResult recognitionResult)
 {
     ZoneConfig        = zoneConfiguration;
     Bitmap            = bitmap;
     RecognitionResult = recognitionResult;
 }
Ejemplo n.º 36
0
        private static string GetLanguage(ZoneConfiguration zoneConfiguration)
        {
            var filterType = zoneConfiguration.TextualDataFilter.FilterType;
            if (filterType == FilterType.Alpha || filterType == FilterType.AlphaOnly)
            {
                return "ES";
            }

            return null;
        }
Ejemplo n.º 37
0
 public static RecognitionConfiguration FromSingleImage(BitmapSource bitmapSource, ITextualDataFilter dataFilter, Symbology symbology)
 {
     var bounds = new Rect(0, 0, bitmapSource.Width, bitmapSource.Height);
     var zoneConfiguration = new ZoneConfiguration() { Bounds = bounds, TextualDataFilter = dataFilter, Id = "", Symbology = symbology};
     return new RecognitionConfiguration() { Zones = new List<ZoneConfiguration>() { zoneConfiguration } };
 }