Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.12
1 change: 0 additions & 1 deletion MANIFEST.in

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ Here are the advanced configuration options during initialization:
- **cuda**: Use GPU for computation (requires appropriate hardware support).

```python
from capybara import Backend
from capybara.runtime import Backend

model = MRZScanner(backend=Backend.cuda) # Use CUDA backend
#
Expand Down
2 changes: 1 addition & 1 deletion README_tw.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ result = model(img, do_postprocess=True)
- **cuda**:使用 GPU 進行運算(需要適當的硬體支援)。

```python
from capybara import Backend
from capybara.runtime import Backend

model = MRZScanner(backend=Backend.cuda) # 使用 CUDA 後端
#
Expand Down
4 changes: 3 additions & 1 deletion mrzscanner/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from importlib.metadata import version

from .scanner import ErrorCodes, ModelType, MRZScanner, SpottingInference
from .utils import replace_digits, replace_letters, replace_sex

__version__ = '1.0.7'
__version__ = version("mrzscanner_docsaid")
64 changes: 36 additions & 28 deletions mrzscanner/det/infer.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,46 @@
from pathlib import Path
from typing import Tuple

import capybara as cb
import cv2
import numpy as np
from capybara.onnxengine import ONNXEngine
from capybara.runtime import Backend
from capybara.utils import download_from_google

DIR = cb.get_curdir(__file__)

__all__ = ['Inference']
__all__ = ["Inference"]


class Inference:

configs = {
'20250222': {
'model_path': 'mrz_detection_20250222_fp32.onnx',
'file_id': '1D--D7glsse51oIdcyerZoYs88iokOmqU',
'img_size_infer': (256, 256),
"20250222": {
"model_path": "mrz_detection_20250222_fp32.onnx",
"file_id": "1D--D7glsse51oIdcyerZoYs88iokOmqU",
"img_size_infer": (256, 256),
}
}

def __init__(
self,
gpu_id: int = 0,
backend: cb.Backend = cb.Backend.cpu,
model_cfg: str = '20250222',
**kwargs
backend: Backend = Backend.cpu,
model_cfg: str = "20250222",
**kwargs,
):
self.root = DIR / 'ckpt'
self.root = DIR / "ckpt"
self.model_cfg = model_cfg
self.cfg = cfg = self.configs[model_cfg]
self.image_size = cfg['img_size_infer']
model_path = self.root / cfg['model_path']
if not cb.Path(model_path).exists():
cb.download_from_google(
cfg['file_id'], model_path.name, str(DIR / 'ckpt'))
self.image_size = cfg["img_size_infer"]
model_path = self.root / cfg["model_path"]
if not Path(model_path).exists():
download_from_google(cfg["file_id"], model_path.name, str(DIR / "ckpt"))

self.model = cb.ONNXEngine(model_path, gpu_id, backend, **kwargs)
self.input_key = list(self.model.input_infos.keys())[0]
self.output_key = list(self.model.output_infos.keys())[0]
self.model = ONNXEngine(model_path, gpu_id, backend, **kwargs)
info = self.model.summary()
self.input_key = info["inputs"][0]["name"]
self.output_key = info["outputs"][0]["name"]

def preprocess(self, img: np.ndarray, normalize: bool = False):

Expand All @@ -46,20 +49,28 @@ def preprocess(self, img: np.ndarray, normalize: bool = False):
pad = (img.shape[1] - img.shape[0]) // 2
padding = (pad, pad, 0, 0)
img = cv2.copyMakeBorder(
img, *padding, cv2.BORDER_CONSTANT, value=(0, 0, 0))
img, *padding, cv2.BORDER_CONSTANT, value=(0, 0, 0)
)
else:
pad = (img.shape[0] - img.shape[1]) // 2
padding = (0, 0, pad, pad)
img = cv2.copyMakeBorder(
img, *padding, cv2.BORDER_CONSTANT, value=(0, 0, 0))
img, *padding, cv2.BORDER_CONSTANT, value=(0, 0, 0)
)

tensor = cb.imresize(img, size=self.image_size)
tensor = np.transpose(tensor, axes=(2, 0, 1)).astype('float32')
tensor = np.transpose(tensor, axes=(2, 0, 1)).astype("float32")
tensor = tensor[None] / 255.0 if normalize else tensor[None]

return {self.input_key: tensor}, (img.shape[0], img.shape[1]), (padding[2], padding[0])
return (
{self.input_key: tensor},
(img.shape[0], img.shape[1]),
(padding[2], padding[0]),
)

