coreocr Python SDK
coreocr 提供 OCR 文字识别和 YOLO 目标检测。4.3 的 wheel 包含 CPU 运行库和两个平台的许可证。首次初始化按需从 ModelScope 下载所选模型,缓存后可离线推理。
安装
使用 64 位 Python 3.9 或以上版本。将对应平台的 wheel 下载到本地,在 wheel 所在目录打开终端。项目构建生成的 wheel 位于 out/coreocr/dist/。
Windows x64,在 PowerShell 中运行:
python -m pip install --no-index .\coreocr-4.3-py3-none-win_amd64.whl
python -c "from importlib.metadata import version; from coreocr import CoreOCROnnx; print(version('coreocr'))"
Linux x86_64,需要 glibc 2.39 或更高版本,已在 Ubuntu 24.04 验证。在终端中运行:
python3 -m pip install --no-index ./coreocr-4.3-py3-none-manylinux_2_39_x86_64.whl
python3 -c "from importlib.metadata import version; from coreocr import CoreOCROnnx; print(version('coreocr'))"
第二条命令输出 4.3 表示当前解释器已能导入 SDK。如果使用虚拟环境,请先激活该环境,再执行对应命令。
使用
下面的 OCR 和 YOLO 示例均可独立运行。准备一张图片,命名为 input.jpg,放在运行命令的当前目录。仓库自带的测试图片是 images/text.jpg,也可以直接把代码中的输入路径改为自己的图片路径。
OCR 识别文字
将以下代码保存为 ocr_demo.py,在 Windows 运行 python ocr_demo.py,在 Linux 运行 python3 ocr_demo.py。程序会打印识别到的文字,并在当前目录保存带框的 ocr_result.jpg。
from coreocr import CoreOCROnnx
CoreOCROnnx.Init() # 首次获取 PP-OCRv6 tiny 检测、方向分类、识别模型和配套字典
try:
result = CoreOCROnnx.Detect("input.jpg")
print(result["strRes"])
result.vis("ocr_result.jpg")
finally:
CoreOCROnnx.FreeEngine()
读取整页文字用 result["strRes"],逐行处理用 result["textBlocks"]。批量处理时,初始化一次,在 try 内多次调用 Detect,处理完再释放。
YOLO 检测目标
将以下代码保存为 yolo_demo.py,在 Windows 运行 python yolo_demo.py,在 Linux 运行 python3 yolo_demo.py。程序会打印检测数量和每个目标的类别、置信度,并保存标注图。
from coreocr import CoreOCROnnx
CoreOCROnnx.YoloInitJson() # 首次只下载 yolov8s.onnx
try:
result = CoreOCROnnx.YoloDetect("input.jpg")
print("检测数量", result["count"])
for item in result["detections"]:
print(item.get("class_name", item["class_id"]), item["confidence"])
result.vis("yolo_result.jpg")
finally:
CoreOCROnnx.YoloFreeEngine()
切换模型和调整参数
把 OCR 示例中的 CoreOCROnnx.Init() 替换成下面的代码,就可以按需下载并使用 small 模型。将 size 改为 "medium" 即可使用 medium 模型。检测模型、识别模型和字典使用同一规格,方向模型可复用。
size = "small"
CoreOCROnnx.Init({
"det_infer": f"PP-OCRv6_{size}_det.onnx",
"rec_infer": f"PP-OCRv6_{size}_rec.onnx",
"keyFile": f"ppocrv6{size}_dict.txt",
"cpu_threads": 4,
})
只需调整个别参数时,可用 CoreOCROnnx.Init({"cpu_threads": 4}) 或 CoreOCROnnx.YoloInitJson(parameterJson={"confidence_threshold": 0.4})。省略的字段使用默认值。两个引擎的 visualize 默认为 True,可用 result.vis(path) 保存为 JPEG。显式设置 visualize=False 时,请同时去掉示例中的 vis() 调用。
配置也可使用包顶层导出的 OCRConfig 和 YOLOConfig 对象。模型文件直接保存在 Windows 的 %LOCALAPPDATA%\coreocr\models\ 或 Linux 的 ~/.cache/coreocr/models/,Linux 支持 XDG_CACHE_HOME。SDK 在使用前核验模型大小和 SHA-256,缓存齐全后可断网重启。旧版缓存中的模型和字典可以复制到此目录复用。
离线机器可以复制上述缓存,也可自行准备模型目录,再调用 CoreOCROnnx.Init({"models_root": "./models"}) 或 CoreOCROnnx.InitDefaultOCREngine("./models")。YOLO 可用 CoreOCROnnx.YoloInitJson("./models/yolov8s.onnx")。显式目录中的文件缺失时会报告该路径。如果仅指定一个绝对模型路径,其余默认组件仍按需下载。
图片数据已在内存中
在上面示例的 try 块内,可按实际输入选择以下方法。字节入口接受 JPEG、PNG 等编码图片的字节,Base64 入口接受这些字节编码后的字符串。
| 输入 | OCR | YOLO |
|---|---|---|
| 文件路径 | Detect("input.jpg") |
YoloDetect("input.jpg") |
| 图片字节 | Detect(image_bytes) |
YoloDetect(image_bytes) |
| Base64 字符串 | DetectBase64(image_base64) |
YoloDetectBase64(image_base64) |
例如在已初始化的 OCR 引擎中读取图片字节:
from pathlib import Path
image_bytes = Path("input.jpg").read_bytes()
result = CoreOCROnnx.Detect(image_bytes)
print(result["strRes"])
授权
默认调用:Windows CPU 可直接运行上面的示例。Linux 第一次调用 Init() 或 YoloInitJson() 时,SDK 会先检查当前进程的授权状态,需要时自动激活包内的 paddleocr-linux.lic。两种引擎共用当前进程的授权,任何一方成功激活后,后续初始化都会复用该状态。
手动激活:如果希望在应用启动时确认授权,将下面代码放在同一进程的引擎初始化之前。ActivateLicense() 会选择当前平台的内置文件,Windows 使用 paddleocr.lic,Linux 使用 paddleocr-linux.lic。如果已有自己的许可证,请把这行替换为 CoreOCROnnx.ActivateLicense("你的许可证文件路径")。
from coreocr import CoreOCROnnx, SDKError
try:
CoreOCROnnx.ActivateLicense()
except SDKError as error:
raise SystemExit(f"许可证激活失败:{error}") from error
status = CoreOCROnnx.GetLicenseStatusInfo()
print("已激活", status["activated"])
# 在这里调用 CoreOCROnnx.Init() 或 CoreOCROnnx.YoloInitJson()。
申请许可证:在需要授权的目标机器上执行以下代码,将生成的 license_request_code.txt 提供给许可证签发方。收到许可证后,使用上面的自定义路径激活。GetLicenseStatusInfo() 返回字典。如需原始 JSON 字符串,可调用 GetLicenseStatus()。
from pathlib import Path
from coreocr import CoreOCROnnx
request_code = CoreOCROnnx.GetLicenseRequestCode()
Path("license_request_code.txt").write_text(request_code, encoding="utf-8")
从源码打包
在仓库根目录完成本平台的 Release CPU 构建,然后运行 python python/build_wheel.py。构建环境需要 wheel,Linux 还需 auditwheel 和 patchelf。产物在 out/coreocr/dist/。
Metadata
Release files for coreocr 4.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distributions (wheels)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| coreocr-4.3-py3-none-win_amd64.whl | Python 3 | none | Windows x86-64 | Details for coreocr-4.3-py3-none-win_amd64.whl |
| coreocr-4.3-py3-none-manylinux_2_39_x86_64.whl | Python 3 | none | Linux glibc 2.39+ x86-64 | Details for coreocr-4.3-py3-none-manylinux_2_39_x86_64.whl |
Total release size: 86.9 MB
Release files / coreocr-4.3-py3-none-win_amd64.whl
| Download URL | coreocr-4.3-py3-none-win_amd64.whl |
|---|---|
| Size | 9.4 MB |
| Tags | Python 3 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
8907fee666ee8b58989db08ab6b63f1498dd7cddede1fa0b69a29b0b335ee105
|
|
BLAKE2b-256 checksum How to use checksums |
a830e68cfb30b0a907ba89f7dd1e5a4786f5ee62a8e4bdf74cc233abc6798603
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.10
|
Release files / coreocr-4.3-py3-none-manylinux_2_39_x86_64.whl
| Download URL | coreocr-4.3-py3-none-manylinux_2_39_x86_64.whl |
|---|---|
| Size | 77.5 MB |
| Tags | Linux glibc 2.39+ x86-64 Python 3 |
|
SHA-256 checksum How to use checksums |
1a90106a3cb50c5d4c5c960d9582a49c48a8fabd82743aa45c02cf4abec879ba
|
|
BLAKE2b-256 checksum How to use checksums |
2145655791f67efd2f85b4819fd958d0a7461cdb8eb621f0fe37bdb3e50d3ac4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.10
|
Security for coreocr 4.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.