Detect and Extract ID Card Information - Python

This tutorial shows how to detect and extract ID card information from a given image in a Python application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to find and extract ID Card 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

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 ID Card Information - Python tutorial.

Create the Project and Add LEADTOOLS References

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:

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 ID Frame Reader and Processing 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.Ocr") 
from Leadtools.Ocr import * 
 
from System import * 
from System.IO import * 

Add a new method named read_id_card(image), load the sample image with RasterCodecs and pass the RasterImage class to the method.

For the purposes of this tutorial the following image is used: <INSTALL_DIR>\LEADTOOLS23\Resources\Images\License_SAMPLE.PNG

def main(): 
 
    Support.set_license(os.path.join(DemosTools.get_root(), "C:\LEADTOOLS23\Support\Common\License")) 
    codecs = RasterCodecs() 
    image = codecs.Load(r"C:\LEADTOOLS23\Resources\Images\License_sample.png") 
    read_id_card(image) 

Add the code below to the read_id_card() method to process the given image and output the ID values if detected.

def read_id_card(image): 
    ocr_engine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD) 
    buffer = Path.Combine(r"C:\LEADTOOLS23\Bin\common\OcrLEADRuntime", "LEAD.Binarize.bin") 
    ocr_engine.Startup(None, None, None, None) 
    id_frame_reader = IDFrameReader(File.ReadAllBytes(buffer), ocr_engine) 
    results = id_frame_reader.Process(image) 
    if (results.Ready): 
        for res in id_frame_reader.Results: 
            print(res.Key, ":", res.Value) 
    else: 
        print("No ID found in this image") 

Handling Streams

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

filename = r"C:\LEADTOOLS23\Resources\Images\License_SAMPLE.PNG" 
codecs = RasterCodecs() 
bytes = File.ReadAllBytes(filename) 
ms = MemoryStream(bytes) 
ms.Position = 0 
image = codecs.Load(ms) 
read_id_card(image) 

Note: This will replace the existing code under the SetLicense() call.

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 detects the ID information and displays it to the console.

Extracted license information displayed to the console.

Wrap-Up

This tutorial showed how to load an image and run license recognition using the IDFrameReader 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.