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
¶
OcrResultList ¶
Ocr ¶
方法:
| 名称 | 描述 |
|---|---|
ocr |
OCR 一个 cv2 的图像。注意识别结果中的全角字符会被转换为半角字符。 |
find |
识别图像中的文本,并寻找满足指定要求的文本。 |
find_all |
识别图像中的文本,并寻找多个满足指定要求的文本。 |
expect |
识别图像中的文本,并寻找满足指定要求的文本。如果未找到则抛出异常。 |
ocr ¶
ocr(img: MatLike, *, rect: Rect | None = None, pad: bool = True) -> OcrResultList
OCR 一个 cv2 的图像。注意识别结果中的全角字符会被转换为半角字符。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
Rect | None
|
如果指定,则只识别指定矩形区域。 |
None
|
|
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
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]
expect ¶
expect(img: MatLike, text: str | Pattern | StringMatchFunction, *, hint: HintBox | None = None, rect: Rect | None = None, pad: bool = 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
equals
cached
¶
equals(text: str, *, remove_space: bool = False, ignore_case: bool = True) -> TextComparator
bounding_box ¶
bounding_box(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]]