What is tesseract.js?
tesseract.js is a JavaScript library that provides Optical Character Recognition (OCR) capabilities. It allows you to extract text from images and PDFs directly in the browser or in a Node.js environment.
What are tesseract.js's main functionalities?
Basic OCR
This feature allows you to perform basic OCR on an image file. The code sample demonstrates how to recognize text from an image using the English language.
const Tesseract = require('tesseract.js');
Tesseract.recognize(
'path/to/image.png',
'eng',
{
logger: m => console.log(m)
}
).then(({ data: { text } }) => {
console.log(text);
});
Handling Multiple Languages
This feature allows you to recognize text in multiple languages. The code sample demonstrates how to recognize text from an image using both English and Spanish languages.
const Tesseract = require('tesseract.js');
Tesseract.recognize(
'path/to/image.png',
'eng+spa',
{
logger: m => console.log(m)
}
).then(({ data: { text } }) => {
console.log(text);
});
Progress Reporting
This feature provides real-time progress updates during the OCR process. The code sample demonstrates how to log progress messages to the console.
const Tesseract = require('tesseract.js');
Tesseract.recognize(
'path/to/image.png',
'eng',
{
logger: m => console.log(m)
}
).then(({ data: { text } }) => {
console.log(text);
});
Using Worker for Performance
This feature allows you to use a worker for better performance, especially for large images or multiple OCR tasks. The code sample demonstrates how to create a worker, load the necessary language, perform OCR, and then terminate the worker.
const { createWorker } = require('tesseract.js');
const worker = createWorker({
logger: m => console.log(m)
});
(async () => {
await worker.load();
await worker.loadLanguage('eng');
await worker.initialize('eng');
const { data: { text } } = await worker.recognize('path/to/image.png');
console.log(text);
await worker.terminate();
})();
Other packages similar to tesseract.js
ocrad.js
ocrad.js is a JavaScript port of the OCRAD OCR library. It is designed to be used in the browser and can recognize text from images. Compared to tesseract.js, ocrad.js is lighter but may not be as accurate or feature-rich.
node-tesseract-ocr
node-tesseract-ocr is a Node.js wrapper for the Tesseract OCR engine. It provides a simpler interface for performing OCR in Node.js environments. While it offers similar functionalities to tesseract.js, it does not support browser usage.
Version 2 is now available and under development in the master branch, read a story about v2: Why I refactor tesseract.js v2?
Check the support/1.x branch for version 1
Tesseract.js is a javascript library that gets words in almost any language out of images. (Demo)
Image Recognition
Video Real-time Recognition
Tesseract.js wraps an emscripten port of the Tesseract OCR Engine.
It works in the browser using webpack or plain script tags with a CDN and on the server with Node.js.
After you install it, using it is as simple as:
import Tesseract from 'tesseract.js';
Tesseract.recognize(
'https://tesseract.projectnaptha.com/img/eng_bw.png',
'eng',
{ logger: m => console.log(m) }
).then(({ data: { text } }) => {
console.log(text);
})
Or more imperative
import { createWorker } from 'tesseract.js';
const worker = createWorker({
logger: m => console.log(m)
});
(async () => {
await worker.load();
await worker.loadLanguage('eng');
await worker.initialize('eng');
const { data: { text } } = await worker.recognize('https://tesseract.projectnaptha.com/img/eng_bw.png');
console.log(text);
await worker.terminate();
})();
Check out the docs for a full explanation of the API.
Major changes in v2
- Upgrade to tesseract v4.1 (using emscripten 1.38.45)
- Support multiple languages at the same time, eg: eng+chi_tra for English and Traditional Chinese
- Supported image formats: png, jpg, bmp, pbm
- Support WebAssembly (fallback to ASM.js when browser doesn't support)
- Support Typescript
Installation
Tesseract.js works with a <script>
tag via local copy or CDN, with webpack via npm
and on Node.js with npm/yarn
.
CDN
<script src='https://unpkg.com/tesseract.js@v2.0.1/dist/tesseract.min.js'></script>
<script src='https://unpkg.com/tesseract.js@1.0.19/src/index.js'></script>
After including the script the Tesseract
variable will be globally available.
Node.js
Tesseract.js currently requires Node.js v6.8.0 or higher
# For v2
npm install tesseract.js
yarn add tesseract.js
# For v1
npm install tesseract.js@1
yarn add tesseract.js@1
Documentation
Use tesseract.js the way you like!
Contributing
Development
To run a development copy of Tesseract.js do the following:
# First we clone the repository
git clone https://github.com/naptha/tesseract.js.git
cd tesseract.js
# Then we install the dependencies
npm install
# And finally we start the development server
npm start
The development server will be available at http://localhost:3000/examples/browser/demo.html in your favorite browser.
It will automatically rebuild tesseract.dev.js
and worker.dev.js
when you change files in the src folder.
Online Setup with a single Click
You can use Gitpod(A free online VS Code like IDE) for contributing. With a single click it will launch a ready to code workspace with the build & start scripts already in process and within a few seconeds it will spin up the dev server so that you can start contributing straight away without wasting any time.
Building Static Files
To build the compiled static files just execute the following:
npm run build
This will output the files into the dist
directory.
Contributors
Code Contributors
This project exists thanks to all the people who contribute. [Contribute].
Financial Contributors
Become a financial contributor and help us sustain our community. [Contribute]
Individuals
Organizations
Support this project with your organization. Your logo will show up here with a link to your website. [Contribute]