diff --git a/package-lock.json b/package-lock.json index e39e07c..b344a7b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8537,9 +8537,9 @@ "integrity": "sha512-QmNgMA9m5ES5uMTqpOAPysrUA80vUx/6WKQlfkK3zhOeAgqv8DjwwcDv9tQv2TgRzOQ+LFKrJn94Y2rw5b2IGw==" }, "tesseract.js-utils": { - "version": "1.0.0-beta.7", - "resolved": "https://registry.npmjs.org/tesseract.js-utils/-/tesseract.js-utils-1.0.0-beta.7.tgz", - "integrity": "sha512-MvBQNxVoueDg/8iN8jb26Tnj0lBRfmHVMcnaNdab+JeQKg/SuNXWMCpTD4D17+S6zeE9AAlVQymDmXH2/3vaMg==", + "version": "1.0.0-beta.6", + "resolved": "https://registry.npmjs.org/tesseract.js-utils/-/tesseract.js-utils-1.0.0-beta.6.tgz", + "integrity": "sha512-AENYhkqafwysayWmKtyApV0gR4abLJ426plLNHs/++8oHt+ekooyp77ew/q4+QLE7cbUDyxiNGawcraOWE/RuQ==", "requires": { "axios": "^0.18.0", "bmp-js": "^0.1.0", diff --git a/package.json b/package.json index 1df7515..5474e33 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "src/index.js", "scripts": { "start": "node scripts/server.js", - "build": "webpack --progress --config scripts/webpack.config.prod.js", + "build": "webpack --config scripts/webpack.config.prod.js", "prepublishOnly": "npm run build", "wait": "wait-on http://localhost:3000/package.json", "test": "npm-run-all -p -r start test:all", @@ -54,7 +54,7 @@ "node-fetch": "^2.3.0", "resolve-url": "^0.2.1", "tesseract.js-core": "^2.0.0-beta.10", - "tesseract.js-utils": "^1.0.0-beta.7" + "tesseract.js-utils": "^1.0.0-beta.6" }, "repository": { "type": "git", diff --git a/scripts/webpack.config.common.js b/scripts/webpack.config.common.js index fabc7f6..a6dd7d5 100644 --- a/scripts/webpack.config.common.js +++ b/scripts/webpack.config.common.js @@ -4,7 +4,6 @@ module.exports = { { test: /\.m?js$/, // exclude: /(node_modules|bower_components)/, - exclude: /(tesseract.js-core)/, use: { loader: 'babel-loader', options: { diff --git a/scripts/webpack.config.dev.js b/scripts/webpack.config.dev.js index 92893b6..9efa913 100644 --- a/scripts/webpack.config.dev.js +++ b/scripts/webpack.config.dev.js @@ -3,10 +3,9 @@ const webpack = require('webpack'); const common = require('./webpack.config.common'); const genConfig = ({ - entry, filename, library, libraryTarget, ...config + entry, filename, library, libraryTarget, }) => ({ ...common, - ...config, mode: 'development', entry, output: { @@ -30,15 +29,10 @@ module.exports = [ library: 'Tesseract', libraryTarget: 'umd', }), - genConfig({ - entry: path.resolve(__dirname, '..', 'src', 'index.js'), - filename: 'tesseract.asm.dev.js', - library: 'Tesseract', - libraryTarget: 'umd', - resolve: { - alias: { - 'tesseract.js-core/tesseract-core.wasm.js': 'tesseract.js-core/tesseract-core.asm.js', - }, - }, - }), + /* + *genConfig({ + * entry: path.resolve(__dirname, '..', 'src', 'browser', 'worker.js'), + * filename: 'worker.dev.js', + *}), + */ ]; diff --git a/scripts/webpack.config.prod.js b/scripts/webpack.config.prod.js index 9f127c7..b088f3f 100644 --- a/scripts/webpack.config.prod.js +++ b/scripts/webpack.config.prod.js @@ -2,12 +2,11 @@ const path = require('path'); const common = require('./webpack.config.common'); const genConfig = ({ - entry, filename, library, libraryTarget, ...config + entry, filename, library, libraryTarget, }) => ({ ...common, - ...config, mode: 'production', - // devtool: 'source-map', + devtool: 'source-map', entry, output: { path: path.resolve(__dirname, '..', 'dist'), @@ -24,15 +23,10 @@ module.exports = [ library: 'Tesseract', libraryTarget: 'umd', }), - genConfig({ - entry: path.resolve(__dirname, '..', 'src', 'index.js'), - filename: 'tesseract.asm.min.js', - library: 'Tesseract', - libraryTarget: 'umd', - resolve: { - alias: { - 'tesseract.js-core/tesseract-core.wasm.js': 'tesseract.js-core/tesseract-core.asm.js', - }, - }, - }), + /* + *genConfig({ + * entry: path.resolve(__dirname, '..', 'src', 'browser', 'worker.js'), + * filename: 'worker.min.js', + *}), + */ ]; diff --git a/src/common/TesseractWorker.js b/src/common/TesseractWorker.js index e911c62..9c92afc 100644 --- a/src/common/TesseractWorker.js +++ b/src/common/TesseractWorker.js @@ -67,12 +67,12 @@ class TesseractWorker { * @function recognize text in given image * @access public * @param {Buffer, string} image - image to be recognized - * @param {string, array} [langs='eng'] - language to recognize + * @param {string} [lang=eng] - language to recognize * @param {object} params - tesseract parameters * */ - recognize(image, langs = 'eng', params = {}) { - return this._sendJob('recognize', image, langs, params); + recognize(image, lang = 'eng', params = {}) { + return this._sendJob('recognize', image, lang, params); } /** @@ -152,13 +152,13 @@ class TesseractWorker { * @param {string} lang language to recognize * @param {object} params tesseract parameters */ - _sendJob(type, image, langs, params) { + _sendJob(type, image, lang, params) { return this._delay((job) => { job.send( type, { image, - langs, + lang, params, options: this.options, }, diff --git a/src/common/workerUtils.js b/src/common/workerUtils.js index 80e051a..93f9bce 100644 --- a/src/common/workerUtils.js +++ b/src/common/workerUtils.js @@ -52,24 +52,16 @@ const setImage = (image) => { return data === null ? pix : data; }; -const getLangsStr = (langs) => { - if (typeof langs === 'string') { - return langs; - } - - return langs.map(lang => (typeof lang === 'string' ? lang : lang.code)).join('+'); -}; - /** * handleParams * * @name handleParams * @function hanlde params from users * @access private - * @param {string} langs - lang string for Init() + * @param {string} lang - lang string for Init() * @param {object} customParams - an object of params */ -const handleParams = (langs, customParams) => { +const handleParams = (lang, customParams) => { const { tessedit_ocr_engine_mode, ...params @@ -77,7 +69,7 @@ const handleParams = (langs, customParams) => { ...defaultParams, ...customParams, }; - api.Init(null, getLangsStr(langs), tessedit_ocr_engine_mode); + api.Init(null, lang, tessedit_ocr_engine_mode); Object.keys(params).forEach((key) => { api.SetVariable(key, params[key]); }); @@ -166,14 +158,14 @@ const handleInit = ({ corePath }, res) => { * @function load language from remote or local cache * @access public * @param {object} req - job payload - * @param {string} req.langs - languages to load, ex: eng, eng+chi_tra + * @param {string} req.lang - languages to load, ex: eng, eng+chi_tra * @param {object} req.options - other options for loadLang function * @param {object} res - job instance * @returns {Promise} A Promise for callback */ -const loadLanguage = ({ langs, options }, res) => { +const loadLanguage = ({ lang, options }, res) => { res.progress({ status: 'loading language traineddata', progress: 0 }); - return loadLang({ langs, TessModule, ...options }).then((...args) => { + return loadLang({ lang, TessModule, ...options }).then((...args) => { res.progress({ status: 'loaded language traineddata', progress: 1 }); return args; }); @@ -187,17 +179,17 @@ const loadLanguage = ({ langs, options }, res) => { * @access public * @param {object} req - job payload * @param {array} req.image - binary image in array format - * @param {string} req.langs - languages to load, ex: eng, eng+chi_tra + * @param {string} req.lang - languages to load, ex: eng, eng+chi_tra * @param {object} req.options - other options for loadLang function * @param {object} req.params - parameters for tesseract * @param {object} res - job instance */ const handleRecognize = ({ - image, langs, options, params, + image, lang, options, params, }, res) => ( handleInit(options, res) .then(() => ( - loadLanguage({ langs, options }, res) + loadLanguage({ lang, options }, res) .catch((e) => { if (e instanceof DOMException) { /* @@ -214,7 +206,7 @@ const handleRecognize = ({ res.progress({ status: 'initializing api', progress }); }; progressUpdate(0); - handleParams(langs, params); + handleParams(lang, params); progressUpdate(0.5); const ptr = setImage(image); progressUpdate(1); @@ -236,18 +228,18 @@ const handleRecognize = ({ * @access public * @param {object} req - job payload * @param {array} req.image - binary image in array format - * @param {string} req.langs - languages to load, ex: eng, eng+chi_tra + * @param {string} req.lang - languages to load, ex: eng, eng+chi_tra * @param {object} req.options - other options for loadLang function * @param {object} res - job instance */ const handleDetect = ({ - image, langs, options, + image, lang, options, }, res) => ( handleInit(options, res) .then(() => ( - loadLanguage({ langs, options }, res) + loadLanguage({ lang, options }, res) .then(() => { - api.Init(null, getLangsStr(langs)); + api.Init(null, lang); api.SetPageSegMode(TessModule.PSM_OSD_ONLY); const ptr = setImage(image); diff --git a/tests/detect.test.js b/tests/detect.test.js index c003106..d739505 100644 --- a/tests/detect.test.js +++ b/tests/detect.test.js @@ -1,4 +1,5 @@ -const { TesseractWorker } = Tesseract; +const { TesseractWorker, utils: { loadLang } } = Tesseract; +const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined'; const IMAGE_PATH = 'http://localhost:3000/tests/assets/images'; const loadLangOptions = { langPath: 'http://localhost:3000/tests/assets/traineddata', @@ -8,6 +9,7 @@ const loadLangOptions = { const getWorker = options => ( new TesseractWorker({ cacheMethod: 'readOnly', + ...(isBrowser ? { workerPath: 'http://localhost:3000/dist/worker.dev.js' } : {}), ...loadLangOptions, ...options, }) diff --git a/tests/recognize.test.js b/tests/recognize.test.js index 575572c..012532b 100644 --- a/tests/recognize.test.js +++ b/tests/recognize.test.js @@ -1,5 +1,6 @@ -const { TesseractWorker } = Tesseract; +const { TesseractWorker, utils: { loadLang } } = Tesseract; +const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined'; const IMAGE_PATH = 'http://localhost:3000/tests/assets/images'; const SIMPLE_TEXT = 'Tesseract.js\n'; const COMSIC_TEXT = 'HellO World\nfrom beyond\nthe Cosmic Void\n'; @@ -15,6 +16,7 @@ const loadLangOptions = { const getWorker = options => ( new TesseractWorker({ cacheMethod: 'readOnly', + ...(isBrowser ? { workerPath: 'http://localhost:3000/dist/worker.dev.js' } : {}), ...loadLangOptions, ...options, }) @@ -131,7 +133,7 @@ describe('recognize()', () => { }).timeout(10000) )); }); - + (isBrowser ? describe : describe.skip)('should read image from video DOM element (browser only)', () => { FORMATS.forEach(format => ( it(`support ${format} format`, (done) => { @@ -158,23 +160,22 @@ describe('recognize()', () => { let canvasDOM = null; let imageDOM = null; let idx = 0; - beforeEach((done) => { + beforeEach(function cb(done) { canvasDOM = document.createElement('canvas'); imageDOM = document.createElement('img'); imageDOM.setAttribute('crossOrigin', 'Anonymous'); imageDOM.onload = () => { canvasDOM.getContext('2d').drawImage(imageDOM, 0, 0); done(); - }; - imageDOM.setAttribute('src', `${IMAGE_PATH}/simple.${formats[idx]}`); - idx += 1; + } + imageDOM.setAttribute('src', `${IMAGE_PATH}/simple.${formats[idx++]}`); }); afterEach(() => { canvasDOM.remove(); imageDOM.remove(); }); - + formats.forEach(format => ( it(`support ${format} format`, (done) => { const worker = getWorker(); @@ -188,4 +189,5 @@ describe('recognize()', () => { }).timeout(10000) )); }); + });