$ show naptha/tesseract.js
Tesseract.js is a JavaScript library that brings the Tesseract OCR engine to the browser and Node.js by wrapping a WebAssembly port (tesseract.js-core). It enables optical character recognition (OCR) from images in over 100 languages, supporting multiple output formats (plain text, hOCR, TSV, PDF, JSON blocks, etc.) and pre-processing options like auto-rotation and rectangle cropping. The library's architecture is centered around two abstractions: Workers (each managing a Tesseract instance in a Web Worker or Worker Thread) and Schedulers (managing pools of Workers for parallel job execution). Key entry points are `createWorker` (which handles language loading, core initialization, and provides `recognize`, `detect`, `setParameters`, etc.) and `createScheduler` (which queues jobs among multiple Workers). Images can be provided as URLs, base64 strings, Buffers, File/Blob objects, or DOM elements (browser only). The core flow involves loading the appropriate WebAssembly binary based on SIMD support, downloading and caching language data (via IndexedDB or filesystem), initializing the Tesseract API, running recognition, and dumping results through a clean output abstraction. External dependencies include tesseract.js-core, wasm-feature-detect, idb-keyval, zlibjs, bmp-js, and node-fetch. The codebase is well-structured, modular, and has comprehensive tests (both Node and browser via Karma+Mocha), though some modules (like `createWorker`) mix concerns of worker lifecycle, job management, and message handling in a single file.
The LLM identified 3 files worth highlighting for reuse:
Implements a reusable job scheduler that manages a pool of workers and queues jobs. Workers are added via `addWorker`, jobs are enqueued with `addJob` (which returns a Promise), and the scheduler automatically assigns jobs to idle workers. The `dequeue` function ensures fair distribution and concurrency. This pattern could be extracted for any scenario requiring parallel processing with limited resources.
A clean, modular utility that converts Tesseract API calls into a standardized JSON output object. Supports multiple output formats (text, hocr, tsv, box, unlv, osd, pdf, image variants, blocks, layoutBlocks, confidence, debug) with a simple boolean flag system. The `processOutput` helper efficiently skips recognition when only non-recognition outputs (like layout blocks or image processing) are requested. This separation of result formatting from execution makes it easy to add new output formats without modifying recognition logic.
A thin, self-contained utility that detects the current JavaScript runtime (webworker, browser, or node) by checking for `WorkerGlobalScope`, `document`, and `process` globals. This pattern is critical for isomorphic libraries that need to swap implementations (e.g., file I/O, Worker creation) between browser and Node environments. Extracting this to a standalone function enables easy reuse in any cross-platform project.