### Build MRZScanner Wheel Package Source: https://github.com/docsaidlab/mrzscanner/blob/main/README.md After cloning the repository, build the wheel package for installation. This involves installing the wheel package and then running the build command. ```bash pip install wheel cd MRZScanner python setup.py bdist_wheel ``` -------------------------------- ### Install MRZScanner from Wheel File Source: https://github.com/docsaidlab/mrzscanner/blob/main/README.md Install the MRZScanner library from the generated wheel file. Replace the asterisk with the actual version number. ```bash pip install dist/mrzscanner_docsaid-*-py3-none-any.whl ``` -------------------------------- ### Install MRZScanner via PyPI Source: https://github.com/docsaidlab/mrzscanner/blob/main/README.md Install the mrzscanner-docsaid package using pip. This is the recommended method for most users. ```bash pip install mrzscanner-docsaid ``` -------------------------------- ### Configure MRZFinetuneDataset for Fine-tuning Source: https://github.com/docsaidlab/mrzscanner/blob/main/dataset/README.md Configure the MRZFinetuneDataset for the fine-tuning stage. This setup is similar to pre-training but omits the dataset length parameter. ```python ds = MRZFinetuneDataset( root='/data/Dataset', image_size=(512, 512), aug_ratio=1.0, return_tensor=True, ) ``` -------------------------------- ### Verify MRZScanner Installation Source: https://github.com/docsaidlab/mrzscanner/blob/main/README.md Check if the mrzscanner library is installed correctly by printing its version number. A successful output indicates the installation was successful. ```python import mrzscanner; print(mrzscanner.__version__) ``` -------------------------------- ### Initialize MRZScanner with CUDA Backend Source: https://github.com/docsaidlab/mrzscanner/blob/main/README.md Use this to initialize the MRZScanner with the CUDA backend for GPU computation. Ensure CUDA is installed and supported. ```python from capybara import Backend model = MRZScanner(backend=Backend.cuda) # Use CUDA backend ``` -------------------------------- ### MRZScanner Spotting Model Inference Example Source: https://github.com/docsaidlab/mrzscanner/blob/main/README.md Example of using the 'spotting' model type for end-to-end MRZ detection and recognition. This model does not return MRZ coordinates. Ensure necessary libraries like cv2 and skimage are installed. ```python import cv2 from skimage import io from mrzscanner import MRZScanner, ModelType # Create model model = MRZScanner( model_type=ModelType.spotting, spotting_cfg='20240919' ) # Read image from the web img = io.imread('https://github.com/DocsaidLab/MRZScanner/blob/main/docs/test_mrz.jpg?raw=true') img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) # Model inference result = model(img, do_center_crop=True, do_postprocess=False) # Output result print(result) # { # 'mrz_polygon': None, # 'mrz_texts': [ # 'PCAZEQAOARIN< # } ``` -------------------------------- ### Process Ground Truth String Source: https://github.com/docsaidlab/mrzscanner/blob/main/dataset/README.md The ground truth `gt` is a string, typically separated by '&'. Split this string to get individual MRZ codes, useful for text recognition tasks. ```python print('GT original:', gt) # >>> GT original: 9SZ563DNUUM1NEAKVHUI09MK4H9SAW&56QI04IS244AF4F0809416QQQ9MDOA&DS0CJGRAALNCT1NKPYGH5DPLAOY72L print('GT:', gt.split('&')) # >>> GT: ['9SZ563DNUUM1NEAKVHUI09MK4H9SAW', # '56QI04IS244AF4F0809416QQQ9MDOA', # 'DS0CJGRAALNCT1NKPYGH5DPLAOY72L'] ``` -------------------------------- ### Initialize MRZ Finetune Dataset Source: https://github.com/docsaidlab/mrzscanner/blob/main/dataset/README.md Instantiate the `MRZFinetuneDataset` for fine-tuning. Ensure the `root` path points to your downloaded MIDV-2020 dataset. Setting `return_tensor=True` converts the output to PyTorch Tensors. ```python from finetune_dataset import MRZFinetuneDataset ds = MRZFinetuneDataset( root='/data/Dataset', # Change to your dataset path image_size=(512, 512), aug_ratio=1.0, return_tensor=True, # Converts the output to a PyTorch Tensor ) ``` -------------------------------- ### Initialize MRZScanner with CPU Backend Source: https://github.com/docsaidlab/mrzscanner/blob/main/README.md Use this to initialize the MRZScanner with the CPU backend for computation. ```python from capybara import Backend model = MRZScanner(backend=Backend.cpu) # Use CPU backend ``` -------------------------------- ### Initialize MRZScanner with Spotting Model Configuration Source: https://github.com/docsaidlab/mrzscanner/blob/main/README.md Initialize the MRZScanner using the 'spotting' model type and specifying the '20240919' configuration for spotting. ```python model = MRZScanner( model_type=ModelType.spotting, spotting_cfg='20240919' ) ``` -------------------------------- ### Configure MRZDataset for Pre-training Source: https://github.com/docsaidlab/mrzscanner/blob/main/dataset/README.md Use this configuration to set up the MRZDataset for the initial pre-training phase. It specifies image size, augmentation ratio, tensor return, and dataset length. ```python ds = MRZDataset( root='/data/Dataset', image_size=(512, 512), aug_ratio=1.0, return_tensor=True, length_of_dataset=640000, ) ``` -------------------------------- ### Access Fine-tuning Dataset Sample Source: https://github.com/docsaidlab/mrzscanner/blob/main/dataset/README.md Retrieve a single data sample (image and ground truth label) from the fine-tuning dataset. The dataset returns only the image and its corresponding MRZ text. ```python img, gt = ds[1] ``` -------------------------------- ### Initialize MRZScanner with Recognition Model Configuration Source: https://github.com/docsaidlab/mrzscanner/blob/main/README.md Initialize the MRZScanner to load only the MRZ recognition model, using the '20250221' configuration. ```python model = MRZScanner( model_type=ModelType.recognition, recognition_cfg='20250221' ) ``` -------------------------------- ### Initialize MRZScanner with Detection Model Configuration Source: https://github.com/docsaidlab/mrzscanner/blob/main/README.md Initialize the MRZScanner to load only the MRZ detection model, using the '20250222' configuration. ```python model = MRZScanner( model_type=ModelType.detection, detection_cfg='20250222' ) ``` -------------------------------- ### Initialize MRZDataset Source: https://github.com/docsaidlab/mrzscanner/blob/main/dataset/README.md Initializes the MRZDataset for generating synthetic MRZ images. Ensure external datasets are downloaded and paths are correctly set. The `return_tensor` option converts output to PyTorch Tensors. ```python import cv2 from train_dataset import MRZDataset ds = MRZDataset( root='/data/Dataset', # Change to your dataset path image_size=(512, 512), aug_ratio=1.0, return_tensor=True, # Converts the output to a PyTorch Tensor length_of_dataset=1000000, # Specify the size of the dataset ) ``` -------------------------------- ### Initialize MRZScanner with Two-Stage Model Configuration Source: https://github.com/docsaidlab/mrzscanner/blob/main/README.md Initialize the MRZScanner for a two-stage process, specifying configurations for both detection ('20250222') and recognition ('20250221'). ```python model = MRZScanner( model_type=ModelType.two_stage, detection_cfg='20250222', recognition_cfg='20250221' ) ``` -------------------------------- ### Clone MRZScanner Repository Source: https://github.com/docsaidlab/mrzscanner/blob/main/README.md Download the MRZScanner project from GitHub using git clone. This is useful for development or if you need the latest unreleased code. ```bash git clone https://github.com/DocsaidLab/MRZScanner.git ``` -------------------------------- ### Initialize MRZScanner with Spotting Model Type Source: https://github.com/docsaidlab/mrzscanner/blob/main/README.md Initialize the MRZScanner to use the 'spotting' model type, which is an end-to-end model for detection and recognition. ```python from mrzscanner import MRZScanner model = MRZScanner(model_type=MRZScanner.spotting) ``` -------------------------------- ### List Available MRZScanner Models Source: https://github.com/docsaidlab/mrzscanner/blob/main/README.md Call this method to view all available model versions for 'spotting', 'detection', and 'recognition'. ```python from mrzscanner import MRZScanner print(MRZScanner().list_models()) # { # 'spotting': ['20240919'], # 'detection': ['20250222'], # 'recognition': ['20250221'] # } ``` -------------------------------- ### Prepare Cropped MRZ Image for Recognition Source: https://github.com/docsaidlab/mrzscanner/blob/main/README.md This snippet demonstrates how to prepare a cropped MRZ image using detected polygon coordinates. It utilizes `imwarp_quadrangle` for image transformation. ```python import numpy as np from skimage import io from capybara import imwarp_quadrangle, imwrite polygon = np.array([ [ 158.536 , 1916.3734], [1682.7792, 1976.1683], [1677.1018, 2120.8926], [ 152.8586, 2061.0977] ], dtype=np.float32) img = io.imread('https://github.com/DocsaidLab/MRZScanner/blob/main/docs/test_mrz.jpg?raw=true') img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) mrz_img = imwarp_quadrangle(img, polygon) imwrite(mrz_img) ``` -------------------------------- ### MRZ Recognition Only Source: https://github.com/docsaidlab/mrzscanner/blob/main/README.md Use the recognition model to perform MRZ text extraction on a pre-cropped MRZ image. Set `do_center_crop=False` as cropping is already done. Requires only the recognition configuration. ```python from mrzscanner import MRZScanner, ModelType # Create model model = MRZScanner( model_type=ModelType.recognition, recognition_cfg='20250221' ) # Input the cropped MRZ image result = model(mrz_img, do_center_crop=False) # Output result print(result) # { # 'mrz_polygon': None, # 'mrz_texts': [ # 'PCAZEQAQARIN< # } ``` -------------------------------- ### Print and Split Ground Truth String Source: https://github.com/docsaidlab/mrzscanner/blob/main/dataset/README.md Display the raw ground truth string and then split it by the '&' delimiter to separate different parts of the MRZ data. This is useful for inspecting the structure of the labels. ```python print('GT:', gt) # >>> GT: PCAZEABDULLAYEV<>> GT: ['PCAZEABDULLAYEV< # } # Draw MRZ location from capybara import draw_polygon, imwrite, centercrop poly_img = draw_polygon(img, result['mrz_polygon'], color=(0, 0, 255), thickness=5) imwrite(centercrop(poly_img)) ``` -------------------------------- ### Plot Character Center Points Source: https://github.com/docsaidlab/mrzscanner/blob/main/dataset/README.md Draws circles on the image at the center of each character within the MRZ, based on `fixed_points`. This is useful for precise character point localization tasks. ```python import capybara as cb point_img = img.copy() for p in fixed_points: if p[0] == 0 and p[1] == 0: break cv2.circle(point_img, (int(p[0]), int(p[1])), 2, (0, 0, 255), -1) cb.imwrite(point_img, 'points.jpg') ``` -------------------------------- ### MRZScanner Citation Source: https://github.com/docsaidlab/mrzscanner/blob/main/README.md BibTeX entry for citing the MRZScanner project. Use this in academic papers and projects. ```bibtex @misc{yuan2024mrzscanner, author = {Ze Yuan}, title = {MRZScanner}, year = {2024}, publisher = {GitHub}, url = {https://github.com/DocsaidLab/MRZScanner}, note = {GitHub repository} } ``` -------------------------------- ### MRZ Detection Only Source: https://github.com/docsaidlab/mrzscanner/blob/main/README.md Use the detection model to only find the MRZ location without performing recognition. This model requires only the detection configuration. ```python import cv2 from skimage import io from mrzscanner import MRZScanner, ModelType # Create model model = MRZScanner( model_type=ModelType.detection, detection_cfg='20250222', ) # Read image from the web img = io.imread('https://github.com/DocsaidLab/MRZScanner/blob/main/docs/test_mrz.jpg?raw=true') img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) # Model inference result = model(img, do_center_crop=True) # Output result print(result) # { # 'mrz_polygon': # array( # [ # [ 158.536 , 1916.3734], # [1682.7792, 1976.1683], # [1677.1018, 2120.8926], # [ 152.8586, 2061.0977] # ], # dtype=float32 # ), # 'mrz_texts': None, # 'msg': # } ``` -------------------------------- ### Draw MRZ Polygon Source: https://github.com/docsaidlab/mrzscanner/blob/main/dataset/README.md Visualizes the four points defining the MRZ area using `capybara.draw_polygon`. This is useful for region localization models. ```python import capybara as cb poly_img = cb.draw_polygon(img.copy(), poly, color=(0, 255, 0)) cb.imwrite(poly_img, 'poly.jpg') ``` -------------------------------- ### MRZ Post-processing Function Source: https://github.com/docsaidlab/mrzscanner/blob/main/README.md A helper function to replace digits with visually similar characters that are valid in MRZ fields. This is part of the post-processing step to correct potential recognition errors. ```python import re def replace_digits(text: str): text = re.sub('0', 'O', text) text = re.sub('1', 'I', text) text = re.sub('2', 'Z', text) text = re.sub('4', 'A', text) text = re.sub('5', 'S', text) text = re.sub('8', 'B', text) return text if doc_type == 3: # TD1 if len(results[0]) != 30 or len(results[1]) != 30 or len(results[2]) != 30: return [''], ErrorCodes.POSTPROCESS_FAILED_TD1_LENGTH # Line1 doc = results[0][0:2] country = replace_digits(results[0][2:5]) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.