• Stars
    star
    110
  • Rank 306,868 (Top 7 %)
  • Language
    TypeScript
  • License
    BSD 2-Clause "Sim...
  • Created about 2 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

JS/WebAssembly build of the Tesseract OCR engine for use in browsers and Node

tesseract-wasm

npm package

A WebAssembly build of the Tesseract OCR engine for use in the browser and Node.

tesseract-wasm can detect and recognize text in document images. It supports multiple languages via different trained models.

πŸ‘‰ Try the demo (Currently supports English)

Features

This Tesseract build has been optimized for use in the browser by:

  • Stripping functionality which is not needed in a browser environment (eg. code to parse various image formats) to reduce download size and improve startup performance. The library and English training data require a ~2.1MB download (with Brotli compression).

  • Using WebAssembly SIMD when available (Chrome >= 91, Firefox >= 90, Safari >= 16.4) to improve text recognition performance.

  • Providing a high-level API that can be used to run web pages without blocking interaction and a low-level API that provides more control over execution.

Setup

  1. Add the tesseract-wasm library to your project:

    npm install tesseract-wasm
  2. Serve the tesseract-core.wasm, tesseract-core-fallback.wasm and tesseract-worker.js files from node_modules/tesseract-wasm/dist alongside your JavaScript bundle.

  3. Get the training data file(s) for the languages you want to support from the tessdata_fast repo and serve it from a URL that your JavaScript can load. The eng.traineddata file supports English for example, and also works with many documents in other languages that use the same script.

Usage

tesseract-wasm provides two APIs: a high-level asynchronous API (OCRClient) and a lower-level synchronous API (OCREngine). The high-level API is the most convenient way to run OCR on an image in a web page. It handles running the OCR engine inside a Web Worker to avoid blocking page interaction. The low-level API is useful if more control is needed over where/how the code runs and has lower latency per API call.

Using OCRClient in a web page

import { OCRClient } from 'tesseract-wasm';

async function runOCR() {
  // Fetch document image and decode it into an ImageBitmap.
  const imageResponse = await fetch('./test-image.jpg');
  const imageBlob = await imageResponse.blob();
  const image = await createImageBitmap(image);

  // Initialize the OCR engine. This will start a Web Worker to do the
  // work in the background.
  const ocr = new OCRClient();

  try {
    // Load the appropriate OCR training data for the image(s) we want to
    // process.
    await ocr.loadModel('eng.traineddata');

    await ocr.loadImage(someImage);

    // Perform text recognition and return text in reading order.
    const text = await ocr.getText();

    console.log('OCR text: ', text);
  } finally {
    // Once all OCR-ing has been done, shut down the Web Worker and free up
    // resources.
    ocr.destroy();
  }
}

runOCR();

Examples and documentation

See the examples/ directory for projects that show usage of the library in the browser and Node.

See the API documentation for detailed usage information.

See the Tesseract User Manual for information on how Tesseract works, as well as advice on improving recognition.

Development

Prerequisites

To build this library locally, you will need:

  • A C++ build toolchain (eg. via the build-essential package on Ubuntu or Xcode on macOS)
  • CMake
  • Ninja

The Emscripten toolchain used to compile C++ to WebAssembly is downloaded as part of the build process.

To install CMake and Ninja:

On macOS:

brew install cmake ninja

On Ubuntu

sudo apt-get install cmake ninja-build

Building the library

git clone https://github.com/robertknight/tesseract-wasm
cd tesseract-wasm

# Build WebAssembly binaries and JS library in dist/ folder
make lib

# Run tests
make test

To test your local build of the library with the example projects, or your own projects, you can use yalc.

# In this project
yalc publish

# In the project where you want to use your local build of tesseract-wasm
yalc link tesseract-wasm

More Repositories

1

webpack-bundle-size-analyzer

A tool for finding out what contributes to the size of Webpack bundles
TypeScript
1,400
star
2

Qt-Inspector

Utility to browse the Qt object tree of a running Qt application and edit object properties on the fly.
C++
375
star
3

react-testing

Slides, notes and sample project from a talk on testing React applications given at the London React meetup group
JavaScript
188
star
4

passcards

A 1Password-compatible command-line and web-based password manager
TypeScript
137
star
5

tex-linebreak

JavaScript implementation of the Knuth-Plass linebreaking algorithm
TypeScript
110
star
6

qt-mustache

Mustache templating library for C++ using Qt
C++
85
star
7

1pass

1Password command-line client
Go
66
star
8

konsole

Terminal for KDE
C++
60
star
9

approx-string-match-js

Approximate string matching library for JavaScript
TypeScript
35
star
10

ts-style

A small library for defining CSS classes using JavaScript or TypeScript
TypeScript
33
star
11

qt-maybe

Implementation of sum/option types using QVariant
C++
29
star
12

qt-signal-tools

Utility classes related to Qt signal and slot handling
C++
24
star
13

rollup-cache

Disk caching to speed up Rollup builds
JavaScript
15
star
14

mandrawer

A miscellaneous collection of scripts I use across various devices.
Python
13
star
15

sass-unused

Utility for finding unused variables in a collection of SASS files
JavaScript
12
star
16

extension-tools

A collection of scripts for automated publishing of browser extensions
JavaScript
11
star
17

kdevelop

KDevelop IDE
C++
10
star
18

babel-plugin-mockable-imports

Babel plugin that enables mocking of ES and CommonJS imports in tests
JavaScript
10
star
19

idb-mutex

Mutual exclusion (locks) between tabs in browsers using IndexedDB
TypeScript
9
star
20

xar-js

A JavaScript library for generating xar archives
TypeScript
8
star
21

dbpro-bluegui

BlueGUI v2 GUI controls plugin for DarkBASIC Professional
C++
7
star
22

rust-optparse

Command-line option parsing lib for Rust
Rust
3
star
23

duckface

Go-like interface generator for C++
Ruby
2
star
24

ureact

A small implementation of the modern React API for educational purposes.
JavaScript
2
star
25

unroll-it

Helper for creating data-driven / parametrized tests with Mocha
JavaScript
2
star
26

textgen

Random text generator built with PyTorch
Python
2
star
27

hot-reloading-talk

A talk for LondonJS on architecting web applications to enable hot reloading
JavaScript
2
star
28

elm-arch-tutorial

Notes made whilst working through the Elm Architecture Tutorial
Elm
1
star
29

talks

Slides and resources for talks I've given or will give
JavaScript
1
star
30

find-unused-css-classes

Tool for finding CSS classes that are styled by selectors in a set of CSS files but not used
JavaScript
1
star
31

rd

A tool for quick access to recently used directories
Go
1
star
32

elm-april-hack-night

Elm
1
star
33

qt-webitemview

Qt item view delegate which uses WebKit to render item content
C++
1
star
34

rust-cmacros

Rust lib for parsing macros from C header files and assisting with translation to Rust code
Rust
1
star
35

labnotes

A river-of-news feed aggregator. Created as a Mendeley hackday project.
TypeScript
1
star
36

used-css-classes

Tool that prints the list of unique CSS classes used in a set of HTML files or templates
Python
1
star
37

gomendeley

A basic demo showing authentication and use of the Mendeley API with Go
Go
1
star
38

prune-merged-branches

Tool to clean up your local Git branches
Python
1
star