Browse Source

Add init_oem parameters in recognize() for switching modes

feature/aio
Jerome Wu 6 years ago
parent
commit
f1c9e2f08c
  1. 17
      src/common/types.js
  2. 8
      src/common/workerUtils.js
  3. 3
      src/index.js

17
src/common/types.js

@ -0,0 +1,17 @@ @@ -0,0 +1,17 @@
module.exports = {
/*
* OEM = OCR Engine Mode, and there are 5 possible modes.
*
* By default tesseract.js uses DEFAULT mode, which uses LSTM when possible.
* If you need to use some tesseract v3 features (like tessedit_chars_whitelist),
* you need to use TESSERACT_ONLY mode.
*
*/
OEM: {
TESSERACT_ONLY: 0,
LSTM_ONLY: 1,
TESSERACT_LSTM_COMBINED: 2,
DEFAULT: 3,
COUNT: 4,
},
};

8
src/common/workerUtils.js

@ -8,6 +8,7 @@ @@ -8,6 +8,7 @@
* @author Jerome Wu <jeromewus@gmail.com>
*/
const { readImage, loadLang } = require('tesseract.js-utils');
const check = require('check-types');
const dump = require('./dump');
/*
@ -122,13 +123,16 @@ const handleRecognize = ({ @@ -122,13 +123,16 @@ const handleRecognize = ({
.then(() => (
loadLanguage({ lang, options }, res)
.then(() => {
const OEM = check.undefined(params['init_oem'])
? TessModule.OEM_DEFAULT
: params['init_oem'];
const progressUpdate = (progress) => {
res.progress({ status: 'initializing api', progress });
};
progressUpdate(0);
api.Init(null, lang);
api.Init(null, lang, OEM);
progressUpdate(0.3);
Object.keys(params).forEach((key) => {
Object.keys(params).filter(key => !key.startsWith('init_')).forEach((key) => {
api.SetVariable(key, params[key]);
});
progressUpdate(0.6);

3
src/index.js

@ -9,10 +9,13 @@ @@ -9,10 +9,13 @@
*/
const utils = require('tesseract.js-utils');
const TesseractWorker = require('./common/TesseractWorker');
const { OEM } = require('./common/types');
module.exports = {
/** Worker for OCR, @see common/TesseractWorker.js */
TesseractWorker,
/** Utilities for tesseract.js, @see {@link https://www.npmjs.com/package/tesseract.js-utils} */
utils,
/** Check ./common/types for more details */
OEM,
};

Loading…
Cancel
Save