Extract Business Card Information - Python

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

Overview  
Summary This tutorial covers how to perform Business Card recognition in a Python Console application using the LEADTOOLS Business Card Recognition SDK.
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 Extract Business 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. References can be added via NuGet packages.

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 Business Card 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.Ocr") 
from Leadtools.Ocr import * 
LibraryLoader.add_reference("Leadtools.Barcode") 
from Leadtools.Barcode import * 
LibraryLoader.add_reference("Leadtools.Forms.Commands") 
from Leadtools.Forms.Commands import * 
 
from System.IO import * 

Add a new method to the Project-Name.py file named extract_business_card(image). Call this method after the call to the load_image() method inside the main() method, as shown below.

def main(): 
 
    Support.set_license(os.path.join(DemosTools.get_root(), "C:/LEADTOOLS23/Support/Common/License")) 
 
    image = load_image(r"C:\LEADTOOLS23\Resources\Images\business_card_sample.jpg") 
    extract_business_card(image) 

Add the following code to the load_image method. Use the commented code when using Memory Stream.

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) 
 
    return codecs.Load(filename) 

Add the below code to the extract_business_card() method to read the business card information from the loaded RasterImage and display it to the console.

def extract_business_card(image): 
 
    ocr_engine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD) 
    barcode_engine = BarcodeEngine() 
    ocr_engine.Startup(None, None, None, r"C:\LEADTOOLS23\Bin\Common\OcrLEADRuntime") 
 
    bc_reader = BusinessCardReader(ocr_engine, barcode_engine) 
 
    status = bc_reader.Process(image) 
 
    if (status == BCProcessStatus.BlurDetected): 
        print("Blur detected in image.") 
    elif (status == BCProcessStatus.GlareDetected): 
        print("Glare detected in image.") 
    elif (status == BCProcessStatus.Failed): 
        print("Failed to recognize image.") 
    elif (status == BCProcessStatus.Success): 
        if (bc_reader.Results != None): 
            for res in bc_reader.Results: 
                bounds = res.Value.Bounds 
                print(f"Field Name: {res.Key}") 
                print(f"Field Value: {res.Value.Value}") 
                print(f"Field Confidence: {res.Value.Confidence}") 
                print(f"Field Bounds: {bounds.X}, {bounds.Y}, {bounds.Width}, {bounds.Height}") 
                print("************************************") 

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 displays the fields from the business card inside the loaded RasterImage. This tutorial uses the sample from the following file path: C:\LEADTOOLS23\Resources\Images\business_card_sample.jpg

Extracted card information displayed to the console.

Wrap-up

This tutorial showed how to extract business card information from an image using the BusinessCardReader 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.