captcha-onnx
Lightweight, fast Python Captcha OCR library powered by ONNX Runtime — bundled CRNN-CTC model, no TensorFlow or OpenCV required.
captcha-onnx is an open-source, offline Python OCR library for recognizing fixed-alphabet alphanumeric captcha images using a pre-trained CRNN + CTC deep learning model exported to ONNX. It ships with a 1.7 MB model bundled directly inside the package and runs in milliseconds on CPU with only onnxruntime, numpy, and pillow.
Sample Predictions
| Captcha Image | Predicted Text | Confidence |
|---|---|---|
2ABJNN |
98.4% |
|
2B87DN |
90.6% |
|
2BENRW |
98.9% |
|
2BFZBG |
97.9% |
|
2C7WKD |
99.6% |
Why captcha-onnx?
| Feature | captcha-onnx (ONNX Runtime) |
Traditional TensorFlow + OpenCV |
|---|---|---|
| Core dependencies | onnxruntime, numpy, pillow |
tensorflow (~1 GB), opencv-python |
| Bundled model | Ready out of the box (default.onnx, 1.7 MB) |
Manual model download & path setup |
| Disk I/O | 100% in-memory (bytes, BytesIO, PIL, ndarray) |
Often writes temp files to disk |
| Batch inference | Native single forward pass (predict_batch) |
Usually loops image-by-image |
| Offline & free | Runs locally on CPU/GPU, zero external API calls | Local or cloud |
| Python support | Python 3.10 – 3.13 (Linux, macOS, Windows) | Often pinned to older Python/TF versions |
Installation
Install from PyPI:
pip install captcha-onnx
Or with uv:
uv add captcha-onnx
Optional Extras
# Load legacy Keras .h5 / .keras models directly
pip install "captcha-onnx[tensorflow]"
# Convert custom Keras CRNN .h5 models to lightweight .onnx
pip install "captcha-onnx[convert]"
Quick Start
1. One-line OCR prediction
import captcha_onnx
# From a file path (str or pathlib.Path)
text = captcha_onnx.predict("assets/2ABJNN.png")
print(text) # '2ABJNN'
2. In-memory bytes, BytesIO, PIL.Image, or numpy.ndarray
No temporary files needed — pass image bytes directly from HTTP requests, web apps, or memory buffers:
import io
from PIL import Image
import captcha_onnx
raw_bytes = open("assets/2C7WKD.png", "rb").read()
# All of these produce '2C7WKD'
captcha_onnx.predict(raw_bytes)
captcha_onnx.predict(io.BytesIO(raw_bytes))
captcha_onnx.predict(Image.open("assets/2C7WKD.png"))
3. Confidence scores & reusable recognizer
from captcha_onnx import CaptchaOCR
ocr = CaptchaOCR()
result = ocr.predict_result("assets/2C7WKD.png")
print(result.text) # '2C7WKD'
print(result.confidence) # 0.9957
print(result.char_confidences) # per-character probabilities
4. Batch inference
Process multiple images in a single ONNX forward pass:
import captcha_onnx
texts = captcha_onnx.predict_batch([
"assets/2ABJNN.png",
"assets/2BENRW.png",
"assets/2C7WKD.png",
])
print(texts) # ['2ABJNN', '2BENRW', '2C7WKD']
Using Custom Models & GPU Acceleration
captcha-onnx supports custom ONNX CRNN-CTC models, custom character alphabets, GPU execution providers (e.g. CUDA / CoreML), and legacy Keras .h5/.keras models:
from captcha_onnx import CaptchaOCR, OCRConfig
config = OCRConfig(
characters=tuple("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
image_width=200,
image_height=40,
trim_end=0,
)
# Custom ONNX model with optional GPU acceleration
ocr = CaptchaOCR(
model_path="path/to/custom_model.onnx",
config=config,
providers=["CUDAExecutionProvider", "CPUExecutionProvider"],
)
# Legacy Keras .h5 / .keras model (requires `pip install "captcha-onnx[tensorflow]"`)
tf_ocr = CaptchaOCR(model_path="path/to/custom_model.h5", config=config)
Convert a Keras CRNN .h5 Model to ONNX
Export a Keras CRNN-CTC .h5 checkpoint to .onnx without installing TensorFlow:
pip install "captcha-onnx[convert]"
python scripts/convert_h5_to_onnx.py --input model.h5 --output model.onnx
API Reference
| Symbol | Description |
|---|---|
captcha_onnx.predict(image) |
Recognize text from a single image using the cached default recognizer. |
captcha_onnx.predict_result(image) |
Return a PredictionResult(text, confidence, char_confidences). |
captcha_onnx.predict_batch(images) |
Recognize a sequence of images in a single batch forward pass. |
captcha_onnx.predict_batch_results(images) |
Batch variant returning list[PredictionResult]. |
CaptchaOCR(model_path=None, config=None, backend=None, providers=None) |
Configurable recognizer class (supports custom .onnx, .h5/.keras, or custom InferenceBackend). |
OCRConfig(characters, image_width, image_height, blank_index, trim_end, transpose_image) |
Immutable preprocessing and CTC decoding configuration. |
CaptchaOCRError |
Base exception (ModelLoadError, ImageLoadError, BackendNotInstalledError). |
Default Model Specifications
| Property | Value |
|---|---|
| Architecture | 2× Conv2D + MaxPooling2D → Dense → 2× Bidirectional LSTM → Softmax + CTC |
| Model format | ONNX (opset 17, dynamic batch size, 1.71 MB) |
| Input tensor | [batch, 200, 40, 1] (float32, grayscale normalized to [0, 1]) |
| Character set (29 chars) | 2356789ABCDEFGHJKMNPQRSTVWXYZ |
FAQ
Does captcha-onnx require internet access or API keys?
No. Inference runs 100% locally and offline using the bundled ONNX model.
Can I run inference on GPU?
Yes. Install onnxruntime-gpu and pass providers=["CUDAExecutionProvider", "CPUExecutionProvider"] to CaptchaOCR(...).
What image formats are supported?
Any format readable by Pillow (PNG, JPEG, BMP, WEBP, GIF, etc.), as well as PIL.Image.Image and 2D/3D numpy.ndarray objects.
Development
git clone https://github.com/hotroso/captcha-onnx.git
cd captcha-onnx
pip install -e ".[dev,convert]"
# Run tests
pytest
# Lint with Ruff
ruff check src tests scripts
# Build sdist & wheel
python -m build
License
Released under the MIT License. Copyright © hotroso.
Metadata
Release files for captcha-onnx 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| captcha_onnx-1.0.1.tar.gz | 1.6 MB | With provenance | Details for captcha_onnx-1.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| captcha_onnx-1.0.1-py3-none-any.whl | Python 3 | none | any | Details for captcha_onnx-1.0.1-py3-none-any.whl |
Total release size: 3.2 MB
Release files / captcha_onnx-1.0.1.tar.gz
| Download URL | captcha_onnx-1.0.1.tar.gz |
|---|---|
| Size | 1.6 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3a5fa7f46743231784bd2061cfd35a056e8220d858a33b3b9a7b7b86c54ef219
|
|
BLAKE2b-256 checksum How to use checksums |
70ded76eeffad511ab13c62858d2c9a4e065facff097f346392b23ab777e96e9
|
| 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 logRelease files / captcha_onnx-1.0.1-py3-none-any.whl
| Download URL | captcha_onnx-1.0.1-py3-none-any.whl |
|---|---|
| Size | 1.6 MB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9dfb93426d95a4b164c5a2f87408df3b32541fb796327bec5a58e39821c670c8
|
|
BLAKE2b-256 checksum How to use checksums |
04c00ded666c9b711ce6109b0df278fa61680adbd9e544277cbda608b44f032a
|
| 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 logSecurity for captcha-onnx 1.0.1
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.
Attestation for captcha_onnx-1.0.1.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 logA 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.