This class contains the form recognition engine that creates, compares, and aligns forms.
public class FormRecognitionEngine
Public Class FormRecognitionEngine
public ref class FormRecognitionEngine
This class is used to create Master Forms and Forms, to compare a form with a Master Form, and to calculate the alignment for a Form in relation to a specified Master Form.
Important: Different OCR engines might have slight differences in form alignment and recognition. Therefore, it is recommended to use the same Leadtools.Forms.Ocr.OcrEngineType when generating the master forms and when recognizing and processing forms.
This example shows a complete skeleton of a form recognition and processing application.
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports Leadtools
Imports Leadtools.Barcode
Imports Leadtools.Codecs
Imports Leadtools.Forms
Imports Leadtools.Forms.Ocr
Imports Leadtools.Forms.Processing
Imports Leadtools.Forms.Recognition
Imports Leadtools.Forms.Recognition.Barcode
Imports Leadtools.Forms.Recognition.Ocr
Imports Leadtools.Forms.Recognition.Search
''' This class contains all Master Form information.
Class MasterForm
Public Image As RasterImage
Public Attributes As FormRecognitionAttributes
Public Properties As FormRecognitionProperties = FormRecognitionProperties.Empty
Public ProcessingPages As FormPages
Public Sub New()
End Sub
End Class
''' This class contains all MyForm information. MyForm is the filled form that is going to be identified and processed by the application.
Class MyForm
Public FileName As String
Public Image As RasterImage
Public Attributes As FormRecognitionAttributes
Public Master As MasterForm
Public Result As FormRecognitionResult
Public Alignment As IList(Of PageAlignment)
Public ProcessingPages As FormPages
Public Sub New()
End Sub
End Class
''' TestForms is a class that has all the needed methods to recognize and process unknown forms. Once an instance of it is created StartUp should be called first to initialize the
''' OCR and barcode engines in addition to building the Master Forms set. Next, call the Run method to recognize and process a specific form. Finally, call the ShutDown method
''' once the program is terminated.
Class TestForms
' LEADTOOLS Forms Recognition engine
Private recognitionEngine As FormRecognitionEngine
' LEADTOOLS Forms Processing engine
Private processingEngine As FormProcessingEngine
' LEADTOOLS Barcode Engine
Private barcodeEngine As BarcodeEngine
' LEADTOOLS OCR Engine
Private ocrEngine As IOcrEngine
' Master Forms set
Private masterFormSet As IList(Of MasterForm)
' Repository name, only used with full text search manager
Private Const repositoryName As String = "My Master Forms"
' Options
' Use full text search manager
Private useFullTextSearchManager As Boolean
' Only recognize the first page in the form
Private recognizeFirstPageOnly As Boolean
' Load images from a scanner
Private loadFromScanner As Boolean
'===========================================================================
''' Load an image from a scanner.
Public Function LoadImageScanner(count As Integer) As RasterImage
' TODO: Refer to Leadtools.Twain documentation
Return Nothing
End Function
''' Load an image from a scanner for recognition.
Public Function LoadRecognitionImageScanner() As RasterImage
' TODO: Refer to Leadtools.Twain documentation
Return Nothing
End Function
' Create a RasterCodecs instance to use for a image load/save operation
Public Shared Function CreateRasterCodecs() As RasterCodecs
' Create an instance suitable for document processing
Const resolution As Integer = 300
Dim rasterCodecs As New RasterCodecs()
rasterCodecs.Options.Load.Resolution = resolution
rasterCodecs.Options.RasterizeDocument.Load.Resolution = resolution
Return rasterCodecs
End Function
''' Load an image from an image file.
Public Function LoadImageFile(fileName As String, firstPage As Integer, lastPage As Integer) As RasterImage
Using rasterCodecs As RasterCodecs = CreateRasterCodecs()
Dim image As RasterImage = rasterCodecs.Load(fileName, 1, CodecsLoadByteOrder.Bgr, firstPage, lastPage)
Return image
End Using
End Function
'===========================================================================
''' Load an image from a file for recognition.
Public Function LoadRecognitionImageFile(fileName As String) As RasterImage
Using rasterCodecs As RasterCodecs = CreateRasterCodecs()
Dim image As RasterImage
If recognizeFirstPageOnly Then
image = LoadImageFile(fileName, 1, 1)
Else
image = LoadImageFile(fileName, 1, -1)
End If
Return image
End Using
End Function
''' Load an image for recognition.
Public Function LoadRecognitionImage(fileName As String) As RasterImage
Dim image As RasterImage
If recognizeFirstPageOnly Then
image = LoadRecognitionImageScanner()
Else
image = LoadRecognitionImageFile(fileName)
End If
Return image
End Function
'===========================================================================
''' Create form attributes object for recognition.
Public Function CreateForm() As FormRecognitionAttributes
Dim attributes As FormRecognitionAttributes = recognitionEngine.CreateForm(Nothing)
recognitionEngine.CloseForm(attributes)
Return attributes
End Function
''' Add a page to a form attributes object for recognition.
Public Sub AddPageToForm(image As RasterImage, attributes As FormRecognitionAttributes)
recognitionEngine.OpenForm(attributes)
recognitionEngine.AddFormPage(attributes, image, Nothing)
recognitionEngine.CloseForm(attributes)
End Sub
''' Delete a page from a form attributes object for recognition.
Public Sub DeletePageFromForm(pagenumber As Integer, form As FormRecognitionAttributes)
recognitionEngine.OpenForm(form)
recognitionEngine.DeleteFormPage(form, pagenumber)
recognitionEngine.CloseForm(form)
End Sub
''' Create a form along with its pages for recognition.
Public Sub CreateFormForRecognition(form As MyForm)
form.Attributes = CreateForm()
Dim saveCurrentPageIndex As Integer = form.Image.Page
For i As Integer = 1 To form.Image.PageCount
form.Image.Page = i
AddPageToForm(form.Image, form.Attributes)
Next
form.Image.Page = saveCurrentPageIndex
End Sub
'===========================================================================
''' Create a Master Form recognition attributes object.
Public Function CreateMasterForm(name As String) As FormRecognitionAttributes
Dim attributes As FormRecognitionAttributes = recognitionEngine.CreateMasterForm(name, Guid.Empty, Nothing)
recognitionEngine.CloseMasterForm(attributes)
Return attributes
End Function
''' Adds a page to a Master Form recognition attributes object.
Public Sub AddPageToMasterForm(image As RasterImage, attributes As FormRecognitionAttributes)
recognitionEngine.OpenMasterForm(attributes)
recognitionEngine.AddMasterFormPage(attributes, image, Nothing)
' This method will add (if they do not exist) or update the master form properties in the database
If Not recognitionEngine.FullTextSearchManager Is Nothing Then
recognitionEngine.UpsertMasterFormToFullTextSearch(attributes, repositoryName, Nothing, Nothing, Nothing, Nothing)
End If
recognitionEngine.CloseMasterForm(attributes)
End Sub
''' Delete a page from a Master Form recognition attributes object.
Public Sub DeletePageFromMasterForm(pagenumber As Integer, form As FormRecognitionAttributes)
recognitionEngine.OpenMasterForm(form)
recognitionEngine.DeleteMasterFormPage(form, pagenumber)
recognitionEngine.CloseMasterForm(form)
' If we are using the full search text engine, then re-index
If Not recognitionEngine.FullTextSearchManager Is Nothing Then
recognitionEngine.FullTextSearchManager.Index()
End If
End Sub
''' Create a Master Form with its pages for recognition purposes.
Public Sub CreateMasterFormForRecognition(name As String, form As MasterForm)
form.Attributes = CreateMasterForm(name)
Dim saveCurrentPageIndex As Integer = form.Image.Page
For i As Integer = 1 To form.Image.PageCount
form.Image.Page = i
AddPageToMasterForm(form.Image, form.Attributes)
Next
form.Image.Page = saveCurrentPageIndex
' If we are using the full search text engine, then re-index
If Not recognitionEngine.FullTextSearchManager Is Nothing Then
recognitionEngine.FullTextSearchManager.Index()
End If
End Sub
'===========================================================================
''' Compare a Form to a Master Form using all pages.
Private Function CompareForm(master As FormRecognitionAttributes, form As FormRecognitionAttributes) As FormRecognitionResult
Return recognitionEngine.CompareForm(master, form, Nothing)
End Function
''' Compare the first page of a Form to the first page of a Master Form.
Private Function CompareFirstPage(master As FormRecognitionAttributes, form As FormRecognitionAttributes) As FormRecognitionResult
Dim resultPage As PageRecognitionResult = recognitionEngine.ComparePage(master, 1, form, 1)
Dim result As New FormRecognitionResult()
result.Confidence = resultPage.Confidence
result.LargestConfidencePageNumber = 1
result.PageResults.Add(resultPage)
result.Reason = FormRecognitionReason.Success
Return result
End Function
''' Identify the type of the form based on the comparison results.
Public Function IdentifyForm(results() As FormRecognitionResult) As Integer
Dim maxIndex As Integer = 0
For i = 1 To results.Length - 1
If (results(maxIndex).Confidence < results(i).Confidence) Then
maxIndex = i
End If
Next
If (results(maxIndex).Confidence < 30) Then
maxIndex = -1 ' no match
End If
Return maxIndex
End Function
''' Recognize the type of an unknown form.
Public Sub RecognizeForm(form As MyForm)
CreateFormForRecognition(form)
Dim masterFormsToUse As IList(Of MasterForm) = Nothing
' Check if full text search is enabled, if so, use it
If Not recognitionEngine.FullTextSearchManager Is Nothing Then
' RecognitionEngine.FullTextSearchMinimumRank and RecognitionEngine.FullTextSearchMaximumCandidates control the candidates selection process
' RecognitionEngine.FullTextSearchMinimumRank value of -1 (default) automatically select the best candidates, otherwise, returns the
' candidates that have at least equals to FullTextSearchMinimumRank. This value xxx
' RecognitionEngine.FullTextSearchMaximumCandidates the maximum possible number of candidates to return, by default this value is 3
Dim candidates As IList(Of FullTextSearchItem) = recognitionEngine.GetFullTextSearchCandidatesForm(form.Attributes, repositoryName, Nothing, Nothing, Nothing)
If Not candidates Is Nothing AndAlso candidates.Count > 0 Then
' Use only good candidates from the list
masterFormsToUse = New List(Of MasterForm)()
For Each candidate As FullTextSearchItem In candidates
' Find it in the master forms
For Each masterForm As MasterForm In masterFormSet
If String.Compare(masterForm.Properties.Name, candidate.FormName) = 0 Then
masterFormsToUse.Add(masterForm)
End If
Next
Next
Else
' This happens because of one or more of the following reasons:
' 1. The user did not add the master form to the database
' 2. The minimum rank is set too high
' 3. The page or the form does not have text, i.e., it may have only barcode or shapes
' 4. The scanned document is not part of the master forms
'
' To handle reason #1, #2 and #3 set the masterFormToUse to use all the form set:
'masterFormsToUse = FormSet;
' But in our case, we will assume the system is setup correctly and it is case 4, there was no match, so we will just return:
Return
End If
Else
masterFormsToUse = masterFormSet
End If
Dim results As FormRecognitionResult() = New FormRecognitionResult(masterFormsToUse.Count) {}
For i As Integer = 0 To masterFormsToUse.Count - 1
If recognizeFirstPageOnly Then
results(i) = CompareFirstPage(masterFormsToUse(i).Attributes, form.Attributes)
Else
results(i) = CompareForm(masterFormsToUse(i).Attributes, form.Attributes)
End If
Next
Dim index As Integer = IdentifyForm(results)
If index >= 0 Then
form.Master = masterFormsToUse(index)
form.Result = results(index)
Else
form.Master = Nothing
form.Result = Nothing
End If
End Sub
'===========================================================================
''' Load images for processing purposes if needed.
Private Function LoadProcessingImage(form As MyForm) As Boolean
Dim count As Integer = form.Master.Properties.Pages - form.Image.PageCount
If (count = 0) Then
Return False
End If
Dim image As RasterImage
If loadFromScanner Then
image = LoadImageScanner(count)
Else
image = LoadImageFile(form.FileName, form.Image.PageCount + 1, form.Image.PageCount + count)
End If
form.Image.AddPages(image, 1, count)
Return True
End Function
'===========================================================================
''' Calculate the alignment for the recognized form.
Public Sub AlignForm(form As MyForm, calculateAlignment As Boolean)
If calculateAlignment Then
CreateFormForRecognition(form)
form.Alignment = recognitionEngine.GetFormAlignment(form.Master.Attributes, form.Attributes, Nothing)
Else
form.Alignment = New List(Of PageAlignment)()
For i As Integer = 0 To form.Result.PageResults.Count - 1
form.Alignment.Add(form.Result.PageResults(i).Alignment)
Next
End If
End Sub
'===========================================================================
''' Process the recognized form.
Public Sub ProcessForm(form As MyForm)
form.ProcessingPages = form.Master.ProcessingPages
processingEngine.Pages.Clear()
processingEngine.Pages.AddRange(form.ProcessingPages)
processingEngine.Process(form.Image, form.Alignment)
End Sub
'===========================================================================
''' Recognize the unknown form then processes it.
Public Sub RunFormRecognitionAndProcessing(form As MyForm)
form.Image = LoadRecognitionImage(form.FileName)
RecognizeForm(form)
If form.Master Is Nothing Then
Console.WriteLine("Unknown form")
Return
End If
Dim calculateAlignment As Boolean = LoadProcessingImage(form)
AlignForm(form, calculateAlignment)
ProcessForm(form)
PrintOutResults(form)
End Sub
'===========================================================================
''' Load the specified master form attributes object, fields, and image.
Public Function LoadMasterForm(attributesFileName As String, fieldsFileName As String, imageFileName As String) As MasterForm
Dim formData() As Byte = File.ReadAllBytes(attributesFileName)
Dim form As New MasterForm()
form.Attributes = New FormRecognitionAttributes()
form.Attributes.SetData(formData)
form.Properties = recognitionEngine.GetFormProperties(form.Attributes)
processingEngine.LoadFields(fieldsFileName)
form.ProcessingPages = processingEngine.Pages
Using rasterCodecs As RasterCodecs = CreateRasterCodecs()
form.Image = rasterCodecs.Load(imageFileName, 1, CodecsLoadByteOrder.Bgr, 1, -1)
End Using
Return form
End Function
''' Save the master form attributes object to the specified filename.
Public Shared Sub SaveMasterFormAttributes(form As MasterForm, attributesFileName As String)
Dim formData() As Byte = form.Attributes.GetData()
File.WriteAllBytes(attributesFileName, formData)
End Sub
''' Save master form fields to the specified filename.
Public Sub SaveMasterFormFields(form As MasterForm, fieldsFileName As String)
processingEngine.Pages.Clear()
processingEngine.Pages.AddRange(form.ProcessingPages)
processingEngine.SaveFields(fieldsFileName)
End Sub
'===========================================================================
''' Build the Master Form Set.
Public Sub BuildMasterFormsSet()
masterFormSet = New List(Of MasterForm)
Dim form107 As MasterForm = LoadMasterForm( _
Path.Combine(LEAD_VARS.ImagesDir, "Forms\MasterForm Sets\OCR\FFC-107.bin"), _
Path.Combine(LEAD_VARS.ImagesDir, "Forms\MasterForm Sets\OCR\FFC-107.xml"), _
Path.Combine(LEAD_VARS.ImagesDir, "Forms\MasterForm Sets\OCR\FFC-107.tif"))
masterFormSet.Add(form107)
Dim form180 As MasterForm = LoadMasterForm( _
Path.Combine(LEAD_VARS.ImagesDir, "Forms\MasterForm Sets\OCR\SF-180.bin"), _
Path.Combine(LEAD_VARS.ImagesDir, "Forms\MasterForm Sets\OCR\SF-180.xml"), _
Path.Combine(LEAD_VARS.ImagesDir, "Forms\MasterForm Sets\OCR\SF-180.tif"))
masterFormSet.Add(form180)
Dim form3881 As MasterForm = LoadMasterForm( _
Path.Combine(LEAD_VARS.ImagesDir, "Forms\MasterForm Sets\OCR\SF-3881.bin"), _
Path.Combine(LEAD_VARS.ImagesDir, "Forms\MasterForm Sets\OCR\SF-3881.xml"), _
Path.Combine(LEAD_VARS.ImagesDir, "Forms\MasterForm Sets\OCR\SF-3881.tif"))
masterFormSet.Add(form3881)
End Sub
'===========================================================================
''' Print the image field results
Public Shared Sub PrintOutImage(field As ImageFormField)
Console.WriteLine()
Console.WriteLine("*** Image Field")
Dim result As ImageFormFieldResult = DirectCast(field.Result, ImageFormFieldResult)
Using rasterCodecs As RasterCodecs = CreateRasterCodecs()
rasterCodecs.Save(result.Image, Path.Combine(LEAD_VARS.ImagesDir, "ImageField.tif"), RasterImageFormat.Tif, 1)
End Using
End Sub
''' Print the barcode field results.
Public Shared Sub PrintOutBarcode(field As BarcodeFormField)
Console.WriteLine()
Console.WriteLine("*** Barcode Field")
Dim result As BarcodeFormFieldResult = DirectCast(field.Result, BarcodeFormFieldResult)
For Each barcode As BarcodeFieldData In result.BarcodeData
Console.WriteLine(" *- Barcode Type: " + barcode.Symbology.ToString())
Next
End Sub
''' Print the OMR field results.
Public Shared Sub PrintOutOMR(field As OmrFormField)
Console.WriteLine()
Console.WriteLine("*** Omr Field")
Dim result As OmrFormFieldResult = DirectCast(field.Result, OmrFormFieldResult)
Console.WriteLine(" Is Checked? [1 = yes, 0 = No] = " + result.Text)
End Sub
''' Print the text field results.
Public Shared Sub PrintOutText(field As TextFormField)
Console.WriteLine()
Console.WriteLine("*** Text Field")
Dim result As TextFormFieldResult = DirectCast(field.Result, TextFormFieldResult)
Console.WriteLine("Text: " + result.Text)
End Sub
''' Print the processing results.
Public Shared Sub PrintOutResults(form As MyForm)
If form.Result.Confidence < 30 Then
Console.WriteLine("No match found")
Return
End If
Console.WriteLine("=======================Type===========================")
Console.WriteLine("Form Type is: " + form.Master.Properties.Name)
Console.WriteLine("Confidence = " + form.Result.Confidence.ToString())
Console.WriteLine()
Console.WriteLine("====================Fields Result=======================")
For pageIndex As Integer = 0 To form.ProcessingPages.Count
Console.WriteLine()
Console.WriteLine("=====Page # " + form.ProcessingPages(pageIndex).PageNumber.ToString() + "=====")
Console.WriteLine()
For Each field As FormField In form.ProcessingPages(pageIndex)
If (TypeOf field Is ImageFormField) Then
PrintOutImage(DirectCast(field, ImageFormField))
ElseIf (TypeOf field Is BarcodeFormField) Then
PrintOutBarcode(DirectCast(field, BarcodeFormField))
ElseIf (TypeOf field Is OmrFormField) Then
PrintOutOMR(DirectCast(field, OmrFormField))
ElseIf (TypeOf field Is TextFormField) Then
PrintOutText(DirectCast(field, TextFormField))
End If
Next
Next
End Sub
'===========================================================================
''' Startup the OCR engine.
Private Sub StartUpOcrEngine()
Try
ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, False)
ocrEngine.Startup(CreateRasterCodecs(), Nothing, Nothing, LEAD_VARS.OcrAdvantageRuntimeDir)
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
''' Shutdown the OCR engine.
Private Sub ShutDownOcrEngine()
ocrEngine.Dispose()
End Sub
''' Startup the Barcode engine.
Private Sub StartUpBarcodeEngine()
Try
barcodeEngine = New BarcodeEngine()
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
''' Set up the object managers.
Public Sub SetObjectManagers(enableDefault As Boolean, enableOcr As Boolean, enableBarcode As Boolean)
If recognitionEngine Is Nothing Then
Return
End If
If enableDefault Then
Dim defaultManager As New DefaultObjectsManager()
recognitionEngine.ObjectsManagers.Add(defaultManager)
End If
If enableOcr Then
Dim ocrManager As New OcrObjectsManager(ocrEngine)
recognitionEngine.ObjectsManagers.Add(ocrManager)
End If
If enableBarcode Then
Dim barcodeManager As New BarcodeObjectsManager(barcodeEngine)
recognitionEngine.ObjectsManagers.Add(barcodeManager)
End If
End Sub
''' Startup all engines.
Public Sub StartUpEngines()
StartUpOcrEngine()
StartUpBarcodeEngine()
recognitionEngine = New FormRecognitionEngine()
' See if we need to use a full text search manager
If useFullTextSearchManager Then
' Or use the SQL full text search manager
Dim indexDirectory As String = Path.Combine(LEAD_VARS.ImagesDir, "FullTextIndex")
Dim diskFullTextSearchManager As New DiskFullTextSearchManager()
diskFullTextSearchManager.IndexDirectory = indexDirectory
recognitionEngine.FullTextSearchManager = diskFullTextSearchManager
End If
SetObjectManagers(False, True, False)
processingEngine = New FormProcessingEngine()
processingEngine.OcrEngine = ocrEngine
processingEngine.BarcodeEngine = barcodeEngine
End Sub
''' Shutdown all engines.
Public Sub ShutDownEngines()
ShutDownOcrEngine()
End Sub
''' Startup up all engines, setup the options and build the form set.
Public Sub StartUp()
'1)
useFullTextSearchManager = True
StartUpEngines()
'2)
recognizeFirstPageOnly = False
loadFromScanner = False
'3)
BuildMasterFormsSet()
End Sub
''' Shutdown all engines and dispose of all images
Public Sub ShutDown()
'1)
ShutDownEngines()
'2)
For Each master As MasterForm In masterFormSet
If Not master.Image Is Nothing Then
master.Image.Dispose()
End If
Next
End Sub
''' Start the recognition process on the unknown form that is stored in a file with the passed fileName.
Public Sub Run(fileName As String)
Dim form As New MyForm()
form.FileName = fileName
RunFormRecognitionAndProcessing(form)
If Not form.Image Is Nothing Then
form.Image.Dispose()
End If
End Sub
End Class
Class Program
''' The main entry to the program.
Shared Sub Main(args() As String)
If args Is Nothing OrElse args.Length = 0 Then
Return
End If
Dim test As New TestForms()
test.StartUp()
test.Run(args(0))
test.ShutDown()
End Sub
End Class
Public NotInheritable Class LEAD_VARS
Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"
Public Const OcrAdvantageRuntimeDir As String = "C:\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime"
End Class
using System;
using System.Collections.Generic;
using System.IO;
using Leadtools;
using Leadtools.Barcode;
using Leadtools.Codecs;
using Leadtools.Forms;
using Leadtools.Forms.Ocr;
using Leadtools.Forms.Processing;
using Leadtools.Forms.Recognition;
using Leadtools.Forms.Recognition.Barcode;
using Leadtools.Forms.Recognition.Ocr;
using Leadtools.Forms.Recognition.Search;
/// This class contains all Master Form information.
class MasterForm
{
public RasterImage Image;
public FormRecognitionAttributes Attributes;
public FormRecognitionProperties Properties = FormRecognitionProperties.Empty;
public FormPages ProcessingPages;
public MasterForm()
{
}
}
/// This class contains all MyForm information. MyForm is the filled form that is going to be identified and processed by the application.
class MyForm
{
public string FileName;
public RasterImage Image;
public FormRecognitionAttributes Attributes;
public MasterForm Master;
public FormRecognitionResult Result;
public IList<PageAlignment> Alignment;
public FormPages ProcessingPages;
public MyForm()
{
}
}
/// TestForms is a class that has all the needed methods to recognize and process unknown forms. Once an instance of it is created StartUp should be called first to initialize the
/// OCR and barcode engines in addition to building the Master Forms set. Next, call the Run method to recognize and process a specific form. Finally, call the ShutDown method
/// once the program is terminated.
class TestForms
{
// LEADTOOLS Forms Recognition engine
private FormRecognitionEngine recognitionEngine;
// LEADTOOLS Forms Processing engine
private FormProcessingEngine processingEngine;
// LEADTOOLS Barcode Engine
private BarcodeEngine barcodeEngine;
// LEADTOOLS OCR Engine
private IOcrEngine ocrEngine;
// Master Forms set
private IList<MasterForm> masterFormSet;
// Repository name, only used with full text search manager
private const string repositoryName = "My Master Forms";
// Options
// Use full text search manager
private bool useFullTextSearchManager;
// Only recognize the first page in the form
private bool recognizeFirstPageOnly;
// Load images from a scanner
private bool loadFromScanner;
//===========================================================================
/// Load an image from a scanner.
public RasterImage LoadImageScanner(int count)
{
// TODO: Refer to Leadtools.Twain documentation
return null;
}
/// Load an image from a scanner for recognition.
public RasterImage LoadRecognitionImageScanner()
{
// TODO: Refer to Leadtools.Twain documentation
return null;
}
// Create a RasterCodecs instance to use for a image load/save operation
public static RasterCodecs CreateRasterCodecs()
{
// Create an instance suitable for document processing
const int resolution = 300;
RasterCodecs rasterCodecs = new RasterCodecs();
rasterCodecs.Options.Load.Resolution = resolution;
rasterCodecs.Options.RasterizeDocument.Load.Resolution = resolution;
return rasterCodecs;
}
/// Load an image from an image file.
public RasterImage LoadImageFile(string fileName, int firstPage, int lastPage)
{
using (RasterCodecs rasterCodecs = CreateRasterCodecs())
{
RasterImage image = rasterCodecs.Load(fileName, 1, CodecsLoadByteOrder.Bgr, firstPage, lastPage);
return image;
}
}
//===========================================================================
/// Load an image from a file for recognition.
public RasterImage LoadRecognitionImageFile(string fileName)
{
using (RasterCodecs rasterCodecs = CreateRasterCodecs())
{
RasterImage image;
if (recognizeFirstPageOnly)
image = LoadImageFile(fileName, 1, 1);
else
image = LoadImageFile(fileName, 1, -1);
return image;
}
}
/// Load an image for recognition.
public RasterImage LoadRecognitionImage(string fileName)
{
RasterImage image;
if (recognizeFirstPageOnly)
image = LoadRecognitionImageScanner();
else
image = LoadRecognitionImageFile(fileName);
return image;
}
//===========================================================================
/// Create form attributes object for recognition.
public FormRecognitionAttributes CreateForm()
{
FormRecognitionAttributes attributes = recognitionEngine.CreateForm(null);
recognitionEngine.CloseForm(attributes);
return attributes;
}
/// Add a page to a form attributes object for recognition.
public void AddPageToForm(RasterImage image, FormRecognitionAttributes attributes)
{
recognitionEngine.OpenForm(attributes);
recognitionEngine.AddFormPage(attributes, image, null);
recognitionEngine.CloseForm(attributes);
}
/// Delete a page from a form attributes object for recognition.
public void DeletePageFromForm(int pagenumber, FormRecognitionAttributes form)
{
recognitionEngine.OpenForm(form);
recognitionEngine.DeleteFormPage(form, pagenumber);
recognitionEngine.CloseForm(form);
}
/// Create a form along with its pages for recognition.
public void CreateFormForRecognition(MyForm form)
{
form.Attributes = CreateForm();
int saveCurrentPageIndex = form.Image.Page;
for (int i = 1; i <= form.Image.PageCount; i++)
{
form.Image.Page = i;
AddPageToForm(form.Image, form.Attributes);
}
form.Image.Page = saveCurrentPageIndex;
}
//===========================================================================
/// Create a Master Form recognition attributes object.
public FormRecognitionAttributes CreateMasterForm(string name)
{
FormRecognitionAttributes attributes = recognitionEngine.CreateMasterForm(name, Guid.Empty, null);
recognitionEngine.CloseMasterForm(attributes);
return attributes;
}
/// Adds a page to a Master Form recognition attributes object.
public void AddPageToMasterForm(RasterImage image, FormRecognitionAttributes attributes)
{
recognitionEngine.OpenMasterForm(attributes);
recognitionEngine.AddMasterFormPage(attributes, image, null);
// This method will add (if they do not exist) or update the master form properties in the database
if (recognitionEngine.FullTextSearchManager != null)
recognitionEngine.UpsertMasterFormToFullTextSearch(attributes, repositoryName, null, null, null, null);
recognitionEngine.CloseMasterForm(attributes);
}
/// Delete a page from a Master Form recognition attributes object.
public void DeletePageFromMasterForm(int pagenumber, FormRecognitionAttributes form)
{
recognitionEngine.OpenMasterForm(form);
recognitionEngine.DeleteMasterFormPage(form, pagenumber);
recognitionEngine.CloseMasterForm(form);
// If we are using the full search text engine, then re-index
if (recognitionEngine.FullTextSearchManager != null)
recognitionEngine.FullTextSearchManager.Index();
}
/// Create a Master Form with its pages for recognition purposes.
public void CreateMasterFormForRecognition(string name, MasterForm form)
{
form.Attributes = CreateMasterForm(name);
int saveCurrentPageIndex = form.Image.Page;
for (int i = 1; i <= form.Image.PageCount; i++)
{
form.Image.Page = i;
AddPageToMasterForm(form.Image, form.Attributes);
}
form.Image.Page = saveCurrentPageIndex;
// If we are using the full search text engine, then re-index
if (recognitionEngine.FullTextSearchManager != null)
recognitionEngine.FullTextSearchManager.Index();
}
//===========================================================================
/// Compare a Form to a Master Form using all pages.
private FormRecognitionResult CompareForm(FormRecognitionAttributes master, FormRecognitionAttributes form)
{
return recognitionEngine.CompareForm(master, form, null);
}
/// Compare the first page of a Form to the first page of a Master Form.
private FormRecognitionResult CompareFirstPage(FormRecognitionAttributes master, FormRecognitionAttributes form)
{
PageRecognitionResult resultPage = recognitionEngine.ComparePage(master, 1, form, 1);
FormRecognitionResult result = new FormRecognitionResult();
result.Confidence = resultPage.Confidence;
result.LargestConfidencePageNumber = 1;
result.PageResults.Add(resultPage);
result.Reason = FormRecognitionReason.Success;
return result;
}
/// Identify the type of the form based on the comparison results.
public int IdentifyForm(FormRecognitionResult[] results)
{
int maxIndex = 0;
for (int i = 1; i < results.Length; i++)
{
if (results[maxIndex].Confidence < results[i].Confidence)
maxIndex = i;
}
if (results[maxIndex].Confidence < 30)
maxIndex = -1; // no match
return maxIndex;
}
/// Recognize the type of an unknown form.
public void RecognizeForm(MyForm form)
{
CreateFormForRecognition(form);
IList<MasterForm> masterFormsToUse = null;
// Check if full text search is enabled, if so, use it
if (recognitionEngine.FullTextSearchManager != null)
{
// RecognitionEngine.FullTextSearchMinimumRank and RecognitionEngine.FullTextSearchMaximumCandidates control the candidates selection process
// RecognitionEngine.FullTextSearchMinimumRank value of -1 (default) automatically select the best candidates, otherwise, returns the
// candidates that have at least equals to FullTextSearchMinimumRank. This value xxx
// RecognitionEngine.FullTextSearchMaximumCandidates the maximum possible number of candidates to return, by default this value is 3
IList<FullTextSearchItem> candidates = recognitionEngine.GetFullTextSearchCandidatesForm(form.Attributes, repositoryName, null, null, null);
if (candidates != null && candidates.Count > 0)
{
// Use only good candidates from the list
masterFormsToUse = new List<MasterForm>();
foreach (FullTextSearchItem candidate in candidates)
{
// Find it in the master forms
foreach (MasterForm masterForm in masterFormSet)
{
if (string.Compare(masterForm.Properties.Name, candidate.FormName) == 0)
{
masterFormsToUse.Add(masterForm);
}
}
}
}
else
{
// This happens because of one or more of the following reasons:
// 1. The user did not add the master form to the database
// 2. The minimum rank is set too high
// 3. The page or the form does not have text, i.e., it may have only barcode or shapes
// 4. The scanned document is not part of the master forms
// To handle reason #1, #2 and #3 set the masterFormToUse to use all the form set:
//masterFormsToUse = FormSet;
// But in our case, we will assume the system is setup correctly and it is case 4, there was no match, so we will just return:
return;
}
}
else
{
masterFormsToUse = masterFormSet;
}
FormRecognitionResult[] results = new FormRecognitionResult[masterFormsToUse.Count];
for (int i = 0; i < masterFormsToUse.Count; i++)
{
if (recognizeFirstPageOnly)
results[i] = CompareFirstPage(masterFormsToUse[i].Attributes, form.Attributes);
else
results[i] = CompareForm(masterFormsToUse[i].Attributes, form.Attributes);
}
int index = IdentifyForm(results);
if (index >= 0)
{
form.Master = masterFormsToUse[index];
form.Result = results[index];
}
else
{
form.Master = null;
form.Result = null;
}
}
//===========================================================================
/// Load images for processing purposes if needed.
private bool LoadProcessingImage(MyForm form)
{
int count = form.Master.Properties.Pages - form.Image.PageCount;
if (count == 0)
return false;
RasterImage image;
if (loadFromScanner)
image = LoadImageScanner(count);
else
image = LoadImageFile(form.FileName, form.Image.PageCount + 1, form.Image.PageCount + count);
form.Image.AddPages(image, 1, count);
return true;
}
//===========================================================================
/// Calculate the alignment for the recognized form.
public void AlignForm(MyForm form, bool calculateAlignment)
{
if (calculateAlignment)
{
CreateFormForRecognition(form);
form.Alignment = recognitionEngine.GetFormAlignment(form.Master.Attributes, form.Attributes, null);
}
else
{
form.Alignment = new List<PageAlignment>();
for (int i = 0; i < form.Result.PageResults.Count; i++)
form.Alignment.Add(form.Result.PageResults[i].Alignment);
}
}
//===========================================================================
/// Process the recognized form.
public void ProcessForm(MyForm form)
{
form.ProcessingPages = form.Master.ProcessingPages;
processingEngine.Pages.Clear();
processingEngine.Pages.AddRange(form.ProcessingPages);
processingEngine.Process(form.Image, form.Alignment);
}
//===========================================================================
/// Recognize the unknown form then processes it.
public void RunFormRecognitionAndProcessing(MyForm form)
{
form.Image = LoadRecognitionImage(form.FileName);
RecognizeForm(form);
if (form.Master == null)
{
Console.WriteLine("Unknown form");
return;
}
bool calculateAlignment = LoadProcessingImage(form);
AlignForm(form, calculateAlignment);
ProcessForm(form);
PrintOutResults(form);
}
//===========================================================================
/// Load the specified master form attributes object, fields, and image.
public MasterForm LoadMasterForm(string attributesFileName, string fieldsFileName, string imageFileName)
{
byte[] formData = File.ReadAllBytes(attributesFileName);
MasterForm form = new MasterForm();
form.Attributes = new FormRecognitionAttributes();
form.Attributes.SetData(formData);
form.Properties = recognitionEngine.GetFormProperties(form.Attributes);
processingEngine.LoadFields(fieldsFileName);
form.ProcessingPages = processingEngine.Pages;
using (RasterCodecs rasterCodecs = CreateRasterCodecs())
form.Image = rasterCodecs.Load(imageFileName, 1, CodecsLoadByteOrder.Bgr, 1, -1);
return form;
}
/// Save the master form attributes object to the specified filename.
public static void SaveMasterFormAttributes(MasterForm form, string attributesFileName)
{
byte[] formData = form.Attributes.GetData();
File.WriteAllBytes(attributesFileName, formData);
}
/// Save master form fields to the specified filename.
public void SaveMasterFormFields(MasterForm form, string fieldsFileName)
{
processingEngine.Pages.Clear();
processingEngine.Pages.AddRange(form.ProcessingPages);
processingEngine.SaveFields(fieldsFileName);
}
//===========================================================================
/// Build the Master Form Set.
public void BuildMasterFormsSet()
{
masterFormSet = new List<MasterForm>();
MasterForm form107 = LoadMasterForm(
Path.Combine(LEAD_VARS.ImagesDir, @"Forms\MasterForm Sets\OCR\FFC-107.bin"),
Path.Combine(LEAD_VARS.ImagesDir, @"Forms\MasterForm Sets\OCR\FFC-107.xml"),
Path.Combine(LEAD_VARS.ImagesDir, @"Forms\MasterForm Sets\OCR\FFC-107.tif"));
masterFormSet.Add(form107);
MasterForm form180 = LoadMasterForm(
Path.Combine(LEAD_VARS.ImagesDir, @"Forms\MasterForm Sets\OCR\SF-180.bin"),
Path.Combine(LEAD_VARS.ImagesDir, @"Forms\MasterForm Sets\OCR\SF-180.xml"),
Path.Combine(LEAD_VARS.ImagesDir, @"Forms\MasterForm Sets\OCR\SF-180.tif"));
masterFormSet.Add(form180);
MasterForm form3881 = LoadMasterForm(
Path.Combine(LEAD_VARS.ImagesDir, @"Forms\MasterForm Sets\OCR\SF-3881.bin"),
Path.Combine(LEAD_VARS.ImagesDir, @"Forms\MasterForm Sets\OCR\SF-3881.xml"),
Path.Combine(LEAD_VARS.ImagesDir, @"Forms\MasterForm Sets\OCR\SF-3881.tif"));
masterFormSet.Add(form3881);
}
//===========================================================================
/// Print the image field results
public static void PrintOutImage(ImageFormField field)
{
Console.WriteLine();
Console.WriteLine("*** Image Field");
ImageFormFieldResult result = field.Result as ImageFormFieldResult;
using (RasterCodecs rasterCodecs = CreateRasterCodecs())
rasterCodecs.Save(result.Image, Path.Combine(LEAD_VARS.ImagesDir, "ImageField.tif"), RasterImageFormat.Tif, 1);
}
/// Print the barcode field results.
public static void PrintOutBarcode(BarcodeFormField field)
{
Console.WriteLine();
Console.WriteLine("*** Barcode Field");
BarcodeFormFieldResult result = field.Result as BarcodeFormFieldResult;
foreach (BarcodeFieldData barcode in result.BarcodeData)
{
Console.WriteLine(" *- Barcode Type: " + barcode.Symbology);
}
}
/// Print the OMR field results.
public static void PrintOutOMR(OmrFormField field)
{
Console.WriteLine();
Console.WriteLine("*** Omr Field");
OmrFormFieldResult result = field.Result as OmrFormFieldResult;
Console.WriteLine(" Is Checked? [1 = yes, 0 = No] = " + result.Text);
}
/// Print the text field results.
public static void PrintOutText(TextFormField field)
{
Console.WriteLine();
Console.WriteLine("*** Text Field");
TextFormFieldResult result = field.Result as TextFormFieldResult;
Console.WriteLine("Text: " + result.Text);
}
/// Print the processing results.
public static void PrintOutResults(MyForm form)
{
if (form.Result.Confidence < 30)
{
Console.WriteLine("No match found");
return;
}
Console.WriteLine("=======================Type===========================");
Console.WriteLine("Form Type is: " + form.Master.Properties.Name);
Console.WriteLine("Confidence = " + form.Result.Confidence);
Console.WriteLine();
Console.WriteLine("====================Fields Result=======================");
for (int pageIndex = 0; pageIndex < form.ProcessingPages.Count; pageIndex++)
{
Console.WriteLine();
Console.WriteLine("=====Page # " + form.ProcessingPages[pageIndex].PageNumber + "=====");
Console.WriteLine();
foreach (FormField field in form.ProcessingPages[pageIndex])
{
if (field is ImageFormField)
PrintOutImage(field as ImageFormField);
else if (field is BarcodeFormField)
PrintOutBarcode(field as BarcodeFormField);
else if (field is OmrFormField)
PrintOutOMR(field as OmrFormField);
else if (field is TextFormField)
PrintOutText(field as TextFormField);
}
}
}
//===========================================================================
/// Startup the OCR engine.
private void StartUpOcrEngine()
{
try
{
ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, false);
ocrEngine.Startup(CreateRasterCodecs(), null, null, LEAD_VARS.OcrAdvantageRuntimeDir);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
/// Shutdown the OCR engine.
private void ShutDownOcrEngine()
{
ocrEngine.Dispose();
}
/// Startup the Barcode engine.
private void StartUpBarcodeEngine()
{
try
{
barcodeEngine = new BarcodeEngine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
/// Set up the object managers.
public void SetObjectManagers(bool enableDefault, bool enableOcr, bool enableBarcode)
{
if (recognitionEngine == null)
return;
if (enableDefault)
{
DefaultObjectsManager defaultManager = new DefaultObjectsManager();
recognitionEngine.ObjectsManagers.Add(defaultManager);
}
if (enableOcr)
{
OcrObjectsManager ocrManager = new OcrObjectsManager(ocrEngine);
recognitionEngine.ObjectsManagers.Add(ocrManager);
}
if (enableBarcode)
{
BarcodeObjectsManager barcodeManager = new BarcodeObjectsManager(barcodeEngine);
recognitionEngine.ObjectsManagers.Add(barcodeManager);
}
}
/// Startup all engines.
public void StartUpEngines()
{
StartUpOcrEngine();
StartUpBarcodeEngine();
recognitionEngine = new FormRecognitionEngine();
// See if we need to use a full text search manager
if (useFullTextSearchManager)
{
// Or use the SQL full text search manager
string indexDirectory = Path.Combine(LEAD_VARS.ImagesDir, @"FullTextIndex");
DiskFullTextSearchManager diskFullTextSearchManager = new DiskFullTextSearchManager();
diskFullTextSearchManager.IndexDirectory = indexDirectory;
recognitionEngine.FullTextSearchManager = diskFullTextSearchManager;
}
SetObjectManagers(false, true, false);
processingEngine = new FormProcessingEngine();
processingEngine.OcrEngine = ocrEngine;
processingEngine.BarcodeEngine = barcodeEngine;
}
/// Shutdown all engines.
public void ShutDownEngines()
{
ShutDownOcrEngine();
}
/// Startup up all engines, setup the options and build the form set.
public void StartUp()
{
//1)
useFullTextSearchManager = true;
StartUpEngines();
//2)
recognizeFirstPageOnly = false;
loadFromScanner = false;
//3)
BuildMasterFormsSet();
}
/// Shutdown all engines and dispose of all images
public void ShutDown()
{
//1)
ShutDownEngines();
//2)
foreach (MasterForm master in masterFormSet)
{
if (master.Image != null)
master.Image.Dispose();
}
}
/// Start the recognition process on the unknown form that is stored in a file with the passed fileName.
public void Run(string fileName)
{
MyForm form = new MyForm();
form.FileName = fileName;
RunFormRecognitionAndProcessing(form);
if (form.Image != null)
form.Image.Dispose();
}
}
class Program
{
/// The main entry to the program.
static void Main(string[] args)
{
if (args == null || args.Length == 0)
return;
TestForms test = new TestForms();
test.StartUp();
test.Run(args[0]);
test.ShutDown();
}
}
static class LEAD_VARS
{
public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";
public const string OcrAdvantageRuntimeDir = @"C:\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime";
}
Products |
Support |
Feedback: FormRecognitionEngine Class - Leadtools.Forms.Recognition |
Introduction |
Help Version 19.0.2017.3.22
|
Raster .NET | C API | C++ Class Library | JavaScript HTML5
Document .NET | C API | C++ Class Library | JavaScript HTML5
Medical .NET | C API | C++ Class Library | JavaScript HTML5
Medical Web Viewer .NET
Your email has been sent to support! Someone should be in touch! If your matter is urgent please come back into chat.
Chat Hours:
Monday - Friday, 8:30am to 6pm ET
Thank you for your feedback!
Please fill out the form again to start a new chat.
All agents are currently offline.
Chat Hours:
Monday - Friday
8:30AM - 6PM EST
To contact us please fill out this form and we will contact you via email.