初始化项目

This commit is contained in:
zk
2026-07-24 15:15:01 +08:00
commit 7b3eca28fb
7 changed files with 918 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
"""OCR 工具
基于 RapidOCR(ONNXRuntime 后端)的图片文字提取工具。
模块级持有单例引擎,导入时完成初始化,后续直接复用。
"""
from rapidocr import RapidOCR
# 模块级单例:导入时初始化一次,全局复用
# 初始化配置(RapidOCR 3.x 用 params 字典,key 为配置项点路径):
# - 关闭方向分类(Cls):公告图基本都是正的,省一道计算
# - 其余检测/识别/后端配置沿用 RapidOCR 默认值,避免和新版枚举参数冲突
_engine = RapidOCR(
params={
"Global.use_cls": False,
"Global.text_score": 0.5,
}
)
def ocr(image: bytes) -> str:
"""识别图片中的文字。
Args:
image: 图片的字节数据(爬虫下载下来的图片流,无需落盘)。
Returns:
识别出的文字,多段以换行拼接为一段文本;没有文字或识别失败时返回空字符串。
"""
result = _engine(image)
# .txts 是识别出的多段文字(元组),没识别到时可能为 None 或空
if not result or not result.txts:
return ""
return "\n".join(result.txts)