Browse Source

choosing memory usage dynamically

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

1
.gitignore vendored

@ -3,3 +3,4 @@ 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');
}); });

16
src/common/worker.js

@ -18,11 +18,15 @@ function dispatchHandlers(packet, send){
latestJob = respond; latestJob = respond;
try {
if(packet.action === 'recognize'){ if(packet.action === 'recognize'){
handleRecognize(packet.payload, respond) handleRecognize(packet.payload, respond)
}else if(packet.action === 'detect'){ }else if(packet.action === 'detect'){
handleDetect(packet.payload, respond) 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