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 @@ @@ -2,4 +2,5 @@
node_modules/*
yarn.lock
tesseract.dev.js
worker.dev.js
worker.dev.js
*.traineddata

4
examples/node/basic.js

@ -9,6 +9,6 @@ Tesseract.recognize(image) @@ -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');
});

132
src/common/job.js

@ -3,79 +3,79 @@ const adapter = require('../node/index.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));
}
}
}
}

24
src/common/worker.js

@ -18,10 +18,14 @@ function dispatchHandlers(packet, send){ @@ -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){ @@ -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 })

Loading…
Cancel
Save