插件化 CAPTCHA OCR 開發套件 - 輕量級、可擴展、自定義 handlers
Project description
captcha-ocr-devkit
captcha-ocr-devkit 是一套跨平台的 CAPTCHA OCR 開發工具箱,專注於「四字元小寫英文」驗證碼範例。框架提供完整的插件化 Handler 架構、內建 demo 與 transformer 範例,可初始化 handler 專案、訓練與評估模型、啟動 FastAPI 服務,並支援 JSON 與 multipart API 呼叫。
安裝 Installation
pip install captcha-ocr-devkit
# 依需求安裝額外功能
pip install "captcha-ocr-devkit[pillow]"
pip install "captcha-ocr-devkit[pytorch]"
pip install "captcha-ocr-devkit[dev]"
PyTorch builders 會依作業系統與硬體差異,請參考官方指引安裝對應版本。
快速上手 Quick Start
# 建立專案骨架 (複製 demo + transformer handlers)
captcha-ocr-devkit init
# 查看 CLI 使用說明
captcha-ocr-devkit --help
主要指令 CLI Reference
| Command | 說明 |
|---|---|
captcha-ocr-devkit init |
複製 demo 與 transformer handlers,支援 --handler-dir 指定自訂模板 |
captcha-ocr-devkit train |
依指定 handler 執行模型訓練 (如 transformer_train) |
captcha-ocr-devkit evaluate |
使用 handler 進行模型評估 (如 transformer_evaluate) |
captcha-ocr-devkit api |
啟動 FastAPI 服務 (如 transformer_ocr) |
captcha-ocr-devkit create-handler |
產生全新的 handler 骨架 |
別名 captcha-ocr-helper 等同於上述 CLI。
Handler 概觀
- DemoHandler:展示用範例,透過 fake OCR 回傳固定/隨機結果,設計目的是示範 handler 架構、流程與 metadata。適合複製模版來擴充自己的 handler。
- TransformerHandlers:實務可用的一組 handler (
transformer_preprocess,transformer_train,transformer_evaluate,transformer_ocr)。提供真實的資料前處理、訓練、推論與 API 整合,處理過程會回報版本資訊、損失與驗證指標,API 回傳包含image_size與 per-character confidence。
Transformer 實務流程範例
以下示範在 macOS (Python 3.12.11) 上建立環境、訓練並啟動 API:
sw_vers
ProductName: macOS
ProductVersion: 26.0
BuildVersion: 25A354
python3 -V
Python 3.12.10
python3 -m venv venv
source venv/bin/activate
pip install captcha-ocr-devkit
cp -r /path/data/ data/
captcha-ocr-devkit init
pip install -r handlers/transformer_handler-requirements.txt
PYTORCH_ENABLE_MPS_FALLBACK=1 captcha-ocr-devkit train \
--input ./data \
--handler transformer_train \
--output model \
--epochs 250 --batch-size 32 --learning-rate 0.000125
captcha-ocr-devkit evaluate \
--target ./data \
--model model \
--handler transformer_evaluate
captcha-ocr-devkit api \
--handler transformer_ocr \
--model model
訓練過程會持續 flush log 顯示 core/handler 版本與每個 epoch 的 loss、val_acc、val_cer;API 啟動後可透過 GET /api/v1/ocr 檢查服務狀態。
API 使用範例
# GET 健康檢查
curl 'http://127.0.0.1:54321/api/v1/ocr'
# POST (JSON + Base64)
curl 'http://127.0.0.1:54321/api/v1/ocr' \
-H 'Content-Type: application/json' \
--data '{"image": "<BASE64_STRING>", "format": "png"}'
# POST (Multipart)
curl -X POST 'http://127.0.0.1:54321/api/v1/ocr' \
-F 'file=@captcha.png'
回傳的 details 會附上原始尺寸、處理後尺寸與 per-character confidences。
專案結構 Project Layout
py-captcha-ocr-devkit/
├── handlers/ # 使用者自訂 handlers (init 後生成)
├── src/captcha_ocr_devkit/
│ ├── core/ # pipeline、registry、base handlers
│ ├── api/ # FastAPI routes 與 schemas
│ ├── cli/ # Typer CLI
│ └── examples/handlers/ # demo + transformer 範例
├── tests/ # pytest suites
├── docs/
├── main.py
├── requirements.txt
└── setup.py
開發指南 Development
python -m venv venv
source venv/bin/activate
pip install -e .[dev]
captcha-ocr-devkit init
pytest -v --cov=src/captcha_ocr_devkit
更新 handler 後記得重新執行 captcha-ocr-devkit init 以同步最新範例資產。
授權 License
MIT License © changyy
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file captcha_ocr_devkit-1.20250921.923.tar.gz.
File metadata
- Download URL: captcha_ocr_devkit-1.20250921.923.tar.gz
- Upload date:
- Size: 84.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f4920d8e163be113c1880f9334de4fd599fc7e57ef77c3454e3e63c8bb2d5889
|
|
| MD5 |
2c698e6c8e109d863c7765f1da21c8fc
|
|
| BLAKE2b-256 |
59cb7a4b1c7ed6e289ed0a5f38301092b08be76cd28fae2aabf78558c80cd045
|
Provenance
The following attestation bundles were made for captcha_ocr_devkit-1.20250921.923.tar.gz:
Publisher:
python-publish.yml on changyy/py-captcha-ocr-devkit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
captcha_ocr_devkit-1.20250921.923.tar.gz -
Subject digest:
f4920d8e163be113c1880f9334de4fd599fc7e57ef77c3454e3e63c8bb2d5889 - Sigstore transparency entry: 550654792
- Sigstore integration time:
-
Permalink:
changyy/py-captcha-ocr-devkit@c18b68248fe67050e5a673036d7b2a2928d01a2e -
Branch / Tag:
refs/tags/v1.20250921.923 - Owner: https://github.com/changyy
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@c18b68248fe67050e5a673036d7b2a2928d01a2e -
Trigger Event:
release
-
Statement type:
File details
Details for the file captcha_ocr_devkit-1.20250921.923-py3-none-any.whl.
File metadata
- Download URL: captcha_ocr_devkit-1.20250921.923-py3-none-any.whl
- Upload date:
- Size: 102.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e299c0eb5b1cdfd81a86d6f891d823b1d589fc170ede057153eefe1db2236e00
|
|
| MD5 |
1cc494dea5d620fae83905dcd7550d54
|
|
| BLAKE2b-256 |
b9b58aff9e841ec92882150ad6a530228e06c7fa7677bca51e50cda761ea016d
|
Provenance
The following attestation bundles were made for captcha_ocr_devkit-1.20250921.923-py3-none-any.whl:
Publisher:
python-publish.yml on changyy/py-captcha-ocr-devkit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
captcha_ocr_devkit-1.20250921.923-py3-none-any.whl -
Subject digest:
e299c0eb5b1cdfd81a86d6f891d823b1d589fc170ede057153eefe1db2236e00 - Sigstore transparency entry: 550654803
- Sigstore integration time:
-
Permalink:
changyy/py-captcha-ocr-devkit@c18b68248fe67050e5a673036d7b2a2928d01a2e -
Branch / Tag:
refs/tags/v1.20250921.923 - Owner: https://github.com/changyy
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@c18b68248fe67050e5a673036d7b2a2928d01a2e -
Trigger Event:
release
-
Statement type: