diff --git a/.gitignore b/.gitignore index dd6d7b2..53e3db6 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ node_modules/* yarn.lock tesseract.dev.js -worker.dev.js \ No newline at end of file +worker.dev.js +*.traineddata \ No newline at end of file diff --git a/examples/node/basic.js b/examples/node/basic.js index e881383..9bba8b0 100644 --- a/examples/node/basic.js +++ b/examples/node/basic.js @@ -9,6 +9,6 @@ Tesseract.recognize(image) .catch(err => { console.log('catch\n', err); }) -.finally(data => { - console.log('finally\n', data.text); +.finally(e => { + console.log('finally\n'); }); \ No newline at end of file diff --git a/src/common/job.js b/src/common/job.js index 3d26f38..3fd2c40 100644 --- a/src/common/job.js +++ b/src/common/job.js @@ -3,79 +3,79 @@ const adapter = require('../node/index.js') let jobCounter = 0; module.exports = class TesseractJob { - constructor(instance){ - this.id = 'Job-' + (++jobCounter) + '-' + Math.random().toString(16).slice(3, 8) + constructor(instance){ + this.id = 'Job-' + (++jobCounter) + '-' + Math.random().toString(16).slice(3, 8) - this._instance = instance; - this._resolve = [] - this._reject = [] - this._progress = [] - this._finally = [] - } - - then(resolve, reject){ - if(this._resolve.push){ - this._resolve.push(resolve) - }else{ - resolve(this._resolve) + this._instance = instance; + this._resolve = [] + this._reject = [] + this._progress = [] + this._finally = [] } - if(reject) this.catch(reject); - return this; - } - catch(reject){ - if(this._reject.push){ - this._reject.push(reject) - }else{ - reject(this._reject) + then(resolve, reject){ + if(this._resolve.push){ + this._resolve.push(resolve) + }else{ + resolve(this._resolve) + } + + if(reject) this.catch(reject); + return this; + } + catch(reject){ + if(this._reject.push){ + this._reject.push(reject) + }else{ + reject(this._reject) + } + return this; + } + progress(fn){ + this._progress.push(fn) + return this; + } + finally(fn) { + this._finally.push(fn) + return this; + } + _send(action, payload){ + adapter.sendPacket(this._instance, { + jobId: this.id, + action: action, + payload: payload + }) } - return this; - } - progress(fn){ - this._progress.push(fn) - return this; - } - finally(fn) { - this._finally.push(fn) - return this; - } - _send(action, payload){ - adapter.sendPacket(this._instance, { - jobId: this.id, - action: action, - payload: payload - }) - } - _handle(packet){ - var data = packet.data; - let runFinallyCbs = false; + _handle(packet){ + var data = packet.data; + let runFinallyCbs = false; - if(packet.status === 'resolve'){ - if(this._resolve.length === 0) console.debug(data); - this._resolve.forEach(fn => { - var ret = fn(data); - if(ret && typeof ret.then == 'function'){ - console.warn('TesseractJob instances do not chain like ES6 Promises. To convert it into a real promise, use Promise.resolve.') + if(packet.status === 'resolve'){ + if(this._resolve.length === 0) console.debug(data); + this._resolve.forEach(fn => { + var ret = fn(data); + if(ret && typeof ret.then == 'function'){ + console.warn('TesseractJob instances do not chain like ES6 Promises. To convert it into a real promise, use Promise.resolve.') + } + }) + this._resolve = data; + this._instance._dequeue() + runFinallyCbs = true; + }else if(packet.status === 'reject'){ + if(this._reject.length === 0) console.error(data); + this._reject.forEach(fn => fn(data)) + this._reject = data; + this._instance._dequeue() + runFinallyCbs = true; + }else if(packet.status === 'progress'){ + this._progress.forEach(fn => fn(data)) + }else{ + console.warn('Message type unknown', packet.status) } - }) - this._resolve = data; - this._instance._dequeue() - runFinallyCbs = true; - }else if(packet.status === 'reject'){ - if(this._reject.length === 0) console.error(data); - this._reject.forEach(fn => fn(data)) - this._reject = data; - this._instance._dequeue() - runFinallyCbs = true; - }else if(packet.status === 'progress'){ - this._progress.forEach(fn => fn(data)) - }else{ - console.warn('Message type unknown', packet.status) - } - if (runFinallyCbs) { - this._finally.forEach(fn => fn(data)); + if (runFinallyCbs) { + this._finally.forEach(fn => fn(data)); + } } - } } \ No newline at end of file diff --git a/src/common/worker.js b/src/common/worker.js index 8d3c0cd..400fe60 100644 --- a/src/common/worker.js +++ b/src/common/worker.js @@ -18,10 +18,14 @@ function dispatchHandlers(packet, send){ latestJob = respond; - if(packet.action === 'recognize'){ - handleRecognize(packet.payload, respond) - }else if(packet.action === 'detect'){ - handleDetect(packet.payload, respond) + try { + if(packet.action === 'recognize'){ + handleRecognize(packet.payload, respond) + }else if(packet.action === 'detect'){ + handleDetect(packet.payload, respond) + } + } catch (err) { + respond.reject(err) } } exports.dispatchHandlers = dispatchHandlers; @@ -32,17 +36,25 @@ exports.setAdapter = function setAdapter(impl){ function handleInit(req, res){ - if(!Module){ + var MIN_MEMORY = 100663296; + + if(['chi_sim', 'chi_tra', 'jpn'].indexOf(req.options.lang) != -1){ + MIN_MEMORY = 167772160; + } + + if(!Module || Module.TOTAL_MEMORY < MIN_MEMORY){ var Core = adapter.getCore(req, res); res.progress({ status: 'initializing tesseract', progress: 0 }) + Module = Core({ - TOTAL_MEMORY: req.memory, + TOTAL_MEMORY: MIN_MEMORY, TesseractProgress(percent){ latestJob.progress({ status: 'recognizing text', progress: Math.max(0, (percent-30)/70) }) }, onRuntimeInitialized() {} }) + Module.FS_createPath("/", "tessdata", true, true) base = new Module.TessBaseAPI() res.progress({ status: 'initializing tesseract', progress: 1 })