Tesseract.js is a javascript library that gets words in almost any language out of images. (Demo)
Tesseract.js works with script tags, webpack/Browserify, and Node.js. After you install it, using it is as simple as
Tesseract.recognize(myImage)
.progress(function (p) { console.log('progress', p) })
.then(function (result) { console.log('result', result) })
Check out the docs for a full treatment of the API.
Tesseract.js wraps an emscripten port of the Tesseract OCR Engine.
Tesseract.js works with a <script>
tag via local copy or CDN, with webpack and Browserify via npm
, and on Node.js via npm
. Check out the docs for a full treatment of the API.
You can simply include Tesseract.js with a CDN like this:
<script src='https://cdn.rawgit.com/naptha/tesseract.js/1.0.10/dist/tesseract.js'></script>
After including your scripts, the Tesseract
variable will be defined globally!
First:
> yarn add tesseract.js
or
> npm install tesseract.js --save
Note: Tesseract.js currently requires Node.js v6.8.0 or higher.
var Tesseract = require('tesseract.js')
or
import Tesseract from 'tesseract.js'
Tesseract.recognize(image: ImageLike[, options]) -> TesseractJob
Figures out what words are in image
, where the words are in image
, etc.
Note:
image
should be sufficiently high resolution. Often, the same image will get much better results if you upscale it before callingrecognize
.
image
is any ImageLike object.options
is either absent (in which case it is interpreted as'eng'
), a string specifing a language short code from the language list, or a flat json object that may:- include properties that override some subset of the default tesseract parameters
- include a
lang
property with a value from the list of lang parameters
Returns a TesseractJob whose then
, progress
, catch
and finally
methods can be used to act on the result.
Tesseract.recognize(myImage)
.then(function(result){
console.log(result)
})
// if we know our image is of spanish words without the letter 'e':
Tesseract.recognize(myImage, {
lang: 'spa',
tessedit_char_blacklist: 'e'
})
.then(function(result){
console.log(result)
})
Tesseract.detect(image: ImageLike) -> TesseractJob
Figures out what script (e.g. 'Latin', 'Chinese') the words in image are written in.
image
is any ImageLike object.
Returns a TesseractJob whose then
, progress
, catch
and finally
methods can be used to act on the result of the script.
Tesseract.detect(myImage)
.then(function(result){
console.log(result)
})
The main Tesseract.js functions take an image
parameter, which should be something that is like an image. What's considered "image-like" differs depending on whether it is being run from the browser or through NodeJS.
On a browser, an image can be:
- an
img
,video
, orcanvas
element - a CanvasRenderingContext2D (returned by
canvas.getContext('2d')
) - a
File
object (from a file<input>
or drag-drop event) - a
Blob
object - a
ImageData
instance (an object containingwidth
,height
anddata
properties) - a path or URL to an accessible image (the image must either be hosted locally or accessible by CORS)
In Node.js, an image can be
- a path to a local image
- a
Buffer
instance containing aPNG
orJPEG
image - a
ImageData
instance (an object containingwidth
,height
anddata
properties)
A TesseractJob is an object returned by a call to recognize
or detect
. It's inspired by the ES6 Promise interface and provides then
and catch
methods. It also provides finally
method, which will be fired regardless of the job fate. One important difference is that these methods return the job itself (to enable chaining) rather than new.
Typical use is:
Tesseract.recognize(myImage)
.progress(message => console.log(message))
.catch(err => console.error(err))
.then(result => console.log(result))
.finally(resultOrError => console.log(resultOrError))
Which is equivalent to:
< 8000 div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="var job1 = Tesseract.recognize(myImage); job1.progress(message => console.log(message)); job1.catch(err => console.error(err)); job1.then(result => console.log(result)); job1.finally(resultOrError => console.log(resultOrError));">var job1 = Tesseract.recognize(myImage); job1.progress(message => console.log(message)); job1.catch(err => console.error(err)); job1.then(result => console.log(result)); job1.finally(resultOrError => console.log(resultOrError));