Recognizing A Form From A Set Of Master Forms

Take the following steps to start a project and to add some code that creates a set of master forms and uses them to recognize a filled form:

  1. Start Visual Studio.
  2. Choose File->New->Project... from the menu.
  3. In the New Project dialog box, choose either "Visual C# Projects" or "VB Projects" in the Projects Type List, and choose "Windows Application" in the Templates List.
  4. Type the project name as "Recognizing A Form" in the Project Name field, and then choose OK. If desired, type a new location for your project or select a directory using the Browse button, and then choose OK.
  5. In the "Solution Explorer" window, right-click on the "References" folder, and select "Add Reference…" from the context menu. In the Add Reference dialog box, select the ".NET" tab, browse to the "C:\LEADTOOLS 20\Bin\Dotnet4\Win32" folder and select the following DLLs:

    • Leadtools.dll
    • Leadtools.Codecs.dll
    • Leadtools.Codecs.Fax.dll
    • Leadtools.Codecs.Tif.dll
    • Leadtools.Forms.Common.dll
    • Leadtools.Document.Writer.dll
    • Leadtools.Ocr.dll
    • Leadtools.Ocr.LEADEngine.dll
    • Leadtools.Forms.Recognition.dll
    • Leadtools.Forms.Recognition.Ocr.dll
  6. Add the following lines at the top of the Form1.cs/Form1.vb file:

    C#
    using Leadtools; 
    using Leadtools.Codecs; 
    using Leadtools.Forms.Common; 
    using Leadtools.Ocr; 
    using Leadtools.Forms.Recognition; 
    using Leadtools.Forms.Recognition.Ocr; 
    using System.IO;  
    VB
    Imports Leadtools 
    Imports Leadtools.Codecs 
    Imports Leadtools.Forms.Common 
    Imports Leadtools.Ocr  
    Imports Leadtools.Forms.Recognition 
    Imports Leadtools.Forms.Recognition.Ocr 
    Imports System.IO 

  7. Add the following members to the Form1 class:

    C#
    //Create the recognition engine 
    FormRecognitionEngine recognitionEngine = new FormRecognitionEngine(); 
    RasterCodecs codecs; 
    //Create the OCR Engine to use in the recognition 
    IOcrEngine formsOCREngine; 
    VB
    'Create the recognition engine 
    Dim recognitionEngine As FormRecognitionEngine = New FormRecognitionEngine() 
    Dim codecs As RasterCodecs 
    'Create the OCR Engine to use in the recognition 
    Dim formsOCREngine As IOcrEngine 

  8. Add the following code to the Load event of the form (Form1_Load()):

    C#
    try 
     { 
        string MY_LICENSE_FILE = "d:\\temp\\TestLic.lic"; 
                         
        // Unlock support 
        string MY_DEVELOPER_KEY = "xyz123abc"; 
        RasterSupport.SetLicense(MY_LICENSE_FILE, MY_DEVELOPER_KEY); 
                         
        codecs = new RasterCodecs(); 
        //Create a LEADTOOLS OCR Module - LEAD Engine and start it 
        formsOCREngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD, false); 
        formsOCREngine.Startup(codecs, null, null, @"C:\LEADTOOLS 20\Bin\Common\OcrLEADRuntime"); 
        //Add an OCRObjectManager to the recognition engines 
        //ObjectManager collection 
        OcrObjectsManager ocrObjectsManager = new OcrObjectsManager(formsOCREngine); 
        ocrObjectsManager.Engine = formsOCREngine; 
        recognitionEngine.ObjectsManagers.Add(ocrObjectsManager); 
     } 
     catch (Exception ex) 
     { 
        MessageBox.Show(ex.Message); 
     }  
    VB
    Try 
       Dim MY_LICENSE_FILE As String = "d:\temp\TestLic.lic" 
                        
       ' Unlock support 
       Dim MY_DEVELOPER_KEY As String = "xyz123abc" 
       RasterSupport.SetLicense(MY_LICENSE_FILE, MY_DEVELOPER_KEY) 
     
       codecs = New RasterCodecs() 
       'Create a LEADTOOLS OCR Module - LEAD Engine OCR Engine and start it 
       formsOCREngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD, False) 
       formsOCREngine.Startup(codecs, Nothing, Nothing, "C:\LEADTOOLS 20\Bin\Common\OcrLEADRuntime") 
       'Add an OCRObjectManager to the recognition engines 
       'ObjectManager collection 
       Dim ocrObjectsManager As OcrObjectsManager = New OcrObjectsManager(formsOCREngine) 
       ocrObjectsManager.Engine = formsOCREngine 
       recognitionEngine.ObjectsManagers.Add(ocrObjectsManager) 
    Catch ex As Exception 
       MessageBox.Show(ex.Message) 
    End Try 

  9. LEADTOOLS ships several master form images which we will use for this tutorial. The path to the images is hard-coded to the default install location so if you chose another path, you will need to change it accordingly. Add a button to the form and set its Text property to "Create Master Form Attributes" then double-click it and add the following code to its handler:

    C#
    try 
    { 
       //Get master form filenames 
       //You may need to update the below path to point to the "Leadtools Images\Forms\MasterForm Sets\OCR" directory. 
       string [] masterFileNames = Directory.GetFiles(@"C:\Users\Public\Documents\LEADTOOLS Images\Forms\MasterForm Sets\OCR", 
                                                          "*.tif", 
                                                          SearchOption.AllDirectories); 
       foreach (string masterFileName in masterFileNames) 
       { 
          string formName = Path.GetFileNameWithoutExtension(masterFileName); 
          //Load the master form image 
          RasterImage image = codecs.Load(masterFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, -1); 
          //Create a new master form 
          FormRecognitionAttributes masterFormAttributes = recognitionEngine.CreateMasterForm(formName, Guid.Empty, null); 
          for (int i = 0; i < image.PageCount; i++) 
          { 
             image.Page = i + 1; 
             //Add the master form page to the recognition engine 
             recognitionEngine.AddMasterFormPage(masterFormAttributes, image, null); 
          } 
          //Close the master form and save it's attributes 
          recognitionEngine.CloseMasterForm(masterFormAttributes); 
          File.WriteAllBytes(formName + ".bin", masterFormAttributes.GetData()); 
       } 
       MessageBox.Show("Master Form Processing Complete", "Complete"); 
    } 
    catch(Exception ex) 
    { 
       MessageBox.Show(ex.Message); 
    } 
    VB
                      
    Try 
       'Get master form filenames 
       'You may need to update the below path to point to the "Leadtools Images\Forms\MasterForm Sets\OCR" directory. 
       Dim masterFileNames As String() = Directory.GetFiles("C:\Users\Public\Documents\LEADTOOLS Images\Forms\MasterForm Sets\OCR", 
                                                                "*.tif", 
                                                                SearchOption.AllDirectories) 
       For Each masterFileName As String In masterFileNames 
          Dim formName As String = Path.GetFileNameWithoutExtension(masterFileName) 
          'Load the master form image  
          Dim image As RasterImage = codecs.Load(masterFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, -1) 
          'Create a new master form 
          Dim masterFormAttributes As FormRecognitionAttributes = recognitionEngine.CreateMasterForm(formName, Guid.Empty, Nothing) 
          Dim i As Integer = 0 
          Do While i < image.PageCount 
             image.Page = i + 1 
             'Add the master form page to the recognition engine 
             recognitionEngine.AddMasterFormPage(masterFormAttributes, image, Nothing) 
             i += 1 
          Loop 
          'Close the master form and save it's attributes 
          recognitionEngine.CloseMasterForm(masterFormAttributes) 
          File.WriteAllBytes(formName & ".bin", masterFormAttributes.GetData()) 
       Next masterFileName 
       MessageBox.Show("Master Form Processing Complete", "Complete") 
    Catch ex As Exception  
       MessageBox.Show(ex.Message) 
    End Try 

  10. LEADTOOLS ships several filled form images for each master form which we will use for this tutorial. Add a button to the form and set its Text property to "Recognize Form", then double-click it and add the following code to its handler:

    C#
    try 
    { 
       //For this tutorial, we will use the sample W9 filled form. 
       //You may need to update the below path to point to "\LEADTOOLS Images\Forms\Forms to be Recognized\OCR\W9_OCR_Filled.tif". 
       string formToRecognize = @"C:\Users\Public\Documents\LEADTOOLS Images\Forms\Forms to be Recognized\OCR\W9_OCR_Filled.tif"; 
       RasterImage image = codecs.Load(formToRecognize, 0, CodecsLoadByteOrder.BgrOrGray, 1, -1); 
       //Load the image to recognize 
       FormRecognitionAttributes filledFormAttributes = recognitionEngine.CreateForm(null); 
       for (int i = 0; i < image.PageCount; i++) 
       { 
          image.Page = i + 1; 
          //Add each page of the filled form to the recognition engine 
          recognitionEngine.AddFormPage(filledFormAttributes, image, null); 
       } 
       recognitionEngine.CloseForm(filledFormAttributes); 
       string resultMessage = "The form could not be recognized"; 
       //Compare the attributes of each master form to the attributes of the filled form 
       string[] masterFileNames = Directory.GetFiles(Application.StartupPath, "*.bin"); 
       foreach (string masterFileName in masterFileNames) 
       { 
          FormRecognitionAttributes masterFormAttributes = new FormRecognitionAttributes(); 
          masterFormAttributes.SetData(File.ReadAllBytes(masterFileName)); 
          FormRecognitionResult recognitionResult = recognitionEngine.CompareForm(masterFormAttributes, filledFormAttributes, null); 
          //In this example, we consider a confidence equal to or greater 
          //than 90 to be a match 
          if(recognitionResult.Confidence >= 90) 
          { 
             resultMessage = String.Format("This form has been recognized as a {0}", Path.GetFileNameWithoutExtension(masterFileName)); 
             break; 
          } 
       } 
       MessageBox.Show(resultMessage, "Recognition Results"); 
    } 
    catch (Exception ex) 
    { 
       MessageBox.Show(ex.Message); 
    } 
    VB
    Try 
       'For this tutorial, we will use the sample W9 filled form. 
       'You may need to update the below path to point to "\LEADTOOLS Images\Forms\Forms to be Recognized\OCR\W9_OCR_Filled.tif". 
       Dim formToRecognize As String = "C:\Users\Public\Documents\LEADTOOLS Images\Forms\Forms to be Recognized\OCR\W9_OCR_Filled.tif" 
       Dim image As RasterImage = codecs.Load(formToRecognize, 0, CodecsLoadByteOrder.BgrOrGray, 1, -1) 
       'Load the image to recognize 
       Dim filledFormAttributes As FormRecognitionAttributes = recognitionEngine.CreateForm(Nothing) 
       Dim i As Integer = 0 
       Do While i < image.PageCount image.Page = i + 1 
          'Add each page of the filled form to the recognition engine 
          recognitionEngine.AddFormPage(filledFormAttributes, image, Nothing) 
          i += 1 
       Loop 
       recognitionEngine.CloseForm(filledFormAttributes) 
       Dim resultMessage As String = "The form could not be recognized" 
       'Compare the attributes of each master form to the attributes of the filled form 
       Dim masterFileNames As String() = Directory.GetFiles(Application.StartupPath, "*.bin") 
       For Each masterFileName As String In masterFileNames 
          Dim masterFormAttributes As FormRecognitionAttributes = New FormRecognitionAttributes() 
          masterFormAttributes.SetData(File.ReadAllBytes(masterFileName)) 
          Dim recognitionResult As FormRecognitionResult = recognitionEngine.CompareForm(masterFormAttributes, filledFormAttributes, Nothing) 
          'In this example, we consider a confidence equal to or greater than 90 to be a match 
          If recognitionResult.Confidence >= 90 Then 
             resultMessage = String.Format("This form has been recognized as a {0}", Path.GetFileNameWithoutExtension(masterFileName)) 
             Exit For  
          End If 
       Next masterFileName  
       MessageBox.Show(resultMessage, "Recognition Results") 
    Catch ex As Exception 
          MessageBox.Show(ex.Message) 
    End Try  

  11. Add the below clean-up code to the FormClosing event handler:

    C#
    if (formsOCREngine != null && formsOCREngine.IsStarted) 
       formsOCREngine.Shutdown(); 
    VB
    If Not formsOCREngine Is Nothing AndAlso formsOCREngine.IsStarted 
       Then formsOCREngine.Shutdown() 
    End If  

  12. Run the project and test it. Click the "Create Master Form Attributes" button first which will enumerate all master form images in the directory and save each of their attributes to the application directory. Next, click the "Recognize Form" button which will load a sample form image, generate its attributes, compare them to each master forms attributes, and display the results. NOTE: If you encounter an "Invalid File Format" or "Feature Not Supported" exception, please refer to the topic Invalid File Format/Feature Not Supported.

Help Version 20.0.2020.4.3
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2020 LEAD Technologies, Inc. All Rights Reserved.

LEADTOOLS Imaging, Medical, and Document