Skip to main content

captcha-onnx

CI Python 3.10+ License: MIT

Lightweight, fast Python OCR library for reading fixed-alphabet alphanumeric captcha images using a bundled CRNN-CTC model powered by ONNX Runtime — no TensorFlow or OpenCV required.

Features

  • Zero-config, bundled model: Ships with a pre-trained 1.7 MB ONNX model (default.onnx) ready to use immediately after pip install.
  • Lightweight & fast: Only depends on onnxruntime, numpy, and pillow (works on Python 3.10–3.13 across Linux, macOS, and Windows).
  • Flexible inputs: Accepts file paths (str / Path), raw bytes / bytearray, binary file streams (BytesIO), PIL.Image.Image, and numpy.ndarray directly in memory (no temporary files on disk).
  • Batch inference & confidence scores: Run multiple images in a single forward pass and inspect per-character confidence scores.
  • Extensible: Bring your own custom .onnx model and character set, or load legacy Keras .h5 / .keras models via the optional [tensorflow] extra.

Installation

pip install captcha-onnx

Install from GitHub:

pip install git+https://github.com/hotroso/captcha-onnx.git

Optional extras:

# Support loading legacy Keras .h5 / .keras models directly
pip install "captcha-onnx[tensorflow]"

# Tools for converting Keras CRNN .h5 models to .onnx
pip install "captcha-onnx[convert]"

Quick Start

One-line prediction

import captcha_onnx

# From a file path
text = captcha_onnx.predict("assets/2ABJNN.png")
print(text)  # '2ABJNN'

# From raw bytes (e.g. downloaded image or HTTP upload)
with open("assets/2C7WKD.png", "rb") as f:
    text = captcha_onnx.predict(f.read())
print(text)  # '2C7WKD'

# Batch prediction
texts = captcha_onnx.predict_batch(["assets/2ABJNN.png", "assets/2BENRW.png"])
print(texts)  # ['2ABJNN', '2BENRW']

Confidence scores & reusable recognizer

from PIL import Image
from captcha_onnx import CaptchaOCR

ocr = CaptchaOCR()

img = Image.open("assets/2C7WKD.png")
result = ocr.predict_result(img)

print(result.text)              # '2C7WKD'
print(result.confidence)        # ~0.996
print(result.char_confidences)  # per-character probabilities

Custom ONNX or Keras model

from captcha_onnx import CaptchaOCR, OCRConfig

config = OCRConfig(
    characters=tuple("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
    image_width=200,
    image_height=40,
    trim_end=0,
)

# Custom ONNX model (also supports GPU providers such as CUDAExecutionProvider)
ocr = CaptchaOCR(
    model_path="path/to/my_model.onnx",
    config=config,
    providers=["CPUExecutionProvider"],
)

# Or load a Keras .h5 / .keras model (requires `pip install captcha-onnx[tensorflow]`)
tf_ocr = CaptchaOCR(model_path="path/to/my_model.h5", config=config)

Default Model Specifications

Property Value
Architecture 2-layer CNN + 2-layer Bidirectional LSTM + CTC
Format ONNX (opset 17, dynamic batch size, ~1.7 MB)
Input size 200 × 40 grayscale (float32 normalized to [0, 1])
Character set (29 chars) 2356789ABCDEFGHJKMNPQRSTVWXYZ

Converting a Keras .h5 Model to ONNX

If you train a new CRNN-CTC model with the same architecture and want to export it to lightweight ONNX without TensorFlow:

pip install "captcha-onnx[convert]"
python scripts/convert_h5_to_onnx.py --input my_model.h5 --output my_model.onnx

Development

git clone https://github.com/hotroso/captcha-onnx.git
cd captcha-onnx
pip install -e ".[dev]"

# Run unit tests
pytest

# Lint code
ruff check src tests scripts

# Build sdist & wheel
python -m build

Publishing to PyPI

This repository includes a Trusted Publishing workflow at .github/workflows/publish.yml.

  1. Configure a Trusted Publisher on PyPI for hotroso/captcha-onnx (publish.yml, environment pypi).
  2. Bump version in pyproject.toml and src/captcha_onnx/__init__.py.
  3. Create and publish a GitHub Release — the workflow builds and uploads the wheel & sdist automatically.

License

MIT License — see LICENSE for details.

Metadata

Release files for captcha-onnx 1.0.0

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

Source distribution (sdist)

Source distribution for captcha-onnx 1.0.0
File Size Uploaded Provenance Actions
captcha_onnx-1.0.0.tar.gz 1.6 MB With provenance Details for captcha_onnx-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for captcha-onnx 1.0.0
File Interpreter ABI Platform Actions
captcha_onnx-1.0.0-py3-none-any.whl Python 3 none any Details for captcha_onnx-1.0.0-py3-none-any.whl

Total release size: 3.2 MB

Release files / captcha_onnx-1.0.0.tar.gz

Download URL captcha_onnx-1.0.0.tar.gz
Size 1.6 MB
Tags Source
SHA-256 checksum
How to use checksums
3fd67ea9d8a9dd8078ab9d26f8e35ce052737cdfd6899de9521adfe5497a8551
BLAKE2b-256 checksum
How to use checksums
4c121ab90c193c4c79789c9be7614004949e7fa7f605e1c372fbe86d32905f2b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.

Transparency log

Release files / captcha_onnx-1.0.0-py3-none-any.whl

Download URL captcha_onnx-1.0.0-py3-none-any.whl
Size 1.6 MB
Tags Python 3
SHA-256 checksum
How to use checksums
b4542928b29f2a939a84ca3d7414f373befc96fd0ced20faadb14fc6b0dd03a5
BLAKE2b-256 checksum
How to use checksums
7a4833293d0d1336129a78890af1a4c6cabd0fb3517559da09cf56b4e8d3b878
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.

Transparency log

Security for captcha-onnx 1.0.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for captcha_onnx-1.0.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.

Transparency log

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.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files Full provenance

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