Esempio n. 1
0
        private void _pagesListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Get the recognized words of the selected page

            _wordsListBox.Items.Clear();

            IOcrPage ocrPage = _ocrDocument.Pages[_pagesListBox.SelectedIndex];

            if (ocrPage.IsRecognized)
            {
                IOcrPageCharacters pageCharacters = ocrPage.GetRecognizedCharacters();
                if (pageCharacters == null)
                {
                    return;
                }

                foreach (IOcrZoneCharacters zoneCharacters in pageCharacters)
                {
                    ICollection <OcrWord> words = zoneCharacters.GetWords();

                    foreach (OcrWord word in words)
                    {
                        _wordsListBox.Items.Add(word.Value);
                    }
                }
            }
        }
Esempio n. 2
0
        private static double GetConfidence(IOcrPage page)
        {
            var recognizedCharacters = page.GetRecognizedCharacters();
            var findZoneCharacters   = recognizedCharacters.FindZoneCharacters(0);

            return(findZoneCharacters
                   .DefaultIfEmpty()
                   .Average(character => character.Confidence) / 100D);
        }
Esempio n. 3
0
        private void BuildWordLists()
        {
            _ocrPageCharacters = _ocrPage.GetRecognizedCharacters();
            _ocrZoneWords      = new List <List <OcrWord> >();

            // Build the words
            foreach (IOcrZoneCharacters zoneCharacters in _ocrPageCharacters)
            {
                List <OcrWord> words = new List <OcrWord>();
                words.AddRange(zoneCharacters.GetWords());

                _ocrZoneWords.Add(words);
            }

            _selectedZoneIndex = -1;
            _selectedWordIndex = -1;
        }
        private void GetOmrReading(IOcrPage ocrPage, FormField field, ImageField imageField, int retry = 1)
        {
            IOcrPageCharacters pageCharacters = ocrPage.GetRecognizedCharacters();

            if (pageCharacters == null)
            {
                logger.Warn($"could not read OMR for ${field} ");
                imageField.FieldResult.Confidence = 0;
                imageField.FieldResult.Text       = "";
            }
            else
            {
                IOcrZoneCharacters zoneCharacters = pageCharacters[0];
                if (zoneCharacters.Count > 0)
                {
                    OcrCharacter omrCharacter = zoneCharacters[0];
                    imageField.FieldResult.Text       = omrCharacter.Code.ToString();
                    imageField.FieldResult.IsFilled   = omrCharacter.Code == FilledChar;
                    imageField.FieldResult.Confidence = omrCharacter.Confidence;
                    // often on a fill we get the line from the box, so we retry more narrowly
                    if (imageField.FieldResult.IsFilled)
                    {
                        if (retry > 0)
                        {
                            var orgZone = ocrPage.Zones[0];
                            orgZone.Bounds = ChangeBoundsRatio(orgZone.Bounds, 0.66);
                            ocrPage.Recognize(null);
                            GetOmrReading(ocrPage, field, imageField, 0);
                            logger.Info($"FILLED {field.Name}");
                        }
                    }
                }
                else
                {
                    imageField.FieldResult.Text = "";
                }
            }
        }
