Skip to main content

This is a mockup library used for OCR of identity documents.

Passport

A Python library for extracting information from passports using OCR (Tesseract). It supports parsing MRZ (Machine Readable Zone) and extracting additional fields like "Place of Issue" and "Date of Issue" from the visual zone.

Features

  • Robust MRZ Parsing: Handles common OCR errors, corrects line lengths, and supports various MRZ formats (TD1, TD2, TD3).
  • Full Text Extraction: Extracts non-MRZ fields like Place of Issue and Date of Issue.
  • Data Formatting:
    • Dates are standardized to dd-MM-YYYY.
    • Country codes are converted to full country names (e.g., VNM -> Vietnam).
    • Names are converted to Title Case (e.g., NGUYEN VAN A -> Nguyen Van A).
  • Input Flexibility: Accepts image file paths or Base64 encoded strings.
  • Fallback Logic: If Date of Issue is missing, it can infer it from Expiry Date (Expiry - 10 years).

Prerequisites

You need to have Tesseract OCR installed on your system.

Linux (Ubuntu/Debian)

sudo apt-get update
sudo apt-get install tesseract-ocr libtesseract-dev

Linux (Rocky/RHEL/CentOS)

sudo dnf install epel-release
sudo dnf install tesseract

[!NOTE] If you encounter version conflicts with language packs, installing just tesseract is often sufficient as it usually includes English data. If you need other languages, ensure the version matches the installed tesseract version.

macOS

brew install tesseract

Windows

Download and install the installer from UB-Mannheim/tesseract.

Installation

  1. Clone this repository.
  2. Install Python dependencies:
pip install m-ocr-mockup

Usage

Basic Usage (File Path)

from m_identify_ocr import read_passport

# Path to your passport image
image_path = "path/to/passport.jpg"

result = read_passport(image_path)

print(result)

Advanced Usage (Base64)

You can pass a Base64 string directly (with or without the data:image/...;base64, header).

from m_identify_ocr import read_passport

# Your base64 string
base64_string = "data:image/jpeg;base64,/9j/4AAQSkZJRg..."

result = read_passport(base64_string)

print(result)

Output Format

The library returns a dictionary with the extracted fields:

{
    'fullname': '',       # Combined Surname + Name (Title Case)
    'surname': '',
    'name': '',
    'sex': 'M',                      # M or F
    'birth_date': '',      # dd-MM-YYYY
    'expiry_date': '',     # dd-MM-YYYY
    'date_of_issue': '',   # dd-MM-YYYY (Extracted or Calculated)
    'place_of_issue': '', # Extracted from visual zone
    'document_number': '',
    'nationality': '',        # Full country name
    'country': '',            # Issuing country
    'type': '',                   # Passport type (TD3 is standard)
    'valid': True,                   # True if MRZ checksums are valid
    'raw_mrz': [...]                 # List of MRZ lines (for debugging)
}

Notes

  • Image Quality: High-resolution, glare-free images work best.
  • Language Support: The library uses Tesseract's English model (eng) by default. It works well for most passports (including Vietnamese) as they are bilingual.

Metadata

Release files for m-identify-ocr 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for m-identify-ocr 1.0.1
File Size Uploaded Provenance Actions
m_identify_ocr-1.0.1.tar.gz 17.0 kB Without provenance Details for m_identify_ocr-1.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for m-identify-ocr 1.0.1
File Interpreter ABI Platform Actions
m_identify_ocr-1.0.1-py3-none-any.whl Python 3 none any Details for m_identify_ocr-1.0.1-py3-none-any.whl

Total release size: 32.7 kB

Release files / m_identify_ocr-1.0.1.tar.gz

Download URL m_identify_ocr-1.0.1.tar.gz
Size 17.0 kB
Tags Source
SHA-256 checksum
How to use checksums
f094f4fb5fc6305d329a3ef089f765afca4fe4496982aa010a961b2d79cb3c99
BLAKE2b-256 checksum
How to use checksums
4ad823c75b6ee4e9497f4591d1035f7d951ca73b43677b4cd10683f23cb02849
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.7

Release files / m_identify_ocr-1.0.1-py3-none-any.whl

Download URL m_identify_ocr-1.0.1-py3-none-any.whl
Size 15.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bfb2370d7bcb51d900b7493898e2429d65cf7ab53231765071c414b31857a653
BLAKE2b-256 checksum
How to use checksums
46f70a03a9f87a774d7659019f2a3947b908196db63221e85355d696567e25f7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.7

Security for m-identify-ocr 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page