private void UpdateUIState() { // Zones controls _btnDeleteZone.Enabled = (_tvZonesList.SelectedNode != null) && (_tvZonesList.SelectedNode.Tag.GetType() == typeof(int)); _btnClearZones.Enabled = _tvZonesList.Nodes.Count > 0; // Cells Controls _btnDetectCells.Enabled = false; if (_ocrPage.Zones.Count > 0) { int zoneIndex = -1; if (_tvZonesList.SelectedNode != null) { if (_tvZonesList.SelectedNode.Parent == null) { zoneIndex = (int)_tvZonesList.SelectedNode.Tag; } else { zoneIndex = (int)_tvZonesList.SelectedNode.Parent.Tag; } if (zoneIndex >= 0) { OcrZone zone = _ocrPage.Zones[zoneIndex]; _btnDetectCells.Enabled = (zone.ZoneType == OcrZoneType.Table) && (_tvZonesList.SelectedNode.Tag.GetType() == typeof(int)); } } } _btnClearCells.Enabled = (_tvZonesList.SelectedNode != null) && (_tvZonesList.SelectedNode.Tag.GetType() == typeof(int)) && (_tvZonesList.SelectedNode.Nodes.Count > 0); // Only show the cells manipulation controls in case of OmniPage engines only (Professional and Arabic engines) _cellsGroupBox.Visible = (_ocrEngine.EngineType == OcrEngineType.OmniPage || _ocrEngine.EngineType == OcrEngineType.OmniPageArabic); }
void _annAutomation_Draw(object sender, AnnDrawDesignerEventArgs e) { if (!(e.OperationStatus == AnnDesignerOperationStatus.End)) { return; } // Add a new zone from the annotation rectangle object ZoneAnnotationObject zoneObject = e.Object as ZoneAnnotationObject; if (zoneObject == null) { return; } OcrZone zone = new OcrZone(); zone.Bounds = RestrictZoneBoundsToPage(_ocrPage, BoundsFromAnnotations(zoneObject, _annAutomation.Container)); if (!zone.Bounds.IsEmpty) { _ocrPage.Zones.Add(zone); } else { InteractiveMode = ViewerControlInteractiveMode.DrawZoneMode; return; } // Set the zone zoneObject.SetZone(_ocrPage, _ocrPage.Zones.Count - 1, _showZonesToolStripButton.Checked, _showZoneNameToolStripButton.Checked); ZonesUpdated(); InteractiveMode = ViewerControlInteractiveMode.DrawZoneMode; UpdateUIState(); }
public void SetZone(IOcrPage ocrPage, int zoneIndex, bool isVisible, bool isNameVisible) { _ocrPage = ocrPage; _zoneIndex = zoneIndex; IsVisible = isVisible; mylabel.IsVisible = isNameVisible; if (_ocrPage != null && _zoneIndex >= 0 && _zoneIndex < _ocrPage.Zones.Count) { OcrZone zone = _ocrPage.Zones[_zoneIndex]; if (string.IsNullOrEmpty(zone.Name)) { mylabel.Text = "Zone " + (_zoneIndex + 1).ToString(); } else { mylabel.Text = zone.Name; } if (zone.ZoneType == OcrZoneType.None || zone.ZoneType == OcrZoneType.Graphic || zone.ZoneType == OcrZoneType.Barcode) { RasterColor color = RasterColorConverter.FromColor(Color.FromArgb(32, Color.Yellow)); this.Fill = AnnSolidColorBrush.Create(color.ToString());//Color.FromArgb(32, Color.Yellow) this.Stroke = AnnStroke.Create(AnnSolidColorBrush.Create("Blue"), new LeadLengthD(1)); } else { this.Stroke = AnnStroke.Create(AnnSolidColorBrush.Create("Red"), new LeadLengthD(1)); } } }
private IOcrPage DoublePass(RasterImage image) { //first pass with default settings IOcrPage page = _ocrEngine.CreatePage(image.Clone(), OcrImageSharingMode.AutoDispose); page.Recognize(null); //second pass with mobile image processing set to true _ocrEngine.SettingManager.SetBooleanValue("Recognition.Preprocess.MobileImagePreprocess", true); IOcrPage mobilePage = _ocrEngine.CreatePage(image.Clone(), OcrImageSharingMode.AutoDispose); mobilePage.Recognize(null); //get the confidence of both pages PageResults firstPassResults = GetPageConfidence(page); PageResults secondPassResults = GetPageConfidence(mobilePage); double confidenceDif = firstPassResults.Confidence - secondPassResults.Confidence; IOcrPage highestConfidence; PageResults pageResultsHighest; if (confidenceDif > 2) { highestConfidence = page; pageResultsHighest = firstPassResults; } else { highestConfidence = mobilePage; pageResultsHighest = secondPassResults; } if (pageResultsHighest.TotalWords < 20) { IOcrPage thirdPass = highestConfidence.Copy(); thirdPass.Unrecognize(); OcrZone singleZone = new OcrZone() { Bounds = new LeadRect(0, 0, image.Width, image.Height) }; thirdPass.Zones.Add(singleZone); thirdPass.Recognize(null); PageResults thirdResults = GetPageConfidence(thirdPass); double confidencetDifThird = thirdResults.Confidence - pageResultsHighest.Confidence; if (confidenceDif > 5) { highestConfidence = thirdPass; pageResultsHighest = thirdResults; } } return(highestConfidence); }
void _rubberBand_RubberBandCompleted(object sender, ImageViewerRubberBandEventArgs e) { if (_ocrPage == null) { return; } try { _tsMainZoomComboBox.Enabled = false; using (WaitCursor cursor = new WaitCursor()) { if (_viewer.Image != null) { _currentHighlightRect = _viewer.ConvertRect( null, ImageViewerCoordinateType.Control, ImageViewerCoordinateType.Image, LeadRect.FromLTRB(e.Points[0].X, e.Points[0].Y, e.Points[1].X, e.Points[1].Y)); if (_currentHighlightRect.Width > 2 && _currentHighlightRect.Height > 2) { OcrZone zone = new OcrZone(); zone.Bounds = _currentHighlightRect; zone.ZoneType = OcrZoneType.Text; zone.CharacterFilters = OcrZoneCharacterFilters.None; _ocrPage.Zones.Clear(); _ocrPage.Zones.Add(zone); _ocrPage.Recognize(null); _recognitionResults.Text = _ocrPage.GetText(0); if (_recognitionResults.Text == "\n" || _recognitionResults.Text == "") { Messager.ShowInformation(this, "No text was recognized."); } } } } } catch (Exception ex) { Messager.ShowError(this, ex); } finally { _viewer.Invalidate(); _tsMainZoomComboBox.Enabled = true; } }
public void ClearTableCells() { if (_ocrPage != null && _zoneIndex >= 0 && _ocrPage.Zones != null && _zoneIndex < _ocrPage.Zones.Count) { OcrZone zone = _ocrPage.Zones[_zoneIndex]; OcrZoneCell[] cells = null; cells = _ocrPage.Zones.GetZoneCells(zone); if (cells != null) { _ocrPage.Zones.SetZoneCells(zone, null); } } }
private void _propertiesComboBox_SelectedIndexChanged(object sender, EventArgs e) { if (_tvZonesList.SelectedNode == null) { return; } OcrZone zone = CurrentZone; if (sender == _typeComboBox) { zone.ZoneType = (OcrZoneType)_typeComboBox.SelectedItem; } else if (sender == _languageComboBox) { zone.Language = ((MyLanguage)(_languageComboBox.SelectedItem)).Language; } else if (sender == _zoneViewPerspectiveComboBox) { zone.ViewPerspective = ((ViewPerspectiveItem)_zoneViewPerspectiveComboBox.SelectedItem).ViewPerspective; } else if (sender == _zoneTextDirectionComboBox) { zone.TextDirection = ((TextDirectionItem)_zoneTextDirectionComboBox.SelectedItem).TextDirection; } OcrZoneCell[] cells = null; cells = _zones.GetZoneCells(_zones[(int)_tvZonesList.SelectedNode.Tag]); if (cells != null && cells.Length > 0) { _tvZonesList.SelectedNode.Nodes.Clear(); _viewerControl.ClearZoneCells((int)_tvZonesList.SelectedNode.Tag); } CurrentZone = zone; // Immediately update the zone if the user changed the zone type to "Table" and wanted to detect the zone cells. if (_ocrPage.Zones != null && _ocrPage.Zones.Count > (int)_tvZonesList.SelectedNode.Tag) { _ocrPage.Zones[(int)_tvZonesList.SelectedNode.Tag] = zone; } ZoneToControls((_tvZonesList.SelectedNode != null) ? (int)_tvZonesList.SelectedNode.Tag : -1); UpdateUIState(); DoAction("ZonePropertiesChanged", null); }
private static string DetectAndRecognizeMicr(string fileName) { try { //Initialize the codecs class and load the image using (var codecs = new RasterCodecs()) using (var img = codecs.Load(fileName)) { //prepare the MICR detector command and run it var micrDetector = new MICRCodeDetectionCommand { SearchingZone = new LeadRect(0, 0, img.Width, img.Height) }; micrDetector.Run(img); //See if there is a MICR detected - if not return if (micrDetector.MICRZone == LeadRect.Empty) { return("No MICR detected in this file."); } //if there is a MICR zone detected startup OCR using (var ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, false)) { ocrEngine.Startup(null, null, null, null); //create the OCR Page var ocrPage = ocrEngine.CreatePage(img, OcrImageSharingMode.None); //Create the OCR zone for the MICR and add to the Ocr Page and recognize var micrZone = new OcrZone { Bounds = LogicalRectangle.FromRectangle(micrDetector.MICRZone), ZoneType = OcrZoneType.Micr }; ocrPage.Zones.Add(micrZone); ocrPage.Recognize(null); //return the MICR text return(ocrPage.GetText(-1)); } } } catch (Exception ex) { return(ex.Message); } }
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); }
private void _btnAddZone_Click(object sender, EventArgs e) { // Add a new zone OcrZone zone = new OcrZone(); zone.Bounds = new LeadRect(0, 0, 1, 1); _ocrPage.Zones.Add(zone); _lbZonesList.Items.Add(new ZoneItem("New zone", _newZoneCount)); _newZoneCount++; _lbZonesList.ClearSelected(); _lbZonesList.SelectedIndex = _lbZonesList.Items.Count - 1; _updateZonesControl.UpdateUIState(); UpdateUIState(); }
/// <summary> /// Called from the main form when the zones are updated /// </summary> public void ZonesUpdated() { // Stop updating the viewer _rasterImageViewer.BeginUpdate(); // Remove all the annotations objects and re-add them from the zones if (_annAutomation != null) { _annAutomation.Cancel(); _annAutomation.Container.Children.Clear(); } _ignoreAddRemove = true; _setSelect = true; // Get the rectangle automation object so we can use the template // to create the new annotation objects bool isVisible = _showZonesToolStripButton.Checked && !MainForm.PerspectiveDeskewActive && !MainForm.UnWarpActive; bool isNameVisible = _showZoneNameToolStripButton.Checked; if (_ocrPage != null && _ocrPage.Zones != null && _ocrPage.Zones.Count > 0) { for (int i = 0; i < _ocrPage.Zones.Count; i++) { OcrZone zone = _ocrPage.Zones[i]; ZoneAnnotationObject zoneObject = new ZoneAnnotationObject(); zoneObject.SetZone(_ocrPage, i, isVisible, isNameVisible); _annAutomation.Container.Children.Add(zoneObject); // Now we can calculate the object bounds correctly LeadRect rc = zone.Bounds; LeadRectD rect = BoundsToAnnotations(zoneObject, rc, _annAutomation.Container); zoneObject.Rect = rect; } } _ignoreAddRemove = false; // Re-update the viewer _rasterImageViewer.EndUpdate(); _rasterImageViewer.Invalidate(); UpdateUIState(); }
/// <summary> /// Uses an instance of the LEADTools OCR Engine to "read" the text /// in the pre-defined OCR region representing the mailing address /// area. Is thread safe. /// </summary> /// <param name="ocrEngine"></param> /// <param name="document"></param> /// <returns></returns> private static string GetAddressBlockText(IOcrEngine ocrEngine, PDFDocument document) { string returnedText = null; using (var codecs = new RasterCodecs()) using (var image = new RasterImage(document.GetPageImage(codecs, 1))) using (IOcrDocument ocrDocument = ocrEngine.DocumentManager.CreateDocument()) using (IOcrPage ocrPage = ocrDocument.Pages.AddPage(image, null)) { var myZone = new OcrZone(); myZone.Bounds = new LogicalRectangle(0, 2, 4, 1.4, LogicalUnit.Inch); ocrPage.Zones.Add(myZone); ocrPage.Recognize(null); returnedText = ocrPage.GetText(0).ToUpper(); } return(returnedText); }
private void _btnAddZone_Click(object sender, EventArgs e) { // Add a new zone OcrZone zone = OcrTypeManager.CreateDefaultOcrZone(); zone.Bounds = new LeadRect(0, 0, 1, 1); _ocrPage.Zones.Add(zone); TreeNode addedZone = _tvZonesList.Nodes.Add(DemosGlobalization.GetResxString(GetType(), "Resx_NewZone") + (_newZoneCount + 1).ToString()); addedZone.Tag = _ocrPage.Zones.Count - 1; _tvZonesList.SelectedNode = addedZone; _newZoneCount++; _updateZonesControl.UpdateUIState(); UpdateUIState(); }
private void _characterFiltersCheckBox_CheckedChanged(object sender, EventArgs e) { if (_tvZonesList.SelectedNode == null) { return; } OcrZoneCharacterFilters filters = OcrZoneCharacterFilters.None; if (_digitCheckBox.Checked) { filters |= OcrZoneCharacterFilters.Digit; } if (_uppercaseCheckBox.Checked) { filters |= OcrZoneCharacterFilters.Uppercase; } if (_lowercaseCheckBox.Checked) { filters |= OcrZoneCharacterFilters.Lowercase; } if (_punctuationCheckBox.Checked) { filters |= OcrZoneCharacterFilters.Punctuation; } if (_miscellaneousCheckBox.Checked) { filters |= OcrZoneCharacterFilters.Miscellaneous; } if (_plusCheckBox.Checked) { filters |= OcrZoneCharacterFilters.Plus; } OcrZone zone = CurrentZone; zone.CharacterFilters = filters; CurrentZone = zone; }
private void _characterFiltersCheckBox_CheckedChanged(object sender, EventArgs e) { OcrZoneCharacterFilters filters = OcrZoneCharacterFilters.None; if (_digitCheckBox.Checked) { filters |= OcrZoneCharacterFilters.Digit; } if (_plusCheckBox.Checked) { filters |= OcrZoneCharacterFilters.Plus; } OcrZone zone = CurrentZone; zone.CharacterFilters = filters; CurrentZone = zone; }
private void _textTextBox_LostFocus(object sender, EventArgs e) { if (_lbZonesList.SelectedItems.Count == 0 || _lbZonesList.SelectedItems.Count > 1) { return; } // Get the new zone name or section name OcrZone zone = CurrentZone; string str = (sender as TextBox).Text; if (sender == _nameTextBox) { zone.Name = str; } CurrentZone = zone; }
public override void Render(AnnContainerMapper mapper, AnnObject annObject) { ZoneAnnotationObject zoneObject = annObject as ZoneAnnotationObject; if (_ocrPage != null && zoneObject.ZoneIndex >= 0 && zoneObject.ZoneIndex < _ocrPage.Zones.Count) { AnnWinFormsRenderingEngine engine = RenderingEngine as AnnWinFormsRenderingEngine; if (engine != null && engine.Context != null && zoneObject != null && zoneObject.Cells != null) { Graphics graphics = engine.Context; OcrZone zone = _ocrPage.Zones[zoneObject.ZoneIndex]; OcrZoneCell[] cells = null; cells = _ocrPage.Zones.GetZoneCells(zone); if (_ocrPage.TableZoneManager != null && zone.ZoneType == OcrZoneType.Table && cells != null && cells.Length > 0 && CellPen != null) { GraphicsState gState = graphics.Save(); if (gState != null) { foreach (OcrZoneCell cell in zoneObject.Cells) { LeadRectD rc = new LeadRectD(cell.Bounds.X, cell.Bounds.Y, cell.Bounds.Width, cell.Bounds.Height); rc = mapper.RectFromContainerCoordinates(rc, AnnFixedStateOperations.None); if (!rc.IsEmpty) { // Draw cells borders as lines in order not to draw the borders with 0 width DrawLine(graphics, OcrCellBorder.Left, cell.LeftBorderStyle, cell.LeftBorderWidth, (float)rc.Left, (float)rc.Top, (float)rc.Left, (float)rc.Bottom); DrawLine(graphics, OcrCellBorder.Top, cell.TopBorderStyle, cell.TopBorderWidth, (float)rc.Left, (float)rc.Top, (float)rc.Right, (float)rc.Top); DrawLine(graphics, OcrCellBorder.Right, cell.RightBorderStyle, cell.RightBorderWidth, (float)rc.Right, (float)rc.Top, (float)rc.Right, (float)rc.Bottom); DrawLine(graphics, OcrCellBorder.Bottom, cell.BottomBorderStyle, cell.BottomBorderWidth, (float)rc.Left, (float)rc.Bottom, (float)rc.Right, (float)rc.Bottom); } } graphics.Restore(gState); } } } } base.Render(mapper, annObject); }
private void _propertiesComboBox_SelectedIndexChanged(object sender, EventArgs e) { if (_lbZonesList.SelectedItems.Count == 0 || _lbZonesList.SelectedItems.Count > 1) { return; } OcrZone zone = CurrentZone; if (sender == _typeComboBox) { zone.ZoneType = (OcrZoneType)_typeComboBox.SelectedItem; } else if (sender == _languageComboBox) { zone.Language = ((MyLanguage)(_languageComboBox.SelectedItem)).Language; } else if (sender == _zoneViewPerspectiveComboBox) { zone.ViewPerspective = ((ViewPerspectiveItem)_zoneViewPerspectiveComboBox.SelectedItem).ViewPerspective; } else if (sender == _zoneTextDirectionComboBox) { zone.TextDirection = ((TextDirectionItem)_zoneTextDirectionComboBox.SelectedItem).TextDirection; } CurrentZone = zone; // Immediately update the zone if the user changed the zone type to "Table". if (_ocrPage.Zones != null && _ocrPage.Zones.Count > _lbZonesList.SelectedIndex) { _ocrPage.Zones[_lbZonesList.SelectedIndex] = zone; } ZoneToControls((_lbZonesList.SelectedItem != null) ? _lbZonesList.SelectedIndex : -1); UpdateUIState(); DoAction("ZonePropertiesChanged", null); }
private void _annAutomation_AfterObjectChanged(object sender, AnnAfterObjectChangedEventArgs e) { // The annotation object has been changed, update the corresponding zone switch (e.ChangeType) { case AnnObjectChangedType.DesignerEdit: { // The object moved or re-sized, update the bounds ZoneAnnotationObject zoneObject = e.Objects[0] as ZoneAnnotationObject; OcrZone zone = _ocrPage.Zones[zoneObject.ZoneIndex]; zone.Bounds = RestrictZoneBoundsToPage(_ocrPage, BoundsFromAnnotations(zoneObject, _annAutomation.Container)); bool zoneChanged = false; if (_ocrPage.Zones[zoneObject.ZoneIndex].Bounds != zone.Bounds) { zoneChanged = true; } _ocrPage.Zones[zoneObject.ZoneIndex] = zone; if (zoneChanged) { _rasterImageViewer.BeginUpdate(); // We should mark the page as unrecognized since we updated its zones _ocrPage.Unrecognize(); // Update the thumbnail(s) DoAction("RefreshPagesControl", false); _rasterImageViewer.EndUpdate(); } } break; } }
static void Main(string[] args) { string licenseFilePath = @"LEADTOOLS.lic"; string developerKey = @"mcxvXsdTqZbnbQrDM9FSk5+RAsBJLhAIot2m3qdpoDO8oK7YMWOw1z6YpXqhCnFE"; RasterSupport.SetLicense(licenseFilePath, developerKey); // Assuming you added "using Leadtools.Codecs;", "using Leadtools.Forms.Ocr;" and "using Leadtools.Forms.DocumentWriters;" at the beginning of this class // *** Step 1: Select the engine type and create an instance of the IOcrEngine interface. // We will use the LEADTOOLS OCR Advantage engine and use it in the same process IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Professional, false); // *** Step 2: Startup the engine. // Use the default parameters ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS 19\Bin\Common\OcrProfessionalRuntime64"); // *** Step 3: Create an OCR document with one or more pages. IOcrDocument ocrDocument = ocrEngine.DocumentManager.CreateDocument(); // Add all the pages of a multi-page TIF image to the document ocrDocument.Pages.AddPages(@"C:\Users\Public\Documents\LEADTOOLS Images\OCR1.tif", 1, -1, null); // *** Step 4: Establish zones on the page(s), either manually or automatically // Automatic zoning ocrDocument.Pages.AutoZone(null); // *** Step 5: (Optional) Set the active languages to be used by the OCR engine // Enable English and German languages ocrEngine.LanguageManager.EnableLanguages(new string[] { "en", "de" }); // *** Step 6: (Optional) Set the spell checking engine // Enable the spell checking system ocrEngine.SpellCheckManager.SpellCheckEngine = OcrSpellCheckEngine.Native; // *** Step 7: (Optional) Set any special recognition module options // Change the zone method for the first zone in the first page to be Graphics so it will not be recognized OcrZone ocrZone = ocrDocument.Pages[0].Zones[0]; ocrZone.ZoneType = OcrZoneType.Text; ocrDocument.Pages[0].Zones[0] = ocrZone; // *** Step 8: Recognize ocrDocument.Pages.Recognize(null); // *** Step 9: Save recognition results // Save the results to a PDF file ocrDocument.Save(@"C:\Users\Public\Documents\LEADTOOLS Images\Document.pdf", DocumentFormat.Pdf, null); ocrDocument.Dispose(); // *** Step 10: Shut down the OCR engine when finished ocrEngine.Shutdown(); ocrEngine.Dispose(); }
/// <summary> /// Called from the main form when the zones are updated /// </summary> public void ZonesUpdated() { if (_ocrPage == null) { return; } // Stop updating the viewer _rasterImageViewer.BeginUpdate(); // Remove all the annotations objects and re-add them from the zones _annAutomation.Cancel(); _ignoreAddRemove = true; _annAutomation.Container.Children.Clear(); _setSelect = true; // Get the rectangle automation object so we can use the template // to create the new annotation objects bool isVisible = _showZonesToolStripButton.Checked && !MainForm.PerspectiveDeskewActive && !MainForm.UnWarpActive; bool isNameVisible = _showZoneNameToolStripButton.Checked; ZoneAnnotationObjectRenderer zoneObjectRenderer = (ZoneAnnotationObjectRenderer)_annAutomationManager.RenderingEngine.Renderers[AnnObject.UserObjectId]; zoneObjectRenderer.OcrPage = _ocrPage; for (int i = 0; i < _ocrPage.Zones.Count; i++) { OcrZone zone = _ocrPage.Zones[i]; ZoneAnnotationObject zoneObject = new ZoneAnnotationObject(); zoneObject.SetZone(_ocrPage, i, isVisible, isNameVisible); _annAutomation.Container.Children.Add(zoneObject); // Now we can calculate the object bounds correctly OcrZoneCell[] cells = null; cells = _ocrPage.Zones.GetZoneCells(zone); LeadRect rc = zone.Bounds; LeadRectD rect = BoundsToAnnotations(zoneObject, rc, _annAutomation.Container); zoneObject.Rect = rect; if (cells != null) { foreach (OcrZoneCell cell in cells) { LeadRect r = cell.Bounds; LeadRectD rd = BoundsToAnnotations(zoneObject, r, _annAutomation.Container); cell.Bounds = BoundsToAnnotations(zoneObject, r, _annAutomation.Container).ToLeadRect(); } zoneObject.Cells = cells; } } _ignoreAddRemove = false; // Re-update the viewer _rasterImageViewer.EndUpdate(); _rasterImageViewer.Invalidate(); UpdateUIState(); }
void AddZones(bool bUserDrawnZone) { try { if (bUserDrawnZone && (_frameRect.Width < 2 || _frameRect.Height < 2)) { return; } // Initialize the OcrZone and add it to the image. OcrZone zoneData = new OcrZone(); MyItemData itemData = (MyItemData)_cmbOcrModules.SelectedItem; OcrZoneType selectedModule = (OcrZoneType)itemData.ZoneType; switch (selectedModule) { case OcrZoneType.Text: // AUTO if (bUserDrawnZone) { zoneData.Bounds = _frameRect; zoneData.ZoneType = OcrZoneType.Text; _ocrPage.Zones.Add(zoneData); } else { _ocrPage.AutoZone(null); } break; case OcrZoneType.Micr: // MICR if (bUserDrawnZone) { zoneData.Bounds = _frameRect; } else { zoneData.Bounds = new LeadRect(38, 678, 1655, 87); } zoneData.ZoneType = OcrZoneType.Micr; _ocrPage.Zones.Add(zoneData); break; case OcrZoneType.Omr: // OMR if (bUserDrawnZone) { zoneData.Bounds = _frameRect; zoneData.ZoneType = OcrZoneType.Omr; _ocrPage.Zones.Add(zoneData); } else { _ocrPage.LoadZones(Path.Combine(ImagesFolder, "Mix_omr.ozf")); } break; case OcrZoneType.Icr: // HandPrintedCharacter if (bUserDrawnZone) { zoneData.Bounds = _frameRect; } else { zoneData.Bounds = new LeadRect(0, 0, _ocrPage.Width, _ocrPage.Height); } zoneData.ZoneType = OcrZoneType.Icr; zoneData.CharacterFilters = (OcrZoneCharacterFilters)itemData.CharacterFilters; _ocrPage.Zones.Add(zoneData); break; } } catch (Exception ex) { Messager.ShowError(this, ex); } }
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; }
public void ClearZoneCells(int zoneIndex) { OcrZone zone = _ocrPage.Zones[zoneIndex]; _ocrPage.Zones.SetZoneCells(zone, null); }
private void _areaTextBox_LostFocus(object sender, EventArgs e) { if (_tvZonesList.SelectedNode == null) { return; } // Make sure it is an integer and in valid range OcrZone zone = CurrentZone; LeadRect bounds = zone.Bounds; TextBox tb = sender as TextBox; int val; if (!int.TryParse(tb.Text, out val) || val < 0) { ResetBoundsValue(tb, bounds); return; } LeadRect newBounds = bounds; // Calculate the new bounds if (tb == _leftTextBox) { newBounds.X = val; } else if (tb == _topTextBox) { newBounds.Y = val; } else if (tb == _widthTextBox) { if (val == 0) { ResetBoundsValue(tb, bounds); return; } newBounds.Width = val; } else if (tb == _heightTextBox) { if (val == 0) { ResetBoundsValue(tb, bounds); return; } newBounds.Height = val; } // Make sure the new bounds does not go outside the page LeadRect pageBounds = new LeadRect(0, 0, _ocrPage.Width, _ocrPage.Height); if (!pageBounds.Contains(newBounds)) { ResetBoundsValue(tb, bounds); return; } // Valid value, update the bounds zone.Bounds = newBounds; CurrentZone = zone; OcrZoneCell[] cells = null; cells = _zones.GetZoneCells(_zones[(int)_tvZonesList.SelectedNode.Tag]); if (_zones[(int)_tvZonesList.SelectedNode.Tag].ZoneType == OcrZoneType.Table && cells != null && _oldValue != val) { _tvZonesList.SelectedNode.Nodes.Clear(); _viewerControl.ClearZoneCells((int)_tvZonesList.SelectedNode.Tag); } }
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 void _areaTextBox_LostFocus(object sender, EventArgs e) { if (_lbZonesList.SelectedItems.Count == 0 || _lbZonesList.SelectedItems.Count > 1) { return; } // Make sure it is an integer and in valid range OcrZone zone = CurrentZone; LeadRect bounds = zone.Bounds; TextBox tb = sender as TextBox; int val; if (!int.TryParse(tb.Text, out val) || val < 0) { ResetBoundsValue(tb, bounds); return; } LeadRect newBounds = bounds; // Calculate the new bounds if (tb == _leftTextBox) { newBounds.X = val; } else if (tb == _topTextBox) { newBounds.Y = val; } else if (tb == _widthTextBox) { if (val == 0) { ResetBoundsValue(tb, bounds); return; } newBounds.Width = val; } else if (tb == _heightTextBox) { if (val == 0) { ResetBoundsValue(tb, bounds); return; } newBounds.Height = val; } // Make sure the new bounds does not go outside the page LeadRect pageBounds = new LeadRect(0, 0, _ocrPage.Width, _ocrPage.Height); if (!pageBounds.Contains(newBounds)) { ResetBoundsValue(tb, bounds); return; } // Valid value, update the bounds zone.Bounds = newBounds; CurrentZone = zone; }
public List <FilledForm> ProcessOcr(ResultsForPrettyJson formResults, List <ImageInfo> fileInfos) { try { var outDir = formResults.OriginalDirectoryName; var retForms = new List <FilledForm>(); var usedMasters = new HashSet <MasterForm>(); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); formResults.PagesInPdf = fileInfos.Count; foreach (var ofi in fileInfos) { FilledForm newForm = new FilledForm(); retForms.Add(newForm); newForm.ImageInfoMaster.InitialImage = ofi; newForm.Name = Path.GetFileNameWithoutExtension(ofi.ImageFileInfo.Name); if (ofi.Image == null) { ofi.Image = LoadImageFile(ofi.ImageFileInfo.FullName, 1, -1); } //CleanupImage(ofi.Image); var par = new FormThreadCallParams() { ImageInfo = ofi, StopWatch = stopWatch, Form = newForm }; if (PageTimeoutInSeconds < 50) { Thread t = new Thread(this.PrepareNewFormThreader); t.Start(par); if (!t.Join(TimeSpan.FromSeconds(PageTimeoutInSeconds))) { t.Abort(); formResults.TimedOutPages.Add(newForm.Name); formResults.BestFormConfidence.Add(-1); if (formResults.TimedOutPages.Count > 2 && formResults.PagesMappedToForm == 0) { formResults.Status = $"Form abandoned for timeout after {formResults.BestFormConfidence.Count} pages"; logger.Error(formResults.Status); return(retForms); } continue; } } else { PrepareNewFormThreader(par); } Debug.Assert(par.Attributes != null); var filledFormAttributes = par.Attributes; //List<FormRecognitionResult> results = new List<FormRecognitionResult>(); MasterForm currentMasterBlockForm = null; int bestConfidence = -1; int currentConfidence = 85; foreach (var master in BlockMasterForms) { if (usedMasters.Contains(master)) { continue; } var result = RecognitionEngine.CompareForm(master.Attributes, filledFormAttributes, null, null); //logger.Debug($"Check {master} for {newForm} {stopWatch.ElapsedMilliseconds} {result.Confidence}"); if (result.Confidence > currentConfidence) { currentMasterBlockForm = master; bestConfidence = currentConfidence = result.Confidence; } else if (result.Confidence > bestConfidence) { bestConfidence = result.Confidence; } } formResults.BestFormConfidence.Add(bestConfidence); if (currentMasterBlockForm != null) { formResults.MasterFormPages.Add(currentMasterBlockForm.Properties.Name); formResults.PagesMappedToForm++; logger.Info($"FilledForm matched {newForm.Name} {newForm.Status} {stopWatch.ElapsedMilliseconds} "); newForm.ImageInfoMaster.InitialImage = ofi; var centeredImage = ofi.Image.CloneAll(); CleanupImage(centeredImage); newForm.ImageInfoMaster.CenteredImage = new ImageInfo() { Image = centeredImage }; var omrImage = centeredImage.CloneAll(); PrepareOmrImage(omrImage); newForm.ImageInfoMaster.OmrImage = new ImageInfo() { Image = omrImage }; newForm.Status = "Matched"; newForm.Master = currentMasterBlockForm; var alignment = RecognitionEngine.GetFormAlignment(newForm.Master.Attributes, newForm.Attributes, null); var fields = currentMasterBlockForm.ProcessingPages[0]; var scaler = currentMasterBlockForm.Resolution; var fieldsOnlyImage = RasterImage.Create(centeredImage.Width, centeredImage.Height, centeredImage.BitsPerPixel, 300, RasterColor.White); //fieldsOnlyImage = new RasterImage(RasterMemoryFlags.Conventional, centeredImage.Width, centeredImage.Height, centeredInage.BitsPerPixel, RasterByteOrder.Rgb, RasterViewPerspective.TopLeft, null, null, 0); var subDirField = Path.Combine(outDir, "fields"); var fileNameFieldOnly = Path.Combine(subDirField, newForm.Name + "_fields.jpg"); var googleResultsFile = Path.Combine(subDirField, newForm.Name + "_google.json"); var combined = false; foreach (var field in fields) { var isBlock = field.Name.Contains("block"); var rect200 = alignment[0].AlignRectangle(field.Bounds); scaler = 300; int fudge = isBlock ? 30 : 1; var rect300 = new LeadRect(rect200.Left * 300 / scaler - fudge, rect200.Top * 300 / scaler - fudge, rect200.Width * 300 / scaler + fudge, rect200.Height * 300 / scaler + fudge); try { var imageInfoToUse = newForm.ImageInfoMaster.CenteredImage; var zoneType = OcrZoneType.Text; if (field.GetType() == typeof(OmrFormField)) { imageInfoToUse = newForm.ImageInfoMaster.OmrImage; zoneType = OcrZoneType.Omr; } else if (field.GetType() == typeof(ImageFormField)) { zoneType = OcrZoneType.Graphic; } var image = imageInfoToUse.Image.CloneAll(); var subDir = Path.Combine(outDir, isBlock ? "blocks" : "fields"); var fileName = Path.Combine(subDir, newForm.Name + "_" + field.Name + ".jpg"); var imageField = new ImageField { Field = field, FieldResult = { FieldName = field.Name, IsBlock = isBlock, ImageFile = fileName, Bounds = rect300.ToString(), FieldType = zoneType.ToString(), Error = "None" } }; imageField.Rectangle = new Rectangle(rect300.X, rect300.Y, rect300.Width, rect300.Height); try { EnsurePathExists(subDir); CropCommand command = new CropCommand { Rectangle = rect300 }; command.Run(image); RasterCodecs.Save(image, fileName, RasterImageFormat.Jpeg, bitsPerPixel: 8); if (!isBlock && zoneType == OcrZoneType.Text && !combined) { try { ; var combiner = new CombineCommand(); //combiner.DestinationImage = fieldsOnlyImage; combiner.SourceImage = image.Clone(); combiner.DestinationRectangle = rect300; var regionBounds = image.GetRegionBounds(null); combiner.SourcePoint = new LeadPoint(regionBounds.X, regionBounds.Y); //combiner.Flags = CombineCommandFlags.OperationAdd | CombineCommandFlags.Destination0 | CombineCommandFlags.Source1 | CombineCommandFlags.Destination0 ; combiner.Flags = CombineCommandFlags.OperationOr | CombineCommandFlags.Destination0;; // |CombineFastCommandFlags.OperationAverage; combiner.Run(fieldsOnlyImage); //combined = true; } catch (Exception exCombine) { logger.Error(exCombine, $"error combining field {field.Name} {rect300}"); } } var imageInfo = new ImageInfo() { Image = image, ImageFileInfo = new FileInfo(fileName) }; imageField.ImageInfo = imageInfo; if (!isBlock && zoneType != OcrZoneType.Graphic) { using (IOcrPage ocrPage = OcrEngine.CreatePage(image, OcrImageSharingMode.AutoDispose)) { OcrZone ocrZone = new OcrZone { ZoneType = zoneType, Bounds = new LeadRect(fudge, fudge, image.ImageSize.Width - fudge, image.ImageSize.Height - fudge) }; ocrPage.Zones.Add(ocrZone); ocrPage.Recognize(null); if (zoneType == OcrZoneType.Omr) { if (field.Name.Contains("C2NGVD1929")) { logger.Info(ocrZone.Bounds); } GetOmrReading(ocrPage, field, imageField); } else if (zoneType == OcrZoneType.Text) { var resultsPage = GetPageConfidence(ocrPage); imageField.FieldResult.Confidence = resultsPage.Confidence; char[] crlf = { '\r', '\n' }; imageField.FieldResult.Text = ocrPage.GetText(0).TrimEnd(crlf); } } } logger.Info( $"field {field.Name} {rect300} [{imageField.FieldResult.Text}] confidence: {imageField.FieldResult.Confidence}"); } catch (Exception exField) { logger.Error(exField, $"Error processing {field.Name}"); formResults.FieldsWithError++; imageField.FieldResult.Error = exField.Message; } newForm.ImageFields.Add(imageField); formResults.OcrFields.Add(imageField.FieldResult); formResults.Status = "FormMatched"; } catch (Exception ex) { logger.Error(ex, $"Error on field {field.Name} {rect300}"); newForm.Status = $"Error|Field {field.Name} {rect300}: [{ex.Message}]"; } } RasterCodecs.Save(PrepareOmrImage(fieldsOnlyImage), fileNameFieldOnly, RasterImageFormat.Jpeg, bitsPerPixel: 8); var googleResults = GoogleOcr(fileNameFieldOnly); if (googleResults.Count > 0) { var json = JsonConvert.SerializeObject(googleResults, Formatting.Indented); File.WriteAllText(googleResultsFile, json); MergeGoogleOcr(newForm, googleResults); } usedMasters.Add(currentMasterBlockForm); } else { newForm.Status = "Unmatched|No MasterForm match"; } logger.Info($"FilledForm processed {newForm.Name} {newForm.Status} {stopWatch.ElapsedMilliseconds} "); if (usedMasters.Count == BlockMasterForms.Count) { logger.Info("found all master forms"); break; } } stopWatch.Stop(); return(retForms); } catch (Exception ex) { logger.Error(ex, "Untrapped error found"); return(null); } }