Browse Source

added progress callback

pull/24/head
Guillermo 10 years ago
parent
commit
b2ff251c7d
  1. 7
      example.htm
  2. 24
      lib/Tesseract.js
  3. 35
      lib/worker.js
  4. 36
      worker_src/worker.js

7
example.htm

@ -13,5 +13,10 @@
// ctx.fillText('2小時可換乘2次2小時可換乘2次', 100, 80) // ctx.fillText('2小時可換乘2次2小時可換乘2次', 100, 80)
ctx.font = '30px sans-serif' ctx.font = '30px sans-serif'
ctx.fillText('the Cosmic Void', 100, 120) ctx.fillText('the Cosmic Void', 100, 120)
Tesseract.recognize(canvas,{tessedit_char_blacklist:'e'}).then( function(d){ console.log(d) } ) Tesseract.recognize(canvas,{
tessedit_char_blacklist:'e',
progress: function(e){
console.log(e)
}
}).then( function(d){ console.log(d) } )
</script> </script>

24
lib/Tesseract.js

@ -34,11 +34,26 @@ Tesseract.recognize = function(image, options, callback){
} }
if(image.getImageData) image = image.getImageData(0, 0, image.canvas.width, image.canvas.height); if(image.getImageData) image = image.getImageData(0, 0, image.canvas.width, image.canvas.height);
var worker = new Worker('https://cdn.rawgit.com/naptha/tesseract.js/master/lib/worker.js') var worker = new Worker('/lib/worker.js')
var progress = (function(){
if(typeof options.progress === 'function'){
var p = options.progress
delete options.progress
return p
}
return function(){}
})()
if(typeof callback === "function"){ if(typeof callback === "function"){
worker.onmessage = function(e){ worker.onmessage = function(e){
callback(e.data.err, e.data.result) if(e.data.progress){
progress(e.data.progress)
}
else{
callback(e.data.err, e.data.result)
}
} }
worker.postMessage({image: image, lang: lang}) worker.postMessage({image: image, lang: lang})
console.log('callback') console.log('callback')
@ -46,7 +61,10 @@ Tesseract.recognize = function(image, options, callback){
else { else {
return new Promise(function(resolve, reject){ return new Promise(function(resolve, reject){
worker.onmessage = function(e){ worker.onmessage = function(e){
if(e.data.err){ if(e.data.progress){
progress(e.data.progress)
}
else if(e.data.err){
reject(e.data.err) reject(e.data.err)
} }
else { else {

35
lib/worker.js

@ -6489,7 +6489,11 @@ var recognize = (function createTesseractInstance(){
var Module = Tesseract304({ var Module = Tesseract304({
TOTAL_MEMORY: 90e6, TOTAL_MEMORY: 90e6,
TesseractProgress: function(percent){ TesseractProgress: function(percent){
console.log('recognized',percent+'%') postMessage({
'progress': {
'recognized': percent/100
}
})
} }
}) })
@ -6505,12 +6509,22 @@ var recognize = (function createTesseractInstance(){
xhr.open('GET', 'https://cdn.rawgit.com/naptha/tessdata/gh-pages/3.02/'+lang+'.traineddata.gz', true); xhr.open('GET', 'https://cdn.rawgit.com/naptha/tessdata/gh-pages/3.02/'+lang+'.traineddata.gz', true);
xhr.responseType = 'arraybuffer'; xhr.responseType = 'arraybuffer';
xhr.onerror = function(){ cb(xhr, null) } xhr.onerror = function(){ cb(xhr, null) }
xhr.onprogress = function(e){console.log('loading',lang,'language model:',Math.round(e.loaded/filesizes[lang]*100)+'%')} xhr.onprogress = function(e){
postMessage({
'progress': {
'loaded_lang_model': e.loaded/filesizes[lang]
}
})
}
xhr.onload = function(){ xhr.onload = function(){
if (xhr.status == 200 || (xhr.status == 0 && xhr.response)) { if (xhr.status == 200 || (xhr.status == 0 && xhr.response)) {
console.log('unzipping language model...') postMessage({
var data = new Uint8Array(pako.deflate(new Uint8Array(xhr.response))) 'progress': 'unzipping_lang_model'
console.log(lang +".traineddata", 'sucessfully unzipped') })
var data = pako.inflate(new Uint8Array(xhr.response))
postMessage({
'progress': 'unzipped_lang_model'
})
Module.FS_createDataFile('tessdata', lang +".traineddata", data, true, false); Module.FS_createDataFile('tessdata', lang +".traineddata", data, true, false);
loaded_langs.push(lang) loaded_langs.push(lang)
cb(null, lang) cb(null, lang)
@ -6698,7 +6712,7 @@ var recognize = (function createTesseractInstance(){
var ptr = Module.allocate(image, 'i8', Module.ALLOC_NORMAL); var ptr = Module.allocate(image, 'i8', Module.ALLOC_NORMAL);
loadLanguage(lang, function(err, result){ loadLanguage(lang, function(err, result){
if(err){ if(err){
console.error("error loading", lang); console.error("error loading", lang);
cb(err, null) cb(err, null)
@ -6707,7 +6721,14 @@ var recognize = (function createTesseractInstance(){
for (var option in options) { for (var option in options) {
if (options.hasOwnProperty(option)) { if (options.hasOwnProperty(option)) {
base.SetVariable(option, options[option]); base.SetVariable(option, options[option]);
console.log('setting', option, '=', options[option]); postMessage({
progress: {
set_variable: {
variable: option,
value: options[option]
}
}
})
} }
} }

36
worker_src/worker.js

@ -79,7 +79,11 @@ var recognize = (function createTesseractInstance(){
var Module = Tesseract304({ var Module = Tesseract304({
TOTAL_MEMORY: 90e6, TOTAL_MEMORY: 90e6,
TesseractProgress: function(percent){ TesseractProgress: function(percent){
console.log('recognized',percent+'%') postMessage({
'progress': {
'recognized': percent/100
}
})
} }
}) })
@ -95,12 +99,22 @@ var recognize = (function createTesseractInstance(){
xhr.open('GET', 'https://cdn.rawgit.com/naptha/tessdata/gh-pages/3.02/'+lang+'.traineddata.gz', true); xhr.open('GET', 'https://cdn.rawgit.com/naptha/tessdata/gh-pages/3.02/'+lang+'.traineddata.gz', true);
xhr.responseType = 'arraybuffer'; xhr.responseType = 'arraybuffer';
xhr.onerror = function(){ cb(xhr, null) } xhr.onerror = function(){ cb(xhr, null) }
xhr.onprogress = function(e){console.log('loading',lang,'language model:',Math.round(e.loaded/filesizes[lang]*100)+'%')} xhr.onprogress = function(e){
postMessage({
'progress': {
'loaded_lang_model': e.loaded/filesizes[lang]
}
})
}
xhr.onload = function(){ xhr.onload = function(){
if (xhr.status == 200 || (xhr.status == 0 && xhr.response)) { if (xhr.status == 200 || (xhr.status == 0 && xhr.response)) {
console.log('unzipping language model...') postMessage({
var data = new Uint8Array(pako.deflate(new Uint8Array(xhr.response))) 'progress': 'unzipping_lang_model'
console.log(lang +".traineddata", 'sucessfully unzipped') })
var data = pako.inflate(new Uint8Array(xhr.response))
postMessage({
'progress': 'unzipped_lang_model'
})
Module.FS_createDataFile('tessdata', lang +".traineddata", data, true, false); Module.FS_createDataFile('tessdata', lang +".traineddata", data, true, false);
loaded_langs.push(lang) loaded_langs.push(lang)
cb(null, lang) cb(null, lang)
@ -288,7 +302,7 @@ var recognize = (function createTesseractInstance(){
var ptr = Module.allocate(image, 'i8', Module.ALLOC_NORMAL); var ptr = Module.allocate(image, 'i8', Module.ALLOC_NORMAL);
loadLanguage(lang, function(err, result){ loadLanguage(lang, function(err, result){
if(err){ if(err){
console.error("error loading", lang); console.error("error loading", lang);
cb(err, null) cb(err, null)
@ -297,7 +311,14 @@ var recognize = (function createTesseractInstance(){
for (var option in options) { for (var option in options) {
if (options.hasOwnProperty(option)) { if (options.hasOwnProperty(option)) {
base.SetVariable(option, options[option]); base.SetVariable(option, options[option]);
console.log('setting', option, '=', options[option]); postMessage({
progress: {
set_variable: {
variable: option,
value: options[option]
}
}
})
} }
} }
@ -312,7 +333,6 @@ var recognize = (function createTesseractInstance(){
}) })
} }
// base._simple = _simple
return recognize return recognize
})() })()

Loading…
Cancel
Save