Skip to content

E-captcha solver: open-source captcha OCR in Python

Abstract. E-captcha solver is a text captcha solver written in Python. A PyTorch CRNN reads five-letter E-captcha images and is trained with CTC loss, so the labels are plain strings with no character positions. The README states about 95% accuracy. The repository holds the model, the training script, a synthetic data generator, an ONNX export and three ways to run inference: a Python class, a FastAPI endpoint and a Node.js module. Trained weights are not included.

Read the quick start Source on GitHub

No.Input image, 200 x 50Label
01E-captcha sample image labelled merbcmerbc
02E-captcha sample image labelled tkcdvtkcdv
03E-captcha sample image labelled rhecfrhecf
04E-captcha sample image labelled mjufvmjufv
05E-captcha sample image labelled drgufdrguf
06E-captcha sample image labelled aefeuaefeu
07E-captcha sample image labelled fhfsvfhfsv
08E-captcha sample image labelled jebvhjebvh
09E-captcha sample image labelled btffjbtffj
10E-captcha sample image labelled zhcabzhcab
Fig. 1. The ten labelled images in samples/. Each label is the ground truth from the file name. No trained weights ship with the repository, so this page shows no predictions.

The model

The network in model_ctc.py has three parts. Five convolutional blocks turn a 200 × 50 grayscale image into a strip that is 1 pixel high, 50 wide and 256 channels deep. Each block is two 3 × 3 convolutions with batch normalisation and ReLU, followed by max pooling.

Only the first two blocks pool horizontally. The width stops at 50 while the height keeps shrinking to 1, and those 50 columns become the time steps of a sequence.

A two-layer bidirectional LSTM with 256 hidden units reads the sequence in both directions. A linear layer then scores 20 classes at every step: the 19 letters of the alphabet plus the CTC blank. The whole model has 4,994,676 trainable parameters, counted from the layer definitions.

CRNN explained, layer by layer

Inputgrayscale, scaled to [-1, 1]1 × 50 × 200Conv block 11 → 32 channels, pool 2 × 232 × 25 × 100Conv block 232 → 64 channels, pool 2 × 264 × 12 × 50Conv block 364 → 128 channels, pool 2 × 1128 × 6 × 50Conv block 4128 → 256 channels, pool 2 × 1256 × 3 × 50Conv block 5256 → 256 channels, pool 3 × 1256 × 1 × 50Squeeze and permutewidth becomes 50 time steps50 × 256Bidirectional LSTM2 layers, 256 hidden, dropout 0.250 × 512Linear512 → 20 classes per step50 × 20merbcGreedy CTC decodeargmax, collapse, drop blanks5 letters
Fig. 2. The CRNN as defined in model_ctc.py, with the output shape of each stage. Feature maps are drawn to scale. The output shown is the label of sample 01.

Decoding with CTC

The model makes 50 predictions for a five-letter answer, and CTC decides how they line up. In training, nn.CTCLoss sums over every 50-step path that reduces to the label. At inference greedy_decode takes the top class at each step, merges runs of the same class and removes blanks.

The blank is what lets a doubled letter survive. In btffj the two runs of f have a blank between them, so they stay two letters. Without it they would merge into one.

  1. Top class at each of the 50 steps------bbb-tt--fff-ff--jj--------------------------
  2. Collapse repeats-b-t-f-f-j-
  3. Drop blanksbtffj
Fig. 3. Greedy CTC decoding on sample 09. Each of the 50 steps advances 4 pixels across the image. The path is written by hand to show the rule. It is not model output.

CTC loss and decoding in this code

What the repository states

Table 1. Figures and constants, each with the file it comes from.
PropertyValueSource
Accuracy, full stringabout 95%Readme.md, captcha_ctc_colab.ipynb
Alphabetabcdefghjkmnrstuvyzmodel_ctc.py
Characters per image5model_ctc.py
Model input200 × 50 grayscale, scaled to [-1, 1]model_ctc.py
Model output50 time steps × 20 classesmodel_ctc.py
Trainable parameters4,994,676counted from model_ctc.py
Default training run400 epochs, batch 64, Adam at 1e-3, cosine scheduletrain_ctc.py
Training time15 to 20 minutes on a Colab GPUcaptcha_ctc_colab.ipynb
Validation hold-out200 imagestrain_ctc.py

The notebook adds that a retry or two puts the effective success rate near 100%. Every prediction comes back with a confidence score, so a caller can retry the weak ones. The repository has no benchmark set beyond the ten images in Fig. 1, and this page does not reproduce the accuracy figure.

Running it

Put a trained ctc_model.pt in the project root first. The Python class returns the text and a confidence between 0 and 1. The HTTP server and the Node.js module wrap the same model.

from infer import CaptchaSolver

solver = CaptchaSolver("ctc_model.pt")
text, conf = solver.solve("captcha.jpg")
text, conf = solver.solve_bytes(response.content)
Listing 1. Python, from infer.py.
pip install fastapi uvicorn python-multipart
uvicorn server:app --port 8000
curl -F file=@captcha.jpg http://localhost:8000/solve
# {"text":"hvnyf","conf":0.99}
Listing 2. HTTP, from server.py.
python export_onnx.py --model ctc_model.pt --out ctc_model.onnx
cd node && npm install
node solve.js ../captcha.jpg ../ctc_model.onnx
# hvnyf
Listing 3. Node.js over ONNX, from node/solve.js.

Scope

The model only knows the E-captcha alphabet and the five-character length, so it is not a general captcha solver. Other text captchas need their own training run. A different alphabet or length also means changing the constants in model_ctc.py, gen_synthetic.py and node/solve.js.

Challenge widgets such as reCAPTCHA or hCaptcha are out of scope.

Guides

  1. Solve a captcha in Python: quick startInstall, add weights, read one image or a folder from the command line or from your own code.
  2. CRNN explained with a real captcha modelEvery layer of the network with its output shape, and why the width ends at 50.
  3. CTC loss and CTC decoding explainedHow 50 predictions become five letters, in training and at inference.
  4. Train a captcha recognition modelLabels file, augmentation, synthetic images, pseudo-labelling and the Colab notebook.
  5. ONNX export and Node.js inferenceExport the checkpoint once and run it from Node with onnxruntime-node and sharp.
  6. Captcha solver API with FastAPIThe POST /solve endpoint, its request and response, and what it does not handle.

MIT licence. Reads E-captcha text images only. Challenge widgets are out of scope.