Load and Save OCR Zones - Python

This tutorial shows how to load and save OCR zones in a Python console application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to work with OCR zones 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 by reviewing the Add References and Set a License tutorial, before working on the Load and Save OCR Zones - Python tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in this tutorial: Add References and Set a License

If you do not have that project, follow the steps in the relevant tutorial to create it.

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 OCR Zones Code

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

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.Ocr") 
from Leadtools.Ocr import * 
 
from System.IO import *  

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

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

Add the code below to the load_and_save_ocr_zones() method to load OCR zones to an IOcrPage, output the OCR zone properties to the console, add a new OCR zone programmatically, and then export the OCR zones to file.

def load_and_save_ocr_zones(): 
     
    codecs = RasterCodecs() 
    ocr_engine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD) 
 
    ocr_engine.Startup(codecs, None, None, r"C:\LEADTOOLS22\Bin\Common\OcrLEADRuntime") 
    zones_file = r"PATH TO OCR ZONES FILE (OZF FILE)" 
    image_file = r"PATH TO IMAGE TO CREATE OCR PAGE" 
    zones_outfile = r"PATH TO SAVE OCR ZONES FILE (XML FILE)" 
 
    # Load the file as a RasterImage() 
    image = codecs.Load(image_file, 1) 
 
    ocr_page = ocr_engine.CreatePage(image, OcrImageSharingMode(0)) 
 
    print(ocr_page.Zones.Count, "Zones after IOcrPage creation.") 
    ocr_page.LoadZones(zones_file) 
 
    print(ocr_page.Zones.Count, "Zones after loading zones from file.") 
    for ocr_zone in ocr_page.Zones: 
        index = ocr_page.Zones.IndexOf(ocr_zone) 
        print("Zone index:", index) 
        print("ID:", ocr_zone.Id) 
        print("Bounds:", ocr_zone.Bounds) 
        print("ZoneType:", ocr_zone.ZoneType) 
        print("CharacterFilters:", ocr_zone.CharacterFilters) 
        print("----------------------------------") 
    zone = OcrZone() 
    zone.Name = "Custom zone" 
    zone.ZoneType = OcrZoneType.Text 
    zone.Bounds = LeadRect(10, 10, ocr_page.Width -20, 100) 
    ocr_page.Zones.Add(zone) 
 
    print(ocr_page.Zones.Count, "Zones after adding zones manually.") 
 
    ocr_page.SaveZones(zones_outfile) 
 
    print("Zones successfully saved to", zones_outfile) 

Handling Streams

To load the image from a memory stream, use the following code instead of loading directly from the file:

image_data = File.ReadAllBytes(image_file) 
image_stream = MemoryStream(image_data) 
image = codecs.Load(image_stream) 

To load the OCR zones file from memory stream, replace ocr_page.LoadZones(zones_file) with the following code:

zones_data = File.ReadAllBytes(zones_file) 
zones_stream = MemoryStream(zones_data) 
ocr_page.LoadZones(zones_stream) 

To save the OCR zones to a memory stream, use the code below instead of ocr_page.SaveZones(zones_outfile)

zones_out_stream = MemoryStream() 
ocr_page.SaveZones(zones_out_stream) 

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 creates an IOcrPage object, loads OCR zones from the specified file, adds a new OcrZone to the IOcrPage, and then exports the zones in XML to the specified file path.

Wrap-up

This tutorial showed how to work with OCR zones using the OCRZone structure. Also, we covered how to use the IOcrEngine and IOcrPage interfaces.

See Also

Help Version 22.0.2024.3.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.