Image-to-Text
Transformers
ONNX
Safetensors
vision-encoder-decoder
image-text-to-text
trocr
ocr
captcha
math-captcha
synthetic-data
Instructions to use arkhabbazan/ocr-math-captcha with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use arkhabbazan/ocr-math-captcha with Transformers:
# Use a pipeline as a high-level helper # Warning: Pipeline type "image-to-text" is no longer supported in transformers v5. # You must load the model directly (see below) or downgrade to v4.x with: # pip install "transformers<5.0.0" from transformers import pipeline pipe = pipeline("image-to-text", model="arkhabbazan/ocr-math-captcha")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForMultimodalLM tokenizer = AutoTokenizer.from_pretrained("arkhabbazan/ocr-math-captcha") model = AutoModelForMultimodalLM.from_pretrained("arkhabbazan/ocr-math-captcha", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Publish complete model card with 2x2 captcha gallery
Browse files
README.md
CHANGED
|
@@ -17,19 +17,26 @@ tags:
|
|
| 17 |
A TrOCR model fine-tuned to recognize short mathematical captcha
|
| 18 |
expressions such as `26+7=?` and `45-6=?`.
|
| 19 |
|
| 20 |
-
<div
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
## Model details
|
| 23 |
|
| 24 |
- **Architecture:** Vision Encoder-Decoder / TrOCR
|
| 25 |
- **Base model:** `microsoft/trocr-base-printed`
|
| 26 |
- **Task:** Image-to-text OCR
|
| 27 |
-
- **
|
| 28 |
-
- **
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
## Training data
|
| 31 |
|
| 32 |
-
The model was fine-tuned on 10,000 target-matched synthetic
|
|
|
|
| 33 |
|
| 34 |
| Split | Samples |
|
| 35 |
|---|---:|
|
|
@@ -37,9 +44,31 @@ The model was fine-tuned on 10,000 target-matched synthetic images:
|
|
| 37 |
| Validation | 1,000 |
|
| 38 |
| Test | 500 |
|
| 39 |
|
| 40 |
-
The generator balances `+` and `-`
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
## Training configuration
|
| 45 |
|
|
@@ -53,29 +82,102 @@ background noise, and small character displacement.
|
|
| 53 |
| Generation beams | 4 |
|
| 54 |
| Maximum output length | 32 |
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
## Usage
|
| 57 |
|
| 58 |
```python
|
|
|
|
| 59 |
from PIL import Image
|
| 60 |
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
| 61 |
|
| 62 |
repo_id = "arkhabbazan/ocr-match-captcha"
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
image = Image.open("captcha.jpeg").convert("RGB")
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
prediction = processor.batch_decode(
|
| 70 |
-
generated_ids,
|
|
|
|
| 71 |
)[0]
|
| 72 |
-
|
|
|
|
|
|
|
| 73 |
```
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
## Limitations
|
| 76 |
|
| 77 |
-
- Training data is primarily synthetic.
|
| 78 |
-
- Unseen fonts and layouts may reduce accuracy.
|
| 79 |
-
- Very faint or heavily occluded operators may be missed.
|
| 80 |
- The model is intended for short expressions, not document OCR.
|
| 81 |
- Usage must be authorized and compliant with applicable policies.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
A TrOCR model fine-tuned to recognize short mathematical captcha
|
| 18 |
expressions such as `26+7=?` and `45-6=?`.
|
| 19 |
|
| 20 |
+
<div align="center">
|
| 21 |
+
<img src="assets/captcha-handwritten-7-plus-12.png" alt="Handwritten mathematical captcha" width="300" height="100"> <img src="assets/captcha-clean-10-plus-3.png" alt="Clean mathematical captcha" width="300" height="100"><br>
|
| 22 |
+
<img src="assets/captcha-distorted-18-plus-5.png" alt="Distorted mathematical captcha" width="300" height="100"> <img src="assets/captcha-omex-37-plus-13.png" alt="OMEX mathematical captcha" width="300" height="100">
|
| 23 |
+
</div>
|
| 24 |
|
| 25 |
## Model details
|
| 26 |
|
| 27 |
- **Architecture:** Vision Encoder-Decoder / TrOCR
|
| 28 |
- **Base model:** `microsoft/trocr-base-printed`
|
| 29 |
- **Task:** Image-to-text OCR
|
| 30 |
+
- **Training target geometry:** 130x30 RGB images
|
| 31 |
+
- **Model-card example size:** 300x100 pixels
|
| 32 |
+
- **Output format:** Mathematical expressions without spaces
|
| 33 |
+
- **Supported operators:** `+` and `-`
|
| 34 |
+
- **Expected expression format:** `number operator number = ?`
|
| 35 |
|
| 36 |
## Training data
|
| 37 |
|
| 38 |
+
The model was fine-tuned on 10,000 target-matched synthetic captcha
|
| 39 |
+
images.
|
| 40 |
|
| 41 |
| Split | Samples |
|
| 42 |
|---|---:|
|
|
|
|
| 44 |
| Validation | 1,000 |
|
| 45 |
| Test | 500 |
|
| 46 |
|
| 47 |
+
The synthetic generator balances `+` and `-` expressions and includes
|
| 48 |
+
`=` and `?` symbols. It simulates several real captcha characteristics:
|
| 49 |
+
|
| 50 |
+
- Thin and low-contrast arithmetic symbols
|
| 51 |
+
- Pastel circle and ring overlap
|
| 52 |
+
- Background noise
|
| 53 |
+
- Blur and interpolation artifacts
|
| 54 |
+
- JPEG compression
|
| 55 |
+
- Small character displacement
|
| 56 |
+
- Variable character spacing
|
| 57 |
+
- Different foreground contrast levels
|
| 58 |
+
|
| 59 |
+
Labels contain no spaces and use ASCII `-` for subtraction. Visual
|
| 60 |
+
spacing is applied only while rendering the image.
|
| 61 |
+
|
| 62 |
+
## Training procedure
|
| 63 |
+
|
| 64 |
+
The complete Vision Encoder-Decoder model was fine-tuned using an
|
| 65 |
+
operator-weighted sequence loss. Tokens representing `+`, `-`, `=`,
|
| 66 |
+
and `?` received additional weight so that digit tokens did not dominate
|
| 67 |
+
the training objective.
|
| 68 |
+
|
| 69 |
+
Checkpoint selection used exact-expression accuracy on the validation
|
| 70 |
+
split. The held-out test split was evaluated only after the best
|
| 71 |
+
validation checkpoint had been restored.
|
| 72 |
|
| 73 |
## Training configuration
|
| 74 |
|
|
|
|
| 82 |
| Generation beams | 4 |
|
| 83 |
| Maximum output length | 32 |
|
| 84 |
|
| 85 |
+
## Evaluation
|
| 86 |
+
|
| 87 |
+
Evaluation measures both complete expression recognition and individual
|
| 88 |
+
character groups.
|
| 89 |
+
|
| 90 |
+
| Metric | Validation | Test |
|
| 91 |
+
|---|---:|---:|
|
| 92 |
+
| Character error rate | 0.000% | 0.000% |
|
| 93 |
+
| Character accuracy | 100.000% | 100.000% |
|
| 94 |
+
| Exact-expression accuracy | 100.000% | 100.000% |
|
| 95 |
+
| Arithmetic-operator accuracy | 100.000% | 100.000% |
|
| 96 |
+
| Symbol-sequence accuracy | 100.000% | 100.000% |
|
| 97 |
+
| Digit-sequence accuracy | 100.000% | 100.000% |
|
| 98 |
+
|
| 99 |
+
These results measure the synthetic rendering family used during
|
| 100 |
+
fine-tuning. They do not guarantee identical accuracy for every real
|
| 101 |
+
captcha provider, unseen font, or previously unseen visual style.
|
| 102 |
+
|
| 103 |
## Usage
|
| 104 |
|
| 105 |
```python
|
| 106 |
+
import torch
|
| 107 |
from PIL import Image
|
| 108 |
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
| 109 |
|
| 110 |
repo_id = "arkhabbazan/ocr-match-captcha"
|
| 111 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 112 |
+
|
| 113 |
+
processor = TrOCRProcessor.from_pretrained(
|
| 114 |
+
repo_id,
|
| 115 |
+
token=True,
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
model = VisionEncoderDecoderModel.from_pretrained(
|
| 119 |
+
repo_id,
|
| 120 |
+
token=True,
|
| 121 |
+
).to(device).eval()
|
| 122 |
|
| 123 |
image = Image.open("captcha.jpeg").convert("RGB")
|
| 124 |
+
|
| 125 |
+
# Keep the original image aspect ratio. The TrOCR processor performs
|
| 126 |
+
# the model's required resizing and normalization.
|
| 127 |
+
pixel_values = processor(
|
| 128 |
+
images=image,
|
| 129 |
+
return_tensors="pt",
|
| 130 |
+
).pixel_values.to(device)
|
| 131 |
+
|
| 132 |
+
with torch.inference_mode():
|
| 133 |
+
generated_ids = model.generate(
|
| 134 |
+
pixel_values,
|
| 135 |
+
num_beams=4,
|
| 136 |
+
max_length=32,
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
prediction = processor.batch_decode(
|
| 140 |
+
generated_ids,
|
| 141 |
+
skip_special_tokens=True,
|
| 142 |
)[0]
|
| 143 |
+
|
| 144 |
+
prediction = prediction.replace(" ", "")
|
| 145 |
+
print(prediction)
|
| 146 |
```
|
| 147 |
|
| 148 |
+
Example output:
|
| 149 |
+
|
| 150 |
+
```text
|
| 151 |
+
26+7=?
|
| 152 |
+
```
|
| 153 |
+
|
| 154 |
+
## Input preprocessing
|
| 155 |
+
|
| 156 |
+
Pass the original RGB captcha image directly to `TrOCRProcessor`.
|
| 157 |
+
Do not manually stretch a wide captcha to a square such as 130x130,
|
| 158 |
+
because that introduces a mismatch between training and inference.
|
| 159 |
+
|
| 160 |
+
The 300x100 dimensions used by the four images in this model card are
|
| 161 |
+
only for consistent presentation. They are not a required inference
|
| 162 |
+
input size.
|
| 163 |
+
|
| 164 |
+
## Intended use
|
| 165 |
+
|
| 166 |
+
This model is intended for:
|
| 167 |
+
|
| 168 |
+
- Short mathematical captcha OCR
|
| 169 |
+
- Authorized captcha-processing workflows
|
| 170 |
+
- OCR experimentation
|
| 171 |
+
- Synthetic-to-real domain adaptation research
|
| 172 |
+
- Arithmetic-symbol recognition evaluation
|
| 173 |
+
|
| 174 |
## Limitations
|
| 175 |
|
|
|
|
|
|
|
|
|
|
| 176 |
- The model is intended for short expressions, not document OCR.
|
| 177 |
- Usage must be authorized and compliant with applicable policies.
|
| 178 |
+
|
| 179 |
+
## Responsible use
|
| 180 |
+
|
| 181 |
+
Use this model only on systems and captcha images that you are
|
| 182 |
+
authorized to process. Users are responsible for complying with
|
| 183 |
+
applicable laws, service terms, and access-control policies.
|