Pure Javascript OCR for more than 100 Languages 📖🎉🖥
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
9.8 KiB

# [Tesseract.js](http://tesseract.projectnaptha.com/)
8 years ago
8 years ago
[![npm version](https://badge.fury.io/js/tesseract.js.svg)](https://badge.fury.io/js/tesseract.js)
8 years ago
Tesseract.js is a javascript library that gets words in [almost any language](./tesseract_lang_list.md) out of images. ([Demo](http://tesseract.projectnaptha.com/))
8 years ago
8 years ago
<!-- Under the hood, Tesseract.js wraps [tesseract.js-core](https://github.com/naptha/tesseract.js-core), an [emscripten](https://github.com/kripken/emscripten) port of the [Tesseract OCR Engine](https://github.com/tesseract-ocr/tesseract).
-->
8 years ago
[![fancy demo gif](./demo.gif "Demo")](http://tesseract.projectnaptha.com)
8 years ago
8 years ago
Tesseract.js works with script tags, webpack/browserify, and node. [After you install it](#installation), using it is as simple as
8 years ago
```javascript
8 years ago
Tesseract.recognize(myImage)
8 years ago
.progress(function (p) { console.log('progress', p) })
.then(function (result) { console.log('result', result) })
8 years ago
```
8 years ago
[Check out the docs](#docs) for a full treatment of the API.
8 years ago
8 years ago
# Installation
8 years ago
Tesseract.js works with a `<script>` tag via local copy or cdn, with webpack and browserify via `npm`, and on node via `npm`. [Check out the docs](#docs) for a full treatment of the API.
8 years ago
8 years ago
## &lt;script />
8 years ago
You can simply include Tesseract.js with a cdn like this:
```html
8 years ago
<script src='https://cdn.rawgit.com/naptha/tesseract.js/0.2.0/dist/tesseract.js'></script>
```
8 years ago
After including your scripts, the `Tesseract` variable should be defined! You can [head to the docs](#docs) for a full treatment of the API.
## npm
8 years ago
First:
```shell
> npm install tesseract.js --save
```
Then
```javascript
var Tesseract = require('tesseract.js')
```
or
```javascript
import Tesseract from 'tesseract.js'
```
You can [head to the docs](#docs) for a full treatment of the API.
8 years ago
# Docs
* [Tesseract.recognize(image: ImageLike[, options]) -> [TesseractJob](#tesseractjob)](#tesseractrecognizeimage-imagelike-options---tesseractjob)
8 years ago
+ [Simple Example](#simple-example)
+ [More Complicated Example](#more-complicated-example)
8 years ago
* [Tesseract.detect(image: ImageLike) -> [TesseractJob](#tesseractjob)](#tesseractdetectimage-imagelike---tesseractjob)
* [ImageLike](#imagelike)
* [TesseractJob](#tesseractjob)
+ [TesseractJob.progress(callback: function) -> TesseractJob](#tesseractjobprogresscallback-function---tesseractjob)
+ [TesseractJob.then(callback: function) -> TesseractJob](#tesseractjobthencallback-function---tesseractjob)
8 years ago
+ [TesseractJob.catch(callback: function) -> TesseractJob](#tesseractjoberrorcallback-function---tesseractjob)
8 years ago
* [Tesseract Configuration](#tesseract-configuration)
+ [corePath](#corepath)
+ [workerPath](#workerpath)
+ [langPath](#langpath)
8 years ago
* [Contributing](#contributing)
+ [Development](#development)
+ [Building Static Files](#building-static-files)
+ [Send us a Pull Request!](#send-us-a-pull-request)
8 years ago
## Tesseract.recognize(image: [ImageLike](#imagelike)[, options]) -> [TesseractJob](#tesseractjob)
Figures out what words are in `image`, where the words are in `image`, etc.
- `image` is any [ImageLike](#imagelike) object.
8 years ago
- `options` is either absent (in which case it is interpreted as `'eng'`), a string specifing a language short code from the [language list](./tesseract_lang_list.md), or a flat json object that may:
+ include properties that override some subset of the [default tesseract parameters](./tesseract_parameters.md)
+ include a `lang` property with a value from the [list of lang parameters](./tesseract_lang_list.md)
8 years ago
Returns a [TesseractJob](#tesseractjob) whose `then`, `progress`, and `catch` methods can be used to act on the result.
### Simple Example:
```javascript
8 years ago
Tesseract.recognize(myImage)
.then(function(result){
console.log(result)
})
```
### More Complicated Example:
```javascript
// if we know our image is of spanish words without the letter 'e':
8 years ago
Tesseract.recognize(myImage, {
lang: 'spa',
tessedit_char_blacklist: 'e'
})
.then(function(result){
console.log(result)
})
```
## Tesseract.detect(image: [ImageLike](#imagelike)) -> [TesseractJob](#tesseractjob)
8 years ago
Figures out what script (e.g. 'Latin', 'Chinese') the words in image are written in.
8 years ago
- `image` is any [ImageLike](#imagelike) object.
Returns a [TesseractJob](#tesseractjob) whose `then`, `progress`, and `error` methods can be used to act on the result of the script.
```javascript
8 years ago
Tesseract.detect(myImage)
.then(function(result){
console.log(result)
})
```
8 years ago
## ImageLike
8 years ago
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`, or `canvas` element
8 years ago
- a CanvasRenderingContext2D (returned by `canvas.getContext('2d')`)
8 years ago
- a `File` object (from a file `<input>` or drag-drop event)
- a `Blob` object
- a `ImageData` instance (an object containing `width`, `height` and `data` properties)
- a path or URL to an accessible image (the image must either be hosted locally or accessible by CORS)
In NodeJS, an image can be
- a path to a local image
- a `Buffer` instance containing a `PNG` or `JPEG` image
- a `ImageData` instance (an object containing `width`, `height` and `data` properties)
8 years ago
## TesseractJob
8 years ago
A TesseractJob is an an object returned by a call to `recognize` or `detect`. It's inspired by the ES6 Promise interface and provides `then` and `catch` methods. One important difference is that these methods return the job itself (to enable chaining) rather than new.
Typical use is:
```javascript
8 years ago
Tesseract.recognize(myImage)
.progress(function(message){console.log(message)})
8 years ago
.catch(function(err){console.error(err)})
.then(function(result){console.log(result)})
```
Which is equivalent to:
```javascript
8 years ago
var job1 = Tesseract.recognize(myImage);
job1.progress(function(message){console.log(message)});
8 years ago
job1.catch(function(err){console.error(err)});
job1.then(function(result){console.log(result)})
```
### TesseractJob.progress(callback: function) -> TesseractJob
Sets `callback` as the function that will be called every time the job progresses.
- `callback` is a function with the signature `callback(progress)` where `progress` is a json object.
For example:
```javascript
8 years ago
Tesseract.recognize(myImage)
8 years ago
.progress(function(message){console.log('progress is: ', message)})
```
The console will show something like:
```javascript
progress is: {loaded_lang_model: "eng", from_cache: true}
progress is: {initialized_with_lang: "eng"}
progress is: {set_variable: Object}
progress is: {set_variable: Object}
progress is: {recognized: 0}
progress is: {recognized: 0.3}
progress is: {recognized: 0.6}
progress is: {recognized: 0.9}
progress is: {recognized: 1}
```
### TesseractJob.then(callback: function) -> TesseractJob
Sets `callback` as the function that will be called if and when the job successfully completes.
- `callback` is a function with the signature `callback(result)` where `result` is a json object.
For example:
```javascript
8 years ago
Tesseract.recognize(myImage)
.then(function(result){console.log('result is: 'result)})
```
The console will show something like:
```javascript
progress is: {
blocks: Array[1]
confidence: 87
html: "<div class='ocr_page' id='page_1' ..."
lines: Array[3]
oem: "DEFAULT"
paragraphs: Array[1]
psm: "SINGLE_BLOCK"
symbols: Array[33]
text: "Hello World↵from beyond↵the Cosmic Void↵↵"
version: "3.04.00"
words: Array[7]
}
```
8 years ago
### TesseractJob.catch(callback: function) -> TesseractJob
Sets `callback` as the function that will be called if the job fails.
- `callback` is a function with the signature `callback(erros)` where `error` is a json object.
8 years ago
## Tesseract Configuration
8 years ago
```javascript
8 years ago
window.Tesseract = Tesseract.create({
workerPath: '/path/to/worker.js',
langPath: 'https://cdn.rawgit.com/naptha/tessdata/gh-pages/3.02/',
corePath: 'https://cdn.rawgit.com/naptha/tesseract.js-core/0.1.0/index.js',
})
8 years ago
```
8 years ago
### corePath
A string specifying the location of the [tesseract.js-core library](https://github.com/naptha/tesseract.js-core), with default value 'https://cdn.rawgit.com/naptha/tesseract.js-core/master/index.js'. Set this string before calling `Tesseract.recognize` and `Tesseract.detect` if you want Tesseract.js to use a different file.
8 years ago
8 years ago
### workerPath
A string specifying the location of the [tesseract.worker.js](./dist/tesseract.worker.js) file, with default value 'https://cdn.rawgit.com/naptha/tesseract.js/8b915dc/dist/tesseract.worker.js'. Set this string before calling `Tesseract.recognize` and `Tesseract.detect` if you want Tesseract.js to use a different file.
8 years ago
8 years ago
### langPath
8 years ago
A string specifying the location of the tesseract language files, with default value 'https://cdn.rawgit.com/naptha/tessdata/gh-pages/3.02/'. Language file urls are calculated according to the formula `langPath + langCode + '.traineddata.gz'`. Set this string before calling `Tesseract.recognize` and `Tesseract.detect` if you want Tesseract.js to use different language files.
8 years ago
8 years ago
## Contributing
### Development
To run a development copy of tesseract.js, first clone this repo.
```shell
> git clone https://github.com/naptha/tesseract.js.git
```
Then, cd in to the folder, `npm install`, and `npm start`
8 years ago
```shell
> cd tesseract.js
> npm install && npm start
8 years ago
... a bunch of npm stuff ...
8 years ago
Starting up http-server, serving ./
Available on:
http://127.0.0.1:7355
http://[your ip]:7355
```
8 years ago
Then open `http://localhost:7355/examples/file-input/demo.html` in your favorite browser. The devServer automatically rebuilds tesseract.js and tesseract.worker.js when you change files in the src folder.
8 years ago
### Building Static Files
After you've cloned the repo and run `npm install` as described in the [Development Section](#development), you can build static library files in the dist folder with
```shell
> npm run build
```
8 years ago
### Send us a Pull Request!
8 years ago
Thanks :)