This tutorial shows how to detect and extract passport information from a given image in a Python application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to find and extract passport features from an image 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 |
|
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 MRTD - Python tutorial.
Start with a copy of the project created in the Load and Save Images 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:
Leadtools.dll
Leadtools.Codecs.dll
Leadtools.Forms.Commands.dll
Leadtools.Ocr.dll
For a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.
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:
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.Ocr")
from Leadtools.Ocr import *
from System.IO import *
Add a new method to the Project-Name.py
file named extract_mrtd(image)
. Call this method below the call to the load_image()
method inside the main()
method, as shown below.
Ensure that you replace the string value passed into the load_image()
method with the file path to your passport image. For the purposes of this tutorial the following image is used: C:\LEADTOOLS23\Resources\Images\mrz_sample.jpg
def main():
Support.set_license("C:/LEADTOOLS23/Support/Common/License")
image = load_image(r"C:\LEADTOOLS23\Resources\Images\mrz_sample.jpg")
extract_mrtd(image)
Add the code below to the extract_mrtd()
method to process the given image and output the MRTD values if detected.
def extract_mrtd(image):
mrtd_reader = MRTDReader()
# Create the OCR Engine
ocr_engine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD)
ocr_engine.Startup(None, None, None, r"C:\LEADTOOLS23\Bin\Common\OcrLEADRuntime")
mrtd_reader.OcrEngine = ocr_engine
# Process Image
mrtd_reader.ProcessImage(image)
# Output values
if (mrtd_reader.Errors == MRTDErrors.NoError):
for value in mrtd_reader.Results:
print(f"Data Element Field: {value.Key.ToString()}")
print(f"Data Element Value: {value.Value.ReadableValue}")
print(f"Data Element Code: {value.Value.MrzCharacters}")
print(f"Data Element Valid: {value.Value.IsValid}")
print("************************************")
To handle files using memory stream, change the code in the load_image
method to:
def load_image(filename):
codecs = RasterCodecs()
# If Wanting to load from memory
byte = File.ReadAllBytes(filename)
ms = MemoryStream(byte)
ms.Position = 0
return codecs.Load(ms)
Run the project by pressing F5, or by selecting Debug -> Start Debugging.
If the steps were followed correctly, the console appears and the application detects the MRTD text and displays the information to the console.
This tutorial showed how to load an image and run passport recognition using the MRTDReader
class.