Skip to content
2 min read · 465 words

Class: TesseractJsOcrAdapter

Defined in: src/batteries/specialists/ocr/tesseract_js/adapter.ts:70

OCR adapter for tesseract.js.

Remarks

Reusable: construct once, call recognize as many times as needed. The worker is resolved lazily on first use (or via preload) and cached with single-flight semantics so concurrent calls share one load. See the module remarks for how this deliberately diverges from the per-call-worker MediaEngine in src/batteries/media/engines/tesseract_js.ts.

Constructors

Constructor

ts
new TesseractJsOcrAdapter(options: unknown): TesseractJsOcrAdapter;

Defined in: src/batteries/specialists/ocr/tesseract_js/adapter.ts:87

Parameters

ParameterTypeDescription
optionsunknownConstructor options. Validated eagerly.

Returns

TesseractJsOcrAdapter

Throws

@nhtio/adk/batteries!E_INVALID_TESSERACT_JS_OCR_OPTIONS when invalid.

Methods

dispose()

ts
dispose(): Promise<void>;

Defined in: src/batteries/specialists/ocr/tesseract_js/adapter.ts:127

Terminates the cached worker and drops the cached handle. Alias of reset — both reclaim the same underlying resource for this adapter (see reset's TSDoc).

Returns

Promise<void>


isAvailable()

ts
isAvailable(): boolean;

Defined in: src/batteries/specialists/ocr/tesseract_js/adapter.ts:92

Instance availability probe (honours an injected isAvailable).

Returns

boolean


preload()

ts
preload(): Promise<void>;

Defined in: src/batteries/specialists/ocr/tesseract_js/adapter.ts:97

Eagerly loads (and caches) the worker so the first recognize call is fast. Idempotent.

Returns

Promise<void>


recognize()

ts
recognize(input: SpecialistImageInput, opts?: RecognizeOptions): Promise<RecognizeResult>;

Defined in: src/batteries/specialists/ocr/tesseract_js/adapter.ts:188

Recognizes text in an image.

Parameters

ParameterTypeDescription
inputSpecialistImageInputThe image/document input (bytes, bytes+MIME, or a Media-like value).
opts?RecognizeOptionsPer-call options. opts.languages, when given, must equal the constructor's languages (order-insensitive) — tesseract.js v7 workers do not support re-initializing an already-created worker's languages via a public, stable API (there is no worker.reinitialize re-language call safe to make on a warm worker without risking cross-call state bleed), so a genuinely different subset throws @nhtio/adk/batteries!E_TESSERACT_JS_OCR_ENGINE_ERROR explaining that per-call language switching requires a new adapter instance.

Returns

Promise<RecognizeResult>

The recognized text and, when tesseract reports a numeric confidence, the mean confidence (0..100).

Throws

@nhtio/adk/batteries!E_TESSERACT_JS_OCR_ENGINE_ERROR when the worker fails to load, the recognize call throws, or a per-call language override cannot be honored.


reset()

ts
reset(): Promise<void>;

Defined in: src/batteries/specialists/ocr/tesseract_js/adapter.ts:114

Terminates the cached worker (if any) and drops the cached handle + in-flight load, so the next call creates a fresh worker.

Returns

Promise<void>

Remarks

Unlike @nhtio/adk/batteries/embeddings/transformers_js!TransformersJsEmbeddingsAdapter.reset, which only nulls the JS reference and leaves native resources for dispose to reclaim, this reset() also terminates the worker. A live tesseract.js worker is the SAME heavy WASM resource dispose() releases — no lighter "just drop the reference" tier exists for it (there is no separate pipeline-session handle to keep warm), so leaving it running after reset() would just leak it under a different method name. Swallows a terminate error (teardown must not throw). Idempotent.


isAvailable()

ts
static isAvailable(): boolean;

Defined in: src/batteries/specialists/ocr/tesseract_js/adapter.ts:79

Whether this battery is available. tesseract.js is environment-neutral (Node + browser), so this is true whenever the runtime can import the peer.

Returns

boolean