createWorker is a factory function that creates a tesseract worker, a worker is basically a Web Worker in browser and Child Process in Node.
Arguments:
optionsan object of customized optionscorePathpath for tesseract-core.js scriptlangPathpath for downloading traineddata, do not include/at the end of the pathworkerPathpath for downloading worker scriptdataPathpath for saving traineddata in WebAssembly file system, not common to modifycachePathpath for the cached traineddata, more useful for Node, for browser it only changes the key in IndexDBcacheMethoda string to indicate the method of cache management, should be one of the following options- write: read cache and write back (default method)
- readOnly: read cache and not to write back
- refresh: not to read cache and write back
- none: not to read cache and not to write back
workerBlobURLa boolean to define whether to use Blob URL for worker script, default: truegzipa boolean to define whether the traineddata from the remote is gzipped, default: trueloggera function to log the progress, a quick example ism => console.log(m)errorHandlera function to handle worker errors, a quick example iserr => console.error(err)
Examples:
const { createWorker } = Tesseract;
const worker = createWorker({
langPath: '...',
logger: m => console.log(m),
});A Worker helps you to do the OCR related tasks, it takes few steps to setup Worker before it is fully functional. The full flow is:
- load
- FS functions // optional
- loadLanguauge
- initialize
- setParameters // optional
- recognize or detect
- terminate
Each function is async, so using async/await or Promise is required. When it is resolved, you get an object:
{
"jobId": "Job-1-123",
"data": { ... }
}jobId is generated by Tesseract.js, but you can put your own when calling any of the function above.
Worker.load() loads tesseract.js-core scripts (download from remote if not presented), it makes Web Worker/Child Process ready for next action.
Arguments:
jobIdPlease see details above
Examples:
(async () => {
await worker.load();
})();Worker.writeText() writes a text file to the path specified in MEMFS, it is useful when you want to use some features that requires tesseract.js to read file from file system.
Arguments:
pathtext file pathtextcontent of the text filejobIdPlease see details above
Examples:
(async () => {
await worker.writeText('tmp.txt', 'Hi\nTesseract.js\n');
})();Worker.readText() reads a text file to the path specified in MEMFS, it is useful when you want to check the content.
Arguments:
pathtext file pathjobIdPlease see details above
Examples:
(async () => {
const { data } = await worker.readText('tmp.txt');
console.log(data);
})();Worker.readFile() remove a file in MEMFS, it is useful when you want to free the memory.
Arguments:
pathfile pathjobIdPlease see details above
Examples:
(async () => {
await worker.removeFile('tmp.txt');
})();Worker.FS() is a generic FS function to do anything you want, you can check HERE for all functions.
Arguments:
methodmethod nameargsarray of arguments to passjobIdPlease see details above
Examples:
(async () => {
await worker.FS('writeFile', ['tmp.txt', 'Hi\nTesseract.js\n']);
// equal to:
// await worker.readText('tmp.txt', 'Hi\nTesseract.js\n');
})();Worker.loadLanguage() loads traineddata from cache or download traineddata from remote, and put traineddata into the WebAssembly file system.
Arguments:
langsa string to indicate the languages traineddata to download, multiple languages are concated with +, ex: eng+chi_trajobIdPlease see details above
Examples:
(async () => {
await worker.loadLanguage('eng+chi_tra');
})();Worker.initialize() initializes the Tesseract API, make sure it is ready for doing OCR tasks.
Arguments:
langsa string to indicate the languages loaded by Tesseract API, it can be the subset of the languauge traineddata you loaded from Worker.loadLanguage.oema enum to indicate the OCR Engine Mode you usejobIdPlease see details above
Examples:
(async () => {
/** You can load more languages in advance, but use only part of them in Worker.initialize() */
await worker.loadLanguage('eng+chi_tra');
await worker.initialize('eng');
})();Worker.setParameters() set parameters for Tesseract API (using SetVariable()), it changes the behavior of Tesseract and some parameters like tessedit_char_whitelist is very useful.
Arguments:
paramsan object with key and value of the parametersjobIdPlease see details above
Supported Paramters:
| name | type | default value | description |
|---|---|---|---|
| tessedit_ocr_engine_mode | enum | OEM.DEFAULT | Check HERE for definition of each mode |
| tessedit_pageseg_mode | enum | PSM.SINGLE_BLOCK | Check HERE for definition of each mode |
| tessedit_char_whitelist | string | '' | setting white list characters makes the result only contains these characters, useful the content in image is limited |
| preserve_interword_spaces | string | '0' | '0' or '1', keeps the space between words |
| user_defined_dpi | string | '' | Define custom dpi, use to fix Warning: Invalid resolution 0 dpi. Using 70 instead. |
| tessjs_create_hocr | string | '1' | only 2 values, '0' or '1', when the value is '1', tesseract.js includes hocr in the result |
| tessjs_create_tsv | string | '1' | only 2 values, '0' or '1', when the value is '1', tesseract.js includes tsv in the result |
| tessjs_create_box | string | '0' | only 2 values, '0' or '1', when the value is '1', tesseract.js includes box in the result |
| tessjs_create_unlv | string | '0' | only 2 values, '0' or '1', when the value is '1', tesseract.js includes unlv in the result |
| tessjs_create_osd | string | '0' | only 2 values, '0' or '1', when the value is '1', tesseract.js includes osd in the result |
Examples:
(async () => {
await worker.setParameters({
tessedit_char_whitelist: '0123456789',
});
})Worker.recognize() provides core function of Tesseract.js as it executes OCR
Figures out what words are in image, where the words are in image, etc.
Note:
imageshould be sufficiently high resolution. Often, the same image will get much better results if you upscale it before callingrecognize.
Arguments:
imagesee Image Format for more details.optionsa object of customized optionsrectanglean object to specify the regions you want to recognized in the image, should contain top, left, width and height, see example below.
jobIdPlease see details above
Output:
Examples:
const { createWorker } = Tesseract;
(async () => {
const worker = createWorker();
await worker.load();
await worker.loadLanguage('eng');
await worker.initialize('eng');
const { data: { text } } = await worker.recognize(image);
console.log(text);
})();With rectangle
const { createWorker } = Tesseract;
(async () => {
const worker = createWorker();
await worker.load();
await worker.loadLanguage('eng');
await worker.initialize('eng');
const { data: { text } } = await worker.recognize(image, {
rectangle: { top: 0, left: 0, width: 100, height: 100 },
});
console.log(text);
})();Worker.detect() does OSD (Orientation and Script Detection) to the image instead of OCR.
Arguments:
imagesee Image Format for more details.jobIdPlease see details above
Examples:
const { createWorker } = Tesseract;
(async () => {
const worker = createWorker();
await worker.load();
await worker.loadLanguage('eng');
await worker.initialize('eng');
const { data } = await worker.detect(image);
console.log(data);
})();Worker.terminate() terminates the worker and cleans up
(async () => {
await worker.terminate();
})();createScheduler() is a factory function to create a scheduler, a scheduler manages a job queue and workers to enable multiple workers to work together, it is useful when you want to speed up your performance.
Examples:
const { createScheduler } = Tesseract;
const scheduler = createScheduler();Scheduler.addWorker() adds a worker into the worker pool inside scheduler, it is suggested to add one worker to only one scheduler.
Arguments:
workersee Worker above
Examples:
const { createWorker, createScheduler } = Tesseract;
const scheduler = createScheduler();
const worker = createWorker();
scheduler.addWorker(worker);Scheduler.addJob() adds a job to the job queue and scheduler waits and finds an idle worker to take the job.
Arguments:
actiona string to indicate the action you want to do, right now only recognize and detect are supportedpayloada arbitrary number of args depending on the action you called.
Examples:
(async () => {
const { data: { text } } = await scheduler.addJob('recognize', image, options);
const { data } = await scheduler.addJob('detect', image);
})();Scheduler.getNumWorkers() returns the length of job queue.
Scheduler.getNumWorkers() returns number of workers added into the scheduler
Scheduler.terminate() terminates all workers added, useful to do quick clean up.
Examples:
(async () => {
await scheduler.terminate();
})();setLogging() sets the logging flag, you can setLogging(true) to see detailed information, useful for debugging.
Arguments:
loggingboolean to define whether to see detailed logs, default: false
Examples:
const { setLogging } = Tesseract;
setLogging(true);recognize() is a function to quickly do recognize() task, it is not recommended to use in real application, but useful when you want to save some time.
See Tesseract.js
Same background as recognize(), but it does detect instead.
See Tesseract.js
See PSM.js
See OEM.js