Skip to main content

High-performance Robust OCR powered by Liveless

Project description

Ppllocr

Logo

License PyPI Python

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

Designed by Liveless & Zjyjoe & Colin1112 as C2027


简介

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

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

目标上对标 Ddddocr,部分情况性能与准确度碾压 Ddddocr

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

目前可以识别字符集

  • 数字:0123456789
  • 大写字母:ABCDEFGHIJKLMNOPQRSTUVWXYZ
  • 小写字母:abcdefghijklmnopqrstuvwxyz
  • 部分特殊字符*/%@#

核心特性

  • 极致轻量:移除 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](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.25, iou=0.45)

贡献者

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

许可证

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ppllocr-1.2.tar.gz (67.7 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ppllocr-1.2-py3-none-any.whl (67.7 MB view details)

Uploaded Python 3

File details

Details for the file ppllocr-1.2.tar.gz.

File metadata

  • Download URL: ppllocr-1.2.tar.gz
  • Upload date:
  • Size: 67.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ppllocr-1.2.tar.gz
Algorithm Hash digest
SHA256 f3541d95344be422609f504bcd678cd160352e5f96bcc50186f8c9d113d84643
MD5 c2fcfa49b6d9356aa5ea2c4f2a707e8f
BLAKE2b-256 0e80271c96a9e786da0b135447ea177061e1410b0b5f609da4dbba49e9a35039

See more details on using hashes here.

File details

Details for the file ppllocr-1.2-py3-none-any.whl.

File metadata

  • Download URL: ppllocr-1.2-py3-none-any.whl
  • Upload date:
  • Size: 67.7 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ppllocr-1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fa271f2a03a166521be9767ab56442914ea6cb59bc4a3f9c37d893188311c3bf
MD5 e4863e7b43809931bd67df080471d6dc
BLAKE2b-256 55c85147d716adf95a08afad02347959ee9821bf7925d5d629f225dca9792e33

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page