Tesseract.js

repository·master·Indexed 13 days ago

https://github.com/naptha/tesseract.js

A pure JavaScript multilingual OCR library that enables Optical Character Recognition in the browser and Node.js by wrapping a WebAssembly port of the Tesseract OCR engine. Version 7.0.0 requires Node.js v16 or newer.

Tokens
11.2K
Snippets
34
Records
49
Agent score
98%

What's inside Tesseract.js

  1. Scope and limitations of Tesseract.js

    master

    Tesseract.js is a JavaScript/WebAssembly port of the Tesseract OCR engine.

    Key Limitations:

    • Handwritten Text: Not supported. The model is optimized for printed text; results for handwriting will be poor regardless of configuration.
    • Engine Bugs: Since Tesseract.js does not modify the underlying engine, bugs originating in the Tesseract engine should be reported to the main Tesseract repository, not here.
    • Framework Support: Supports all frameworks with WebAssembly support, except for React Native (which lacks WebAssembly support).
  2. Create and manage a Scheduler for parallel processing

    master

    A Scheduler manages a job queue and a pool of workers to enable multiple workers to work together, which is useful for speeding up performance through parallelization. You can create a scheduler using createScheduler() and then add workers to it using addWorker(worker).

    const { createWorker, createScheduler } = Tesseract;
    const scheduler = createScheduler();
    const worker = await createWorker();
    scheduler.addWorker(worker);
  3. How Tesseract.js manages trained data files

    master

    Tesseract.js caches *.traineddata files to avoid repeated downloads:

    • Browser: Uses IndexedDB.
    • Node.js: Uses the file system (fs) in the directory where the command is executed.

    When a language model is requested, Tesseract.js checks for the existing file. If not found, it fetches *.traineddata.gz from the tessdata repository, ungzips it, and stores it in the local cache. You can force a re-download by manually deleting the cached file.

  4. How Workers and Schedulers work together

    master

    Tesseract.js provides two distinct patterns for running recognition jobs depending on your performance needs:

    1. Direct Worker Usage: Best for single or sequential recognition tasks. You create a worker, use it to recognize an image, and then terminate it. For better performance, you should create the worker once and reuse it for multiple recognize calls rather than recreating it for every image.

    2. Scheduler Usage: Best for high-throughput parallel processing. A scheduler manages a pool of multiple workers and distributes jobs among them. While a single job won't run faster with a scheduler, a large batch of jobs will complete significantly faster because they are processed in parallel across the worker pool.

    Important Constraint: When using a scheduler, all workers added to it must be homogenous. They should use the same language and the same configuration parameters. Because the scheduler assigns jobs to workers non-deterministically, using heterogeneous workers will lead to inconsistent recognition results.

    // Concept: Single Worker (Sequential)
    const worker = await Tesseract.createWorker('eng');
    await worker.recognize(image);
    await worker.terminate();
    
    // Concept: Scheduler (Parallel)
    const scheduler = Tesseract.createScheduler();
    scheduler.addWorker(worker);
    await scheduler.addJob('recognize', image);
    await scheduler.terminate();
  5. Understand the scope and limitations of Tesseract.js

    master

    Tesseract.js is a WebAssembly port of the Tesseract OCR engine. It is designed to bring Tesseract capabilities to the browser and Node.js without modifying the core engine.

    Key Limitations:

    • No PDF Support: Tesseract.js does not support PDF files directly. If you need PDF extraction, consider using the Scribe.js library.
    • No Model Modifications: It does not modify the Tesseract recognition model to improve accuracy. It provides the engine as-is.
  6. Quickstart: Recognize text from an image

    master

    To perform basic Optical Character Recognition (OCR), use createWorker to initialize a worker with a specific language, then call worker.recognize with the image source (URL, path, or buffer).

    Best Practice: When processing multiple images, create the worker once, run worker.recognize for each image, and call worker.terminate() only once at the end to avoid the overhead of repeatedly initializing the engine.

    import { createWorker } from 'tesseract.js';
    
    (async () => {
      const worker = await createWorker('eng');
      const ret = await worker.recognize('https://tesseract.projectnaptha.com/img/eng_bw.png');
      console.log(ret.data.text);
      await worker.terminate();
    })();
  7. Use Tesseract.js via CDN

    master

    You can include Tesseract.js in the browser using a <script> tag. After inclusion, the Tesseract variable is available globally.

    Standard Script Tag (v5):

    <script src='https://cdn.jsdelivr.net/npm/tesseract.js@5/dist/tesseract.min.js'></script>

    ESM Build (for import syntax): Use the ESM build at: https://cdn.jsdelivr.net/npm/tesseract.js@5/dist/tesseract.esm.min.js

  8. Run parallel recognition jobs using a Scheduler

    master

    To execute multiple recognition jobs in parallel, use Tesseract.createScheduler(). You must first create workers and add them to the scheduler using scheduler.addWorker(worker). Once the workers are ready, you can dispatch jobs using scheduler.addJob(method, input).

    Calling scheduler.terminate() will automatically terminate all workers contained within that scheduler.

    const scheduler = Tesseract.createScheduler();
    
    // Creates worker and adds to scheduler
    const workerGen = async () => {
      const worker = await Tesseract.createWorker('eng');
      scheduler.addWorker(worker);
    }
    
    const workerN = 4;
    (async () => {
      const resArr = Array(workerN);
      for (let i=0; i<workerN; i++) {
        resArr[i] = workerGen();
      }
      await Promise.all(resArr);
      /** Add 10 recognition jobs */
      const results = await Promise.all(Array(10).fill(0).map(() => (
        scheduler.addJob('recognize', 'https://tesseract.projectnaptha.com/img/eng_bw.png').then((x) => console.log(x.data.text))
      )))
      await scheduler.terminate(); // It also terminates all workers.
    })();
  9. Perform a local installation of Tesseract.js

    master

    While loading tesseract.js from a CDN is recommended, you can host all files locally by passing custom paths to the createWorker function. This is useful for offline environments or specific deployment requirements.

    In a browser environment, you can customize workerPath, langPath, and corePath.

    In a Node.js environment, you typically only need to customize langPath.

    const worker = await createWorker('eng', 1, {
      workerPath: 'https://cdn.jsdelivr.net/npm/tesseract.js@v5.0.0/dist/worker.min.js',
      langPath: 'https://tessdata.projectnaptha.com/4.0.0',
      corePath: 'https://cdn.jsdelivr.net/npm/tesseract.js-core@v5.0.0',
    });
  10. Install Tesseract.js via npm or yarn

    master

    Tesseract.js v7 requires Node.js v16 or newer. Use the following commands to install the latest version or a specific older version.

    Latest version:

    npm install tesseract.js
    yarn add tesseract.js

    Specific version (e.g., v3.0.3):

    npm install tesseract.js@3.0.3
    yarn add tesseract.js@3.0.3
    npm install tesseract.js
  11. Reduce setup time by pre-loading workers

    master

    To minimize the perceived latency when a user first triggers OCR, initialize your Tesseract.js workers and download necessary data ahead of time.

    For example, in a web application where OCR is an infrequent feature, you should avoid downloading the ~15MB of code and data on initial page load. Instead, trigger the worker setup when the user indicates they intend to use OCR, but before they actually select an image to process.

  12. How to process PDF files with Tesseract.js

    master

    Tesseract.js does not support PDF files directly. To perform OCR on a PDF, you have two primary options:

    1. Use Scribe.js: A library built on top of Tesseract.js that includes native PDF support and can extract text directly from text-native PDFs (which is faster and more accurate than OCR).
    2. Render PDFs to Images: Use a third-party library to convert .pdf pages into .png images, then pass those images to Tesseract.js. Recommended libraries include:
      • PDF.js (Apache-2.0 license)
      • muPDF (AGPL-3.0 license)