Skip to content

Captcha templates

@scwrap/captcha-templates is a local, free TextCaptchaSolver for fixed-font text captchas, with a trainer that builds a glyph model from a site's samples.

@scwrap/captcha-templates reads text captchas without calling anything. It is a provider for the TextCaptchaSolver Capability.

It works for captchas that always use the same font: it cuts the image into letters and compares each one with a stored template. For such captchas it is fast, free and accurate. It does not work for captchas whose letters overlap or whose font varies.

ExportJob
trainGlyphModel(settings, captchas)Builds a glyph model from labelled samples.
GlyphModelThe schema of a glyph model, for storing it as data.
glyphModelSolver(model, name?)The solver as an Effect, for use in a firstConfident chain.
makeTemplateTextSolver(model, name?)The solver from an already decoded model.
upscaleCaptcha(factor)Turns a JPEG captcha into a larger PNG, for a model that reads small images badly.

The glyph model ​

A glyph model is what the solver knows about one site's captchas: one template image per letter, plus the settings used to cut a captcha into letters.

It is plain data. It ships next to the site's Scraper, in the Scraper's package.

SettingMeaning
width, heightThe captcha's size in pixels.
glyphsHow many letters a captcha has.
darkLuminancePixels darker than this count as ink. 0 to 255.
minimumComponentConnected ink smaller than this is noise.
openingSizeHow strongly thin lines are removed before cutting.
distinctLettersWhether a captcha never repeats a letter. The solver uses it to rule out impossible readings.

Train a model ​

Collect captchas whose answers you know to be right, ideally confirmed by the site accepting them. Name each file after its answer.

Example (A training script for four-letter captchas of 180 by 50 pixels)

ts
import { GlyphModel, trainGlyphModel } from "@scwrap/captcha-templates";
import { Schema } from "effect";

const model = trainGlyphModel(
  {
    darkLuminance: 130,
    distinctLetters: true,
    glyphs: 4,
    height: 50,
    minimumComponent: 25,
    openingSize: 3,
    width: 180,
  },
  captchas
);

const encoded = Schema.encodeSync(GlyphModel)(model);

captchas is an array of { answer, jpeg }. Samples that do not cut into the expected number of letters are skipped.

The script writes encoded into a source file that the Scraper imports. The solver test trains a model on synthetic captchas the same way.

Keep some verified captchas out of the training set. Measure the solver on those before you trust it.

Use the solver ​

Example (A Layer for one site)

ts
import { glyphModelSolver } from "@scwrap/captcha-templates";
import { TextCaptchaSolver } from "@scwrap/domain";
import { Layer } from "effect";

import { siteGlyphModel } from "./site-glyph-model.js";

export const siteGlyphSolver = glyphModelSolver(siteGlyphModel, "site-templates");

export const SiteTemplateCaptchaSolver = Layer.effect(
  TextCaptchaSolver,
  siteGlyphSolver
);

The second argument names the solver in readings and spans.

What a reading means ​

The solver returns the best word and a confidence from 0 to 1. The confidence is that of its least certain letter.

It fails with CaptchaUnreadable when:

  • the image is not a JPEG,
  • the JPEG cannot be decoded,
  • the image cuts into a different number of letters than expected.

A low confidence is not a failure. Decide on a threshold from your held-out samples and put a second solver behind it with firstConfident. For example, accept template readings from 0.85 and ask a model below that.

upscaleCaptcha ​

upscaleCaptcha(factor) returns a function for the prepare option of TextCaptchaSolver.fromDecisionModel. It decodes a JPEG captcha, enlarges it with bilinear interpolation and encodes it as PNG.

ts
TextCaptchaSolver.fromDecisionModel({
  prepare: upscaleCaptcha(4),
  solver: "clef",
});

A factor of 1, or a captcha that is not a JPEG, passes through unchanged.

Source ​