def postprocess(self, hmap: np.ndarray, img_size: Tuple[int, int], shift: Tuple[int, int]) -> np.ndarray:
def postprocess(
self, hmap: np.ndarray, img_size: Tuple[int, int], shift: Tuple[int, int]
) -> np.ndarray:
hmap = np.uint8(hmap * 255)
hmap = cb.imresize(hmap, size=img_size)
hmap = cb.imbinarize(hmap)
Expand All @@ -75,12 +86,9 @@ def postprocess(self, hmap: np.ndarray, img_size: Tuple[int, int], shift: Tuple[
return poly

def __call__(self, img: np.ndarray, normalize: bool = True) -> np.ndarray:
tensor, img_size, shift = self.preprocess(
img, normalize=normalize)
tensor, img_size, shift = self.preprocess(img, normalize=normalize)
x = self.model(**tensor)
polygon = self.postprocess(
hmap=x[self.output_key][0],
img_size=img_size,
shift=shift
hmap=x[self.output_key][0], img_size=img_size, shift=shift
)
return polygon
56 changes: 28 additions & 28 deletions mrzscanner/rec/infer.py
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
from pathlib import Path
from typing import Any, Callable, List, Tuple

import capybara as cb
import numpy as np
from capybara.onnxengine import ONNXEngine
from capybara.runtime import Backend
from capybara.utils import download_from_google

from ..utils import DecodeMode, TextDecoder

DIR = cb.get_curdir(__file__)

__all__ = ['Inference']
__all__ = ["Inference"]


class Inference:

configs = {
'20250221': {
'model_path': 'mrz_recognition_20250221_fp32.onnx',
'file_id': '16t-kYHoBnI72MWDMCQ0K8GEyb90Tx2Rp',
'img_size_infer': (64, 640),
"20250221": {
"model_path": "mrz_recognition_20250221_fp32.onnx",
"file_id": "16t-kYHoBnI72MWDMCQ0K8GEyb90Tx2Rp",
"img_size_infer": (64, 640),
},
}

def __init__(
self,
gpu_id: int = 0,
backend: cb.Backend = cb.Backend.cpu,
model_cfg: str = '20250221',
delimeter: str = '<SEP>',
**kwargs
backend: Backend = Backend.cpu,
model_cfg: str = "20250221",
delimeter: str = "<SEP>",
**kwargs,
):
self.root = DIR / 'ckpt'
self.root = DIR / "ckpt"
self.model_cfg = model_cfg
self.cfg = cfg = self.configs[model_cfg]
self.image_size = cfg['img_size_infer']
self.image_size = cfg["img_size_infer"]
self.delimeter = delimeter
model_path = self.root / cfg['model_path']
if not cb.Path(model_path).exists():
cb.download_from_google(
cfg['file_id'], model_path.name, str(DIR / 'ckpt'))
model_path = self.root / cfg["model_path"]
if not Path(model_path).exists():
download_from_google(cfg["file_id"], model_path.name, str(DIR / "ckpt"))

self.model = cb.ONNXEngine(model_path, gpu_id, backend, **kwargs)
self.input_key = list(self.model.input_infos.keys())[0]
self.output_key = list(self.model.output_infos.keys())[0]
self.model = ONNXEngine(model_path, gpu_id, backend, **kwargs)
info = self.model.summary()
self.input_key = info["inputs"][0]["name"]
self.output_key = info["outputs"][0]["name"]

keys = ["<PAD>", "<EOS>", delimeter] + \
list("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789<")
chars_dict = {
k: i
for i, k in enumerate(keys)
}
keys = ["<PAD>", "<EOS>", delimeter] + list(
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789<"
)
chars_dict = {k: i for i, k in enumerate(keys)}

self.text_dec = TextDecoder(
chars_dict=chars_dict,
decode_mode=DecodeMode.Normal
chars_dict=chars_dict, decode_mode=DecodeMode.Normal
)

def preprocess(self, img: np.ndarray, normalize: bool = False):
tensor = cb.imresize(img, size=tuple(self.image_size))
tensor = np.transpose(tensor, axes=(2, 0, 1)).astype('float32')
tensor = np.transpose(tensor, axes=(2, 0, 1)).astype("float32")
tensor = tensor[None] / 255.0 if normalize else tensor[None]
return {self.input_key: tensor}

Expand Down
Loading