跳转至

backend.ocr

ocr

类:

名称 描述
OcrResult
OcrResultList
Ocr

函数:

名称 描述
sanitize_text

对识别结果进行清理。此函数将被所有 OCR 引擎调用。

regex

返回正则表达式字符串匹配函数。

contains

返回包含指定文本的函数。

equals

返回等于指定文本的函数。

bounding_box

计算点集的外接矩形。

pad_to

将图像居中填充到指定大小。缺少部分使用指定颜色填充。

jp

日语 OCR 引擎。

en

英语 OCR 引擎。

属性:

名称 类型 描述
global_character_mapping dict[str, str]

全局字符映射表。某些字符可能在某些情况下被错误地识别,此时可以在这里添加映射。

global_character_mapping module-attribute

global_character_mapping: dict[str, str] = {'ó': '6', 'ą': 'a'}

全局字符映射表。某些字符可能在某些情况下被错误地识别,此时可以在这里添加映射。

OcrResult dataclass

方法:

名称 描述
replace

替换识别结果中的文本。

regex

提取识别结果中符合正则表达式的文本。

numbers

提取识别结果中的数字。

属性:

名称 类型 描述
original_rect Rect

识别结果在原图中的区域坐标。

original_rect instance-attribute

original_rect: Rect

识别结果在原图中的区域坐标。

如果识别时没有设置 recthint 参数,则此属性值与 rect 相同。

replace

replace(old: str, new: str, count: int = -1) -> Self

替换识别结果中的文本。

regex

regex(pattern: Pattern | str) -> list[str]

提取识别结果中符合正则表达式的文本。

numbers

numbers() -> list[int]

提取识别结果中的数字。

OcrResultList

Bases: list[OcrResult]

方法:

名称 描述
squash

将所有识别结果合并为一个大结果。

first

返回第一个识别结果。

where

返回符合条件的识别结果。

squash

squash(remove_newlines: bool = True) -> OcrResult

将所有识别结果合并为一个大结果。

first

first() -> OcrResult | None

返回第一个识别结果。

where

where(pattern: StringMatchFunction) -> OcrResultList

返回符合条件的识别结果。

Ocr

方法:

名称 描述
ocr

OCR 一个 cv2 的图像。注意识别结果中的全角字符会被转换为半角字符

find

识别图像中的文本,并寻找满足指定要求的文本。

find_all

识别图像中的文本,并寻找多个满足指定要求的文本。

expect

识别图像中的文本,并寻找满足指定要求的文本。如果未找到则抛出异常。

ocr

ocr(img: MatLike, *, rect: Rect | None = None, pad: bool = True) -> OcrResultList

OCR 一个 cv2 的图像。注意识别结果中的全角字符会被转换为半角字符

参数:

名称 类型 描述 默认
rect
Rect | None

如果指定,则只识别指定矩形区域。

None
pad
bool

是否将过小的图像(尺寸 < 631x631)的图像填充到 631x631。 默认为 True。

对于 PaddleOCR 模型,图片尺寸太小会降低准确率。 将图片周围填充放大,有助于提高准确率,降低耗时。

True

返回:

类型 描述
OcrResultList

所有识别结果

find

find(img: MatLike, text: str | Pattern | StringMatchFunction, *, hint: HintBox | None = None, rect: Rect | None = None, pad: bool = True) -> OcrResult | None

识别图像中的文本,并寻找满足指定要求的文本。

参数:

名称 类型 描述 默认
hint
HintBox | None

如果指定,则首先只识别 HintBox 范围内的文本,若未命中,再全局寻找。

None
rect
Rect | None

如果指定,则只识别指定矩形区域。此参数优先级低于 hint

None
pad
bool

ocrpad 参数。

True

返回:

类型 描述
OcrResult | None

找到的文本,如果未找到则返回 None

find_all

find_all(img: MatLike, texts: list[str | Pattern | StringMatchFunction], *, hint: HintBox | None = None, rect: Rect | None = None, pad: bool = True) -> list[OcrResult | None]

识别图像中的文本,并寻找多个满足指定要求的文本。

返回:

类型 描述
list[OcrResult | None]

所有找到的文本,结果顺序与输入顺序相同。 若某个文本未找到,则该位置为 None。

expect

expect(img: MatLike, text: str | Pattern | StringMatchFunction, *, hint: HintBox | None = None, rect: Rect | None = None, pad: bool = True) -> OcrResult

识别图像中的文本,并寻找满足指定要求的文本。如果未找到则抛出异常。

参数:

名称 类型 描述 默认
hint
HintBox | None

如果指定,则首先只识别 HintBox 范围内的文本,若未命中,再全局寻找。

None
rect
Rect | None

如果指定,则只识别指定矩形区域。此参数优先级高于 hint

None
pad
bool

ocrpad 参数。

True

返回:

类型 描述
OcrResult

找到的文本

sanitize_text

sanitize_text(text: str) -> str

对识别结果进行清理。此函数将被所有 OCR 引擎调用。

默认行为为先将文本 Unicode 规范化_,然后使用 global_character_mapping 中的映射数据进行清理。 可以重写此函数以实现自定义的清理逻辑。

.. note:: Unicode 规范化最常见的一个行为是将全角字符转换为半角字符。

.. _Unicode 规范化: https://docs.python.org/zh-cn/3.14/library/unicodedata.html#unicodedata.normalize

regex cached

regex(regex: str) -> TextComparator

返回正则表达式字符串匹配函数。

contains cached

contains(text: str, *, ignore_case: bool = False) -> TextComparator

返回包含指定文本的函数。

equals cached

equals(text: str, *, remove_space: bool = False, ignore_case: bool = True) -> TextComparator

返回等于指定文本的函数。

参数:

名称 类型 描述 默认

text

str

要比较的文本。

必需

remove_space

bool

是否忽略空格。默认为 False。

False

ignore_case

bool

是否忽略大小写。默认为 True。

True

bounding_box

bounding_box(points: list[tuple[int, int]]) -> tuple[int, int, int, int]

计算点集的外接矩形。

参数:

名称 类型 描述 默认

points

list[tuple[int, int]]

点集。以左上角为原点,向下向右为正方向。

必需

返回:

类型 描述
tuple[int, int, int, int]

外接矩形的左上角坐标和宽高

pad_to

pad_to(img: MatLike, target_size: int, rgb: tuple[int, int, int] = (255, 255, 255)) -> tuple[MatLike, tuple[int, int]]

将图像居中填充到指定大小。缺少部分使用指定颜色填充。

返回:

类型 描述
tuple[MatLike, tuple[int, int]]

填充后的图像和填充的偏移量 (x, y)。

jp

jp() -> Ocr

日语 OCR 引擎。

en

en() -> Ocr

英语 OCR 引擎。