Auto Recognize and Process a Form - Python

This tutorial shows how to create a set of Master Forms, then automatically recognize and process a form in a Python application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to recognize and process a form using the High-Level AutoFormsEngine class in a Python application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (2 KB)
Platform Python Console Application
IDE Visual Studio 2022
Runtime Target Python 3.10 or Higher
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Auto Recognize and Process a Form - Python tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project.

This tutorial requires the following DLLs:

For a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Initialize AutoFormsEngine, RasterCodes, Iocr_engine, and DiskMasterFormsRepository

With the project created, the references added, and the license set, coding can begin.

In the Solution Explorer, open Project-Name.pyand place the following references below the "Add references to LEADTOOLS" comment

# Add references to LEADTOOLS 
from leadtools import LibraryLoader 
LibraryLoader.add_reference("Leadtools") 
from Leadtools import * 
LibraryLoader.add_reference("Leadtools.Codecs") 
from Leadtools.Codecs import * 
LibraryLoader.add_reference("Leadtools.Document") 
from Leadtools.Document import * 
LibraryLoader.add_reference("Leadtools.Forms.Auto") 
from Leadtools.Forms.Auto import * 
from Leadtools.Forms.Processing import * 
from Leadtools.Forms.Recognition import *  
from Leadtools.Ocr import * 
from System.IO import * 

Add a new method to the Project-Name.py file named init_forms_engines(). Call the init_forms_engines() method inside the main() method below the set license call, as shown below.

def main(): 
 
    Support.set_license(os.path.join(DemosTools.get_root(), "C:/LEADTOOLS22/Support/Common/License")) 
 
    init_forms_engines() 

Add the code below to the init_forms_engines() method to initialize the AutoFormsEngine, RasterCodecs, Iocr_engine, and DiskMasterFormsRepository objects.

Python  
def init_forms_engines(): 
     
    codecs = RasterCodecs() 
 
    global ocr_engine 
    ocr_engine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD) 
    ocr_engine.Startup(codecs, None, None, r"C:\LEADTOOLS22\Bin\Common\OcrLEADRuntime") 
   
    forms_repository = DiskMasterFormsRepository(codecs, r"C:\LEADTOOLS22\Resources\Images\Forms\MasterForm Sets\OCR") 
     
    global auto_engine 
    auto_engine = AutoFormsEngine(forms_repository, ocr_engine, None, AutoFormsRecognitionManager.Default and AutoFormsRecognitionManager.Ocr, 30, 80, True) 
    print("Engines initialized successfully!") 

Add the Forms Recognition and Processing Code

In the Project-Name.py file add two new methods named recognize_and_process_form() and show_processed_results(run_result). Call the recognize_and_process_form() method inside the main() method after the init_forms_engines() method, as shown in the next section. Call the show_processed_results() method, passing run_result as the overload, at the bottom of the recognize_and_process_form() method, as shown below.

Add the code below to the recognize_and_process_form() method to recognize the given form based on the Master Forms and process the form to extract the desired information.

def recognize_and_process_form(): 
     
    result_message = "Form not recognized" 
    form_to_recognize = r"C:\LEADTOOLS22\Resources\Images\Forms\Forms to be Recognized\OCR\W9_OCR_Filled.tif" 
 
    run_result = auto_engine.Run(form_to_recognize, None) 
 
    if (run_result != None):  
        recognition_result = run_result.RecognitionResult.Result 
        result_message = f"This form has been recognized as a {run_result.RecognitionResult.MasterForm.Name} with {recognition_result.Confidence} confidence." 
    print("Recognition Results: ") 
    print(result_message) 
    print("============================== 

Add the code below to the show_processed_results() method to display the processed results from the recognize_and_process_form() method to the console.

def show_processed_results(run_result): 
    if (run_result == None): 
        return 
    results_message = "" 
     
    for form_page in run_result.FormFields: 
        for field in form_page: 
            if (field != None): 
                results_message = f"{results_message}{field.Name} = {field.Result.Text}\n" 
 
    print("Field Processing Results:") 
    if not results_message: 
        print("No fields were processed") 
    else: 
        print(results_message) 

Note

Shipped and installed with the LEADTOOLS SDK are sample master form sets and sample filled forms for recognition and processing. This tutorial uses these samples. The sample files are installed at <INSTALL_DIR>\LEADTOOLS22\Resources\Images\Forms.

Shut Down the OCR Engine

In the main() method, add auto_engine.Dispose() and ocr_engine.Shutdown() code under the recognize_and_process_form() method to properly dispose of the AutoFormsEngine and shut down the OCR engine. The main section should now look like:

def main(): 
 
    Support.set_license(os.path.join(DemosTools.get_root(), "C:/LEADTOOLS22/Support/Common/License")) 
 
    init_forms_engines() 
    recognize_and_process_form() 
     
    auto_engine.Dispose() 
    if (ocr_engine != None and ocr_engine.IsStarted): 
        ocr_engine.Shutdown() 

Handling Streams

If you would like to load the form using memory stream, then add the following code to the recognize_and_process_form() method under string form_to_recognize = @"C:\LEADTOOLS22\Resources\Images\Forms\Forms to be Recognized\OCR\W9_OCR_Filled.tif";:

stream = File.OpenRead(form_to_recognize) 
options = LoadDocumentOptions() 
document = DocumentFactory.LoadFromStream(stream, options) 
     
# Console commands to double check that the document was loaded properly 
print(document.DocumentId) 
print("Document Loaded") 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the console appears and displays the following data results:

  1. Recognized form and confidence value (e.g., W9 with 98% confidence.)
  2. Processed filled-in data (e.g., Business Name, Address, etc.)

The console displays the recognized form and the processed results from the filled form.

For this example, a W-9 form was used. It was correctly recognized, with a confidence of 98% (where 0 means no confidence and 100% means complete confidence).

Wrap-up

This tutorial showed how to create a master forms set using the DiskMasterFormsRepository class, and then recognized and processed a filled form using the AutoFormsEngine class.

See Also

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


Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.