From 3b2b8bd2b82bdf063fbcbfc4ffa50b585b800df5 Mon Sep 17 00:00:00 2001 From: Jerome Wu Date: Fri, 23 Nov 2018 17:17:53 +0800 Subject: [PATCH] Minor refactor --- src/browser/index.js | 18 +++----- src/browser/worker.js | 5 ++- src/common/worker.js | 100 ++++++++++++++++++++++-------------------- src/node/index.js | 20 ++++----- src/node/worker.js | 6 +-- 5 files changed, 74 insertions(+), 75 deletions(-) diff --git a/src/browser/index.js b/src/browser/index.js index ef6e2ce..a916914 100644 --- a/src/browser/index.js +++ b/src/browser/index.js @@ -28,13 +28,13 @@ exports.defaultOptions = { corePath: `https://cdn.jsdelivr.net/gh/naptha/tesseract.js-core@v2.0.0-beta.5/tesseract-core${typeof WebAssembly === 'object' ? '' : '.asm'}.js`, }; -exports.spawnWorker = (instance, workerOptions) => { +exports.spawnWorker = (instance, { workerPath }) => { let worker; if (window.Blob && window.URL) { - const blob = new Blob([`importScripts("${workerOptions.workerPath}");`]); + const blob = new Blob([`importScripts("${resolveURL(workerPath)}");`]); worker = new Worker(window.URL.createObjectURL(blob)); } else { - worker = new Worker(workerOptions.workerPath); + worker = new Worker(workerPath); } worker.onmessage = ({ data }) => { @@ -48,16 +48,12 @@ exports.terminateWorker = (instance) => { instance.worker.terminate(); }; -exports.sendPacket = (instance, packet) => { +exports.sendPacket = (instance, iPacket) => { + const packet = { ...iPacket }; loadImage(packet.payload.image) .then(buf => new Uint8Array(buf)) .then((img) => { - instance.worker.postMessage({ - ...packet, - payload: { - ...packet.payload, - image: Array.from(img), - }, - }); + packet.payload.image = Array.from(img); + instance.worker.postMessage(packet); }); }; diff --git a/src/browser/worker.js b/src/browser/worker.js index 77e4a69..28e5933 100644 --- a/src/browser/worker.js +++ b/src/browser/worker.js @@ -1,3 +1,4 @@ +const resolveURL = require('resolve-url'); const workerUtils = require('../common/worker'); global.addEventListener('message', ({ data }) => { @@ -5,10 +6,10 @@ global.addEventListener('message', ({ data }) => { }); workerUtils.setAdapter({ - getCore: (req, res) => { + getCore: (corePath, res) => { if (!global.TesseractCore) { res.progress({ status: 'loading tesseract core', progress: 0 }); - global.importScripts(req.workerOptions.corePath); + global.importScripts(resolveURL(corePath)); global.TesseractCore = typeof WebAssembly === 'object' ? global.TesseractCoreWASM : global.TesseractCoreASM; res.progress({ status: 'loading tesseract core', progress: 1 }); } diff --git a/src/common/worker.js b/src/common/worker.js index 30b446f..df6aff9 100644 --- a/src/common/worker.js +++ b/src/common/worker.js @@ -1,28 +1,28 @@ const { readImage, loadLang } = require('tesseract.js-utils'); const dump = require('./dump'); -let Module; -let base; +let TessModule; +let api; let latestJob; let adapter = {}; const setImage = (image) => { const { w, h, bytesPerPixel, data, pix, - } = readImage(Module, Array.from(image)); + } = readImage(TessModule, Array.from(image)); if (data === null) { - base.SetImage(pix); + api.SetImage(pix); } else { - base.SetImage(data, w, h, bytesPerPixel, w * bytesPerPixel); + api.SetImage(data, w, h, bytesPerPixel, w * bytesPerPixel); } - base.SetRectangle(0, 0, w, h); + api.SetRectangle(0, 0, w, h); return data; }; -const handleInit = (req, res) => { - if (!Module) { - const Core = adapter.getCore(req, res); +const handleInit = ({ corePath }, res) => { + if (!TessModule) { + const Core = adapter.getCore(corePath, res); res.progress({ status: 'initializing tesseract', progress: 0 }); @@ -31,9 +31,9 @@ const handleInit = (req, res) => { latestJob.progress({ status: 'recognizing text', progress: Math.max(0, (percent - 30) / 70) }); }, }) - .then((TessModule) => { - Module = TessModule; - base = new Module.TessBaseAPI(); + .then((tessModule) => { + TessModule = tessModule; + api = new TessModule.TessBaseAPI(); res.progress({ status: 'initialized tesseract', progress: 1 }); }); } @@ -46,68 +46,74 @@ const loadLanguage = ({ options: { langPath, cachePath, cacheMethod, dataPath, }, -}) => ( - loadLang({ +}, res) => { + res.progress({ status: 'loading language traineddata', progress: 0 }); + return loadLang({ langs: lang, - tessModule: Module, + tessModule: TessModule, langURI: langPath, cachePath, cacheMethod, dataPath, - }) -); + }).then((...args) => { + res.progress({ status: 'loaded language traineddata', progress: 1 }); + return args; + }); +}; -const handleRecognize = (req, res) => ( - handleInit(req, res) +const handleRecognize = ({ + image, lang, options, params, +}, res) => ( + handleInit(options, res) .then(() => ( - loadLanguage(req) + loadLanguage({ lang, options }, res) .then(() => { - const { image, lang, params } = req; const progressUpdate = (progress) => { res.progress({ status: 'initializing api', progress }); }; progressUpdate(0); - base.Init(null, lang); + api.Init(null, lang); progressUpdate(0.3); Object.keys(params).forEach((key) => { - base.SetVariable(key, params[key]); + api.SetVariable(key, params[key]); }); progressUpdate(0.6); const ptr = setImage(image); progressUpdate(1); - base.Recognize(null); - const result = dump(Module, base); - base.End(); - Module._free(ptr); + api.Recognize(null); + const result = dump(TessModule, api); + api.End(); + TessModule._free(ptr); res.resolve(result); }) )) ); -const handleDetect = (req, res) => ( - handleInit(req, res) +const handleDetect = ({ + image, lang, options, +}, res) => ( + handleInit(options, res) .then(() => ( - loadLanguage(req) + loadLanguage({ lang, options }, res) .then(() => { - const { image, lang } = req; - base.Init(null, lang); - base.SetPageSegMode(Module.PSM_OSD_ONLY); + api.Init(null, lang); + api.SetPageSegMode(TessModule.PSM_OSD_ONLY); const ptr = setImage(image); - const results = new Module.OSResults(); + const results = new TessModule.OSResults(); - if (!base.DetectOS(results)) { - base.End(); - Module._free(ptr); + if (!api.DetectOS(results)) { + api.End(); + TessModule._free(ptr); res.reject('Failed to detect OS'); } else { const best = results.get_best_result(); const oid = best.get_orientation_id(); const sid = best.get_script_id(); - base.End(); - Module._free(ptr); + api.End(); + TessModule._free(ptr); res.resolve({ tesseract_script_id: sid, @@ -121,12 +127,12 @@ const handleDetect = (req, res) => ( )) ); -exports.dispatchHandlers = (packet, send) => { +exports.dispatchHandlers = ({ jobId, action, payload }, send) => { const respond = (status, data) => { send({ - jobId: packet.jobId, + jobId, status, - action: packet.action, + action, data, }); }; @@ -137,13 +143,13 @@ exports.dispatchHandlers = (packet, send) => { latestJob = respond; try { - if (packet.action === 'recognize') { - handleRecognize(packet.payload, respond); - } else if (packet.action === 'detect') { - handleDetect(packet.payload, respond); + if (action === 'recognize') { + handleRecognize(payload, respond); + } else if (action === 'detect') { + handleDetect(payload, respond); } } catch (err) { - // Prepare exception to travel through postMessage + /** Prepare exception to travel through postMessage */ respond.reject(err.toString()); } }; diff --git a/src/node/index.js b/src/node/index.js index a2f0183..161776a 100644 --- a/src/node/index.js +++ b/src/node/index.js @@ -8,12 +8,12 @@ const { defaultOptions } = require('../common/options'); const readFile = util.promisify(fs.readFile); -const loadImage = (imageURI) => { - if (isURL(imageURI)) { - return fetch(imageURI) +const loadImage = (image) => { + if (isURL(image)) { + return fetch(image) .then(resp => resp.arrayBuffer()); } - return readFile(imageURI); + return readFile(image); }; exports.defaultOptions = { @@ -33,16 +33,12 @@ exports.terminateWorker = (instance) => { instance.worker.kill(); }; -exports.sendPacket = (instance, packet) => { +exports.sendPacket = (instance, iPacket) => { + const packet = { ...iPacket }; loadImage(packet.payload.image) .then(buf => new Uint8Array(buf)) .then((img) => { - instance.worker.send({ - ...packet, - payload: { - ...packet.payload, - image: Array.from(img), - }, - }); + packet.payload.image = Array.from(img); + instance.worker.send(packet); }); }; diff --git a/src/node/worker.js b/src/node/worker.js index d5aaab2..8cb8874 100644 --- a/src/node/worker.js +++ b/src/node/worker.js @@ -8,11 +8,11 @@ process.on('message', (packet) => { }); workerUtils.setAdapter({ - getCore: (req, res) => { + getCore: (corePath, res) => { if (check.null(TesseractCore)) { - res.progress({ status: 'loading tesseract core' }); + res.progress({ status: 'loading tesseract core', progress: 0 }); TesseractCore = require('tesseract.js-core'); - res.progress({ status: 'loaded tesseract core' }); + res.progress({ status: 'loaded tesseract core', progress: 1 }); } return TesseractCore; },