Skip to main content

Ppllocr

Logo

License PyPI Python

高性能 · 抗干扰 · 纯 ONNX 验证码识别引擎

Designed by liveless & zjyjoe & colin1112a as C2027


简介

Ppllocr 是一个基于 YOLOv11ONNX Runtime 构建的轻量级 OCR 库。

它专为解决高难度验证码。Ppllocr 在训练阶段引入了“地狱级”对抗样本生成技术(包括弹性形变、混沌轨迹干扰线、靶向切割、伪装笔画等),使其在面对严重扭曲和重度干扰的图像时,依然保持极高的识别精度。

目标上对标 Ddddocr,部分情况性能与准确度碾压 Ddddocr反之亦然)。

尽管如此,具体效果看玄学。

2.2 beta 更新和强化了对四则运算(即字符集+-())、马赛克特效、随机撒点等干扰的支持。

目前可以识别字符集

['#', '%', '*', '+', '-', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '=', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

核心特性

  • 极致轻量:移除 PyTorch 笨重依赖,核心仅依赖 onnxruntimenumpy,速度毫秒级。
  • 抗干扰强:对抗网格遮罩、随机贝塞尔干扰线、鱼眼/波浪扭曲(尤其针对洛谷)。
  • 开箱即用:内置训练好的高性能 ONNX 模型,安装即用,无需额外下载权重。
  • Web 友好:原生支持 bytes 流输入,完美适配爬虫。

安装

通过 PyPI 直接安装(推荐):

pip install ppllocr

经测试 cpu 推理已经足够快(<120 ms),故不提供 gpu 推理。

快速开始

1. 基础识别 (classification)

最简单的用法,直接获取识别出的字符串。

from ppllocr import OCR

# 初始化 (自动加载内置模型)
ocr = OCR()

# 传入图片二进制数据 (bytes)
with open("captcha_sample.jpg", "rb") as f:
    img_bytes = f.read()

# 直接返回字符串,例如 "2a3B"
text = ocr.classification(img_bytes)

print(f"识别结果: {text}")

2. 获取详细信息 (classification_box)

如果你需要知道字符的位置(坐标)或置信度。

from ppllocr import OCR

ocr = OCR()

with open("captcha_sample.jpg", "rb") as f:
    img_bytes = f.read()

# 返回 文本 和 详细信息列表
text, details = ocr.classification_box(img_bytes)

print(f"识别结果: {text}")

# details 结构示例:
# [
#   {'char': '2', 'conf': 0.98, 'box': [10.5, 5.0, 30.2, 45.1]},
#   {'char': 'a', 'conf': 0.95, 'box': [35.0, 8.0, 55.0, 48.0]},
#   ...
# ]
for char_info in details:
    print(f"字符: {char_info['char']}, 置信度: {char_info['conf']:.2f}, 坐标: {char_info['box']}")

3. 网络图片与爬虫

在爬虫中,直接将 requests 获取的 content 传给 classification 即可。

import requests
from ppllocr import OCR

ocr = OCR()

# 模拟从网络获取图片
url = "https://www.luogu.com.cn/lg4/captcha"
img_bytes = requests.get(url).content

# 直接传入 bytes
text = ocr.classification(img_bytes)

print(f"验证码是: {text}")

4. 动态调参

Ppllocr 允许你在推理时动态调整阈值,以平衡召回率准确率

# conf: 置信度阈值 (默认 0.25)
# iou:  NMS 重叠阈值 (默认 0.45)
text = ocr.classification(open("hard_sample.jpg", "rb").read(), conf=0.6, iou=0.45)

贡献者

  • liveless: 这个人用暴力数据解决了一切问题。
  • zjyjoe: 感谢提供的 GPU 运行时。
  • colin1112a: 万恶之源。

许可证

本项目采用 MIT 许可证。详情请参阅 LICENSE 文件。

Release files for ppllocr 2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ppllocr 2.2
File Size Uploaded
ppllocr-2.2.tar.gz 66.7 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for ppllocr 2.2
File Interpreter ABI Platform
ppllocr-2.2-py3-none-any.whl Python 3 none any Details

Total release size:133.4 MB

Release files / ppllocr-2.2.tar.gz

Download URL ppllocr-2.2.tar.gz
Size 66.7 MB
Tags Source
SHA-256 checksum
How to use checksums
2103eae1472a58bf74846c7eb318de2d3a990d0a652f54aa917cf476e73fbf26
BLAKE2b-256 checksum
How to use checksums
048327d14e56651b4c569962c073d7830c8e4cdcf9e7d4e2e9e7556a30e19869
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / ppllocr-2.2-py3-none-any.whl

Download URL ppllocr-2.2-py3-none-any.whl
Size 66.7 MB
Tags Python 3
SHA-256 checksum
How to use checksums
44c157bdfaf34f1b0b38bedbfe64fbee2b2fbb8d23802a8c7544a92c700b15c8
BLAKE2b-256 checksum
How to use checksums
df19c2b1a1a9f1938c3d886e7782a8f469e4a47c2eea59f3bfe6dbfa7147aba2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

2.2 This release

2 release files

1.2

2 release files

1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page