Esempio n. 5
0
        public void ZoneToControls(int index)
        {
            // Fill the controls from the current zone
            if (index != -1)
            {
                OcrZone zone = _zones[index];
                _nameTextBox.Text = zone.Name;

                // Convert the bounds to pixels
                LeadRect bounds = zone.Bounds;
                _leftTextBox.Text   = bounds.X.ToString();
                _topTextBox.Text    = bounds.Y.ToString();
                _widthTextBox.Text  = bounds.Width.ToString();
                _heightTextBox.Text = bounds.Height.ToString();

                // Disable these events when changing the combo boxes selected items once the "UpdateZonesControl" gets activated
                this._typeComboBox.SelectedIndexChanged                -= new System.EventHandler(this._propertiesComboBox_SelectedIndexChanged);
                this._languageComboBox.SelectedIndexChanged            -= new System.EventHandler(this._propertiesComboBox_SelectedIndexChanged);
                this._zoneViewPerspectiveComboBox.SelectedIndexChanged -= new System.EventHandler(this._propertiesComboBox_SelectedIndexChanged);
                this._zoneTextDirectionComboBox.SelectedIndexChanged   -= new System.EventHandler(this._propertiesComboBox_SelectedIndexChanged);

                _typeComboBox.SelectedItem = zone.ZoneType;

                for (int i = 0; i < _languageComboBox.Items.Count; i++)
                {
                    MyLanguage ml = (MyLanguage)_languageComboBox.Items[i];
                    if (zone.Language == null || zone.Language == String.Empty)
                    {
                        if (ml.Language == String.Empty)
                        {
                            _languageComboBox.SelectedItem = ml;
                            break;
                        }
                    }
                    else
                    {
                        if (ml.Language == zone.Language)
                        {
                            _languageComboBox.SelectedItem = ml;
                            break;
                        }
                    }
                }

                _zoneViewPerspectiveComboBox.SelectedIndex = 0;
                foreach (ViewPerspectiveItem item in _zoneViewPerspectiveComboBox.Items)
                {
                    if (item.ViewPerspective == zone.ViewPerspective)
                    {
                        _zoneViewPerspectiveComboBox.SelectedItem = item;
                        break;
                    }
                }

                _zoneTextDirectionComboBox.SelectedIndex = 0;
                foreach (TextDirectionItem item in _zoneTextDirectionComboBox.Items)
                {
                    if (item.TextDirection == zone.TextDirection)
                    {
                        _zoneTextDirectionComboBox.SelectedItem = item;
                        break;
                    }
                }

                this._typeComboBox.SelectedIndexChanged                += new System.EventHandler(this._propertiesComboBox_SelectedIndexChanged);
                this._languageComboBox.SelectedIndexChanged            += new System.EventHandler(this._propertiesComboBox_SelectedIndexChanged);
                this._zoneViewPerspectiveComboBox.SelectedIndexChanged += new System.EventHandler(this._propertiesComboBox_SelectedIndexChanged);
                this._zoneTextDirectionComboBox.SelectedIndexChanged   += new System.EventHandler(this._propertiesComboBox_SelectedIndexChanged);

                if (zone.ZoneType == OcrZoneType.Omr)
                {
                    StringBuilder sb = new StringBuilder();

                    if (!_ocrPage.IsRecognized)
                    {
                        sb.Append("Unfilled (0% certain)");
                    }
                    else
                    {
                        IOcrPageCharacters pageCharacters = _ocrPage.GetRecognizedCharacters();
                        if (pageCharacters == null || pageCharacters.Count == 0 || zone.Id >= pageCharacters.Count)
                        {
                            sb.Append("Unfilled (0% certain)");
                        }
                        else
                        {
                            IOcrZoneCharacters zoneCharacters = pageCharacters[zone.Id];
                            if (zoneCharacters.Count > 0)
                            {
                                OcrCharacter omrCharacter = zoneCharacters[0];
                                char         filledChar   = _ocrEngine.ZoneManager.OmrOptions.GetStateRecognitionCharacter(OcrOmrZoneState.Filled);
                                char         unfilledChar = _ocrEngine.ZoneManager.OmrOptions.GetStateRecognitionCharacter(OcrOmrZoneState.Unfilled);
                                if (omrCharacter.Code == filledChar)
                                {
                                    sb.Append("Filled");
                                }
                                else
                                {
                                    sb.Append("Unfilled");
                                }

                                sb.AppendFormat(" ({0}% certain)", omrCharacter.Confidence);
                            }
                            else
                            {
                                sb.AppendFormat("Unfilled (0% certain)");
                            }
                        }
                    }

                    _omrStatusLabel.Text = sb.ToString();
                }
                else
                {
                    _omrStatusLabel.Text = string.Empty;
                }

                if ((zone.CharacterFilters & OcrZoneCharacterFilters.Digit) == OcrZoneCharacterFilters.Digit)
                {
                    _digitCheckBox.Checked = true;
                }
                else
                {
                    _digitCheckBox.Checked = false;
                }

                if ((zone.CharacterFilters & OcrZoneCharacterFilters.Plus) == OcrZoneCharacterFilters.Plus)
                {
                    _plusCheckBox.Checked = true;
                }
                else
                {
                    _plusCheckBox.Checked = false;
                }
            }
            else
            {
                _nameTextBox.Text = string.Empty;

                _leftTextBox.Text   = string.Empty;
                _topTextBox.Text    = string.Empty;
                _widthTextBox.Text  = string.Empty;
                _heightTextBox.Text = string.Empty;

                _typeComboBox.SelectedIndex                = 0;
                _languageComboBox.SelectedIndex            = 0;
                _zoneViewPerspectiveComboBox.SelectedIndex = 0;
                _zoneTextDirectionComboBox.SelectedIndex   = 0;
                _omrStatusLabel.Text = string.Empty;

                _digitCheckBox.Checked = false;
                _plusCheckBox.Checked  = false;
            }
        }
 private static double GetConfidence(IOcrPage page)
 {
     var recognizedCharacters = page.GetRecognizedCharacters();
     var findZoneCharacters = recognizedCharacters.FindZoneCharacters(0);
     return findZoneCharacters
         .DefaultIfEmpty()
         .Average(character => character.Confidence) / 100D;
 }
Esempio n. 7
0
        private PageResults GetPageConfidence(IOcrPage ocrPage)
        {
            IOcrPageCharacters pageCharacters = ocrPage.GetRecognizedCharacters();
            double             pageConfidence = 0;
            int certainWords   = 0;
            int totalWords     = 0;
            int totalZoneWords = 0;
            int textZoneCount  = 0;

            for (int i = 0; i < ocrPage.Zones.Count; i++)
            {
                IOcrZoneCharacters zoneCharacters = pageCharacters.FindZoneCharacters(i);
                if (zoneCharacters.Count == 0)
                {
                    continue;
                }

                textZoneCount++;
                double zoneConfidence = 0;
                int    characterCount = 0;
                double wordConfidence = 0;
                totalZoneWords = 0;
                bool newWord = true;
                foreach (var ocrCharacter in zoneCharacters)
                {
                    if (newWord)
                    {
                        wordConfidence = 0;
                        characterCount = 0;
                        wordConfidence = 1000;
                    }
                    if (ocrCharacter.Confidence < wordConfidence)
                    {
                        wordConfidence = ocrCharacter.Confidence;
                    }
                    characterCount++;

                    if ((ocrCharacter.Position & OcrCharacterPosition.EndOfWord) == OcrCharacterPosition.EndOfWord || (ocrCharacter.Position & OcrCharacterPosition.EndOfLine) == OcrCharacterPosition.EndOfLine)
                    {
                        if (characterCount > 3)
                        {
                            if (ocrCharacter.WordIsCertain)
                            {
                                certainWords++;
                            }
                            totalWords++;
                            totalZoneWords++;
                            zoneConfidence += wordConfidence;
                        }

                        newWord = true;
                    }
                    else
                    {
                        newWord = false;
                    }
                }

                if (totalZoneWords > 0)
                {
                    zoneConfidence /= totalZoneWords;
                    pageConfidence += zoneConfidence;
                }
                else
                {
                    zoneConfidence  = 0;
                    pageConfidence += zoneConfidence;
                }
            }
            if (textZoneCount > 0)
            {
                pageConfidence /= textZoneCount;
            }
            else
            {
                pageConfidence = 0;
            }

            PageResults results = new PageResults(pageConfidence, certainWords, totalWords);

            return(results);
        }