Giter Club home page Giter Club logo

python-mrz-scanner-sdk's Introduction

Python Extension: MRZ Scanner SDK

The goal of this project is to provide a Python-C++ binding for Dynamsoft Label Recognizer, enabling developers to create MRZ scanner applications for both Windows and Linux platforms using Python.

License Key

Get a 30-day FREE trial license to activate the SDK.

Supported Python Edition

  • Python 3.x

Install Dependencies

pip install mrz opencv-python

Command-line Usage

$ scanmrz <file-name> -l <license-key>

# Show the image with OpenCV
$ scanmrz <file-name> -u 1 -l <license-key>

python mrz scanner

Quick Start

import mrzscanner
from mrz.checker.td1 import TD1CodeChecker
from mrz.checker.td2 import TD2CodeChecker
from mrz.checker.td3 import TD3CodeChecker
from mrz.checker.mrva import MRVACodeChecker
from mrz.checker.mrvb import MRVBCodeChecker

def check(lines):
    try:
        td1_check = TD1CodeChecker(lines)
        if bool(td1_check):
            return "TD1", td1_check.fields()
    except Exception as err:
        pass
    
    try:
        td2_check = TD2CodeChecker(lines)
        if bool(td2_check):
            return "TD2", td2_check.fields()
    except Exception as err:
        pass
    
    try:
        td3_check = TD3CodeChecker(lines)
        if bool(td3_check):
            return "TD3", td3_check.fields()
    except Exception as err:
        pass
    
    try:
        mrva_check = MRVACodeChecker(lines)
        if bool(mrva_check):
            return "MRVA", mrva_check.fields()
    except Exception as err:
        pass
    
    try:
        mrvb_check = MRVBCodeChecker(lines)
        if bool(mrvb_check):
            return "MRVB", mrvb_check.fields()
    except Exception as err:
        pass
    
    return 'No valid MRZ information found'

# set license
mrzscanner.initLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==")

# initialize mrz scanner
scanner = mrzscanner.createInstance()

# load MRZ model
scanner.loadModel(mrzscanner.load_settings())

print('')
# decodeFile()
s = ""
results = scanner.decodeFile("images/1.png")
for result in results:
    print(result.text)
    s += result.text + '\n'
print('')
print(check(s[:-1]))
print('')

Methods

  • mrzscanner.initLicense('YOUR-LICENSE-KEY') # set the license globally

    mrzscanner.initLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==")
  • mrzscanner.createInstance() # create a MRZ scanner instance

    scanner = mrzscanner.createInstance()
  • scanner.loadModel(<model configuration file>) # load MRZ model

    scanner.loadModel(mrzscanner.load_settings())
  • decodeFile(<image file>) # recognize MRZ from an image file

    results = scanner.decodeFile(<image-file>)
    for result in results:
        print(result.text)
  • decodeMat(<opencv mat data>) # recognize MRZ from OpenCV Mat

    import cv2
    image = cv2.imread(<image-file>)
    results = scanner.decodeMat(image)
    for result in results:
        print(result.text)
  • addAsyncListener(callback function) # start a native thread and register a Python function for receiving the MRZ recognition results

  • decodeMatAsync(<opencv mat data>) # recognize MRZ from OpenCV Mat asynchronously

    def callback(results):
        s = ""
        for result in results:
            print(result.text)
            s += result.text + '\n'
    
        print('')
        print(check(s[:-1]))
    
    import cv2
    image = cv2.imread(<image-file>)
    scanner.addAsyncListener(callback)
    for i in range (2):
        scanner.decodeMatAsync(image)
        sleep(1)

How to Build the Python MRZ Scanner Extension

  • Create a source distribution:

    python setup.py sdist
  • setuptools:

    python setup.py build
    python setup.py develop 
  • Build wheel:

    pip wheel . --verbose
    # Or
    python setup.py bdist_wheel

python-mrz-scanner-sdk's People

Contributors

yushulx avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

python-mrz-scanner-sdk's Issues

Is this package considered *official*?

3rd time's a charm but I was asked by our eng manage wether this package is considered official or not, I understand you are a Dynamsoft employee, any reason this SDK is not hosted under the dynamsoft org?

The primary worry here is supply chain and security considerations.

Thanks!

Add type defs?

We rolled our own but it might be good for customers to have them shipped.

from cv2 import Mat

def initLicense(lic: str) -> None: ...
def createInstance() -> DynamsoftMrzReader: ...
def load_settings() -> str: ...

class MrzResult:
    text: str
    confidence: int
    x1: int
    x2: int
    x3: int
    x4: int
    y1: int
    y2: int
    y3: int
    y4: int

class DynamsoftMrzReader:
    def loadModel(self, model: str) -> None: ...
    def decodeFile(self, path: str) -> list[MrzResult]: ...
    def decodeMat(self, mat: Mat) -> list[MrzResult]: ...

See https://peps.python.org/pep-0561/

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.