Browse Source

choosing memory usage dynamically

pull/34/head
Kevin Kwok 8 years ago
parent
commit
ea54eaa23f
  1. 3
      .gitignore
  2. 4
      examples/node/basic.js
  3. 132
      src/common/job.js
  4. 24
      src/common/worker.js

3
.gitignore vendored

@ -2,4 +2,5 @@
node_modules/* node_modules/*
yarn.lock yarn.lock
tesseract.dev.js tesseract.dev.js
worker.dev.js worker.dev.js
*.traineddata

4
examples/node/basic.js

@ -9,6 +9,6 @@ Tesseract.recognize(image)
.catch(err => { .catch(err => {
console.log('catch\n', err); console.log('catch\n', err);
}) })
.finally(data => { .finally(e => {
console.log('finally\n', data.text); console.log('finally\n');
}); });

132
src/common/job.js

@ -3,79 +3,79 @@ const adapter = require('../node/index.js')
let jobCounter = 0; let jobCounter = 0;
module.exports = class TesseractJob { module.exports = class TesseractJob {
constructor(instance){ constructor(instance){
this.id = 'Job-' + (++jobCounter) + '-' + Math.random().toString(16).slice(3, 8) this.id = 'Job-' + (++jobCounter) + '-' + Math.random().toString(16).slice(3, 8)
this._instance = instance; this._instance = instance;
this._resolve = [] this._resolve = []
this._reject = [] this._reject = []
this._progress = [] this._progress = []
this._finally = [] this._finally = []
}
then(resolve, reject){
if(this._resolve.push){
this._resolve.push(resolve)
}else{
resolve(this._resolve)
} }
if(reject) this.catch(reject); then(resolve, reject){
return this; if(this._resolve.push){
} this._resolve.push(resolve)
catch(reject){ }else{
if(this._reject.push){ resolve(this._resolve)
this._reject.push(reject) }
}else{
reject(this._reject) 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){ _handle(packet){
var data = packet.data; var data = packet.data;
let runFinallyCbs = false; let runFinallyCbs = false;
if(packet.status === 'resolve'){ if(packet.status === 'resolve'){
if(this._resolve.length === 0) console.debug(data); if(this._resolve.length === 0) console.debug(data);
this._resolve.forEach(fn => { this._resolve.forEach(fn => {
var ret = fn(data); var ret = fn(data);
if(ret && typeof ret.then == 'function'){ 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.') 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) { if (runFinallyCbs) {
this._finally.forEach(fn => fn(data)); this._finally.forEach(fn => fn(data));
}
} }
}
} }

24
src/common/worker.js

@ -18,10 +18,14 @@ function dispatchHandlers(packet, send){
latestJob = respond; latestJob = respond;
if(packet.action === 'recognize'){ try {
handleRecognize(packet.payload, respond) if(packet.action === 'recognize'){
}else if(packet.action === 'detect'){ handleRecognize(packet.payload, respond)
handleDetect(packet.payload, respond) }else if(packet.action === 'detect'){
handleDetect(packet.payload, respond)
}
} catch (err) {
respond.reject(err)
} }
} }
exports.dispatchHandlers = dispatchHandlers; exports.dispatchHandlers = dispatchHandlers;
@ -32,17 +36,25 @@ exports.setAdapter = function setAdapter(impl){
function handleInit(req, res){ 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); var Core = adapter.getCore(req, res);
res.progress({ status: 'initializing tesseract', progress: 0 }) res.progress({ status: 'initializing tesseract', progress: 0 })
Module = Core({ Module = Core({
TOTAL_MEMORY: req.memory, TOTAL_MEMORY: MIN_MEMORY,
TesseractProgress(percent){ TesseractProgress(percent){
latestJob.progress({ status: 'recognizing text', progress: Math.max(0, (percent-30)/70) }) latestJob.progress({ status: 'recognizing text', progress: Math.max(0, (percent-30)/70) })
}, },
onRuntimeInitialized() {} onRuntimeInitialized() {}
}) })
Module.FS_createPath("/", "tessdata", true, true) Module.FS_createPath("/", "tessdata", true, true)
base = new Module.TessBaseAPI() base = new Module.TessBaseAPI()
res.progress({ status: 'initializing tesseract', progress: 1 }) res.progress({ status: 'initializing tesseract', progress: 1 })

Loading…
Cancel
Save