Detect and Extract MICR - Python

This tutorial shows how to perform MICR detection and recognition in a Python application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to run MICR detection and output the recognition results in a Python Console application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (1 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 and loading an image by reviewing the Add References and Set a License and Load and Save Images tutorials, before working on the Detect and Extract MICR - Python tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the following topic: * Add References and Set a License for Python

If you do not have that project, follow the steps in the relevant 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:

Add the MICR Detection and Bank Check Reader Code

With the project created, the references added, the license set, and the load image code added, coding can begin. The image save code is not necessary for this tutorial, so that code can be commented out or deleted.

In the Solution Explorer, open Project-Name.py and 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.Forms.Commands") 
from Leadtools.Forms.Commands import * 
LibraryLoader.add_reference("Leadtools.ImageProcessing.Core") 
from Leadtools.ImageProcessing.Core import * 
LibraryLoader.add_reference("Leadtools.Ocr") 
from Leadtools.Ocr import * 
 
from System import * 
from System.IO import * 
from System.Text import * 

Add a new method to the Project-Name.py file named run_micr_detection_recognition(image). Call the run_micr_detection_recognition() method inside the main() method below the call to the load_image() method, as shown below. The method's parameter will be the RasterImage loaded in the load_image() method. For the purposes of this tutorial the sample check images in the file paths below are used.

def main(): 
 
    Support.set_license(os.path.join(DemosTools.get_root(), "C:/LEADTOOLS23/Support/Common/License")) 
 
    # CMC7 sample check image, uncomment if wanting to test CMC7 
    #image = load_image(r"C:\LEADTOOLS23\Resources\Images\cmc7.jpg") 
 
    # E13b sample check image 
    image = load_image(r"C:\LEADTOOLS23\Resources\Images\bankcheck.jpg") 
    run_micr_detection_recognition(image) 

Add the code below to the run_micr_detection_recognition() method to run MICR detection, process the results, and display them to the console.

def run_micr_detection_recognition(image): 
    codecs = RasterCodecs() 
    sb = StringBuilder() 
     
    micr_reader = BankCheckReader() 
 
    ocr_engine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD) 
    ocr_engine.Startup(codecs, None, None, r"C:\LEADTOOLS23\Bin\Common\OcrLEADRuntime") 
    micr_reader.OcrEngine = ocr_engine 
 
    # MICR Code Detection searches for E13b MICR font type  
    e13b_cmd = MICRCodeDetectionCommand() 
    e13b_cmd.SearchingZone = LeadRect(0, 0, image.Width, image.Height) 
    e13b_cmd.Run(image) 
 
    # Run CMC7 Detection  
    cmc7_cmd = CMC7CodeDetectionCommand() 
    cmc7_cmd.Run(image) 
     
    # If E13b MICR code found 
    if (e13b_cmd.MICRZone != LeadRect.Empty): 
        micr_reader.MicrFontType = BankCheckMicrFontType.E13b 
        micr_reader.ProcessImage(image) 
    elif (cmc7_cmd.CMC7Zone != LeadRect.Empty): 
        micr_reader.MicrFontType = BankCheckMicrFontType.Cmc7 
        micr_reader.ProcessImage(image) 
         
    else: 
        print("No MICR text detected!") 
        return 
 
    for value in micr_reader.Results: 
        if (value.Key != "Signature"): 
                sb.Append("\n") 
                sb.Append(f"Field Name: {value.Key} \n") 
                sb.Append(f"Field Value: {value.Value.Text} \n") 
        print(sb.ToString()) 
 
    ocr_engine.Shutdown() 

Note: The code snippet above supports functionality for gathering MICR information from an E13b check and a CMC7 check.

Handling Streams

If you would like to detect and extract the data using memory stream, then add the following code to the main() method:

filename = r"C:\LEADTOOLS23\Resources\Images\bankcheck.jpg" 
buffer = File.ReadAllBytes(filename) 
codecs = RasterCodecs() 
ms = MemoryStream(buffer) 
ms.Position = 0; 
image = codecs.Load(ms) 
run_micr_detection_recognition(image) 
# image = load_image(r"C:\LEADTOOLS23\Resources\Images\bankcheck.jpg") 

Note: This code will replace the existing code in the main method under the SetLicense call as seen in the commented out code.

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 the application runs MICR detection and displays to the console the check image's MICR information.

Screenshot of the console displaying the check image's MICR information.

Wrap-up

This tutorial showed how to run MICR detection using the MICRCodeDetectionCommand class and output the recognition results to the console using the BankCheckReader class.

See Also

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

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