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 @@ @@ -13,5 +13,10 @@
// ctx.fillText('2小時可換乘2次2小時可換乘2次', 100, 80)
ctx.font = '30px sans-serif'
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>

24
lib/Tesseract.js

@ -34,11 +34,26 @@ Tesseract.recognize = function(image, options, callback){ @@ -34,11 +34,26 @@ Tesseract.recognize = function(image, options, callback){
}
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"){
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})
console.log('callback')
@ -46,7 +61,10 @@ Tesseract.recognize = function(image, options, callback){ @@ -46,7 +61,10 @@ Tesseract.recognize = function(image, options, callback){
else {
return new Promise(function(resolve, reject){
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)
}
else {

35
lib/worker.js

@ -6489,7 +6489,11 @@ var recognize = (function createTesseractInstance(){ @@ -6489,7 +6489,11 @@ var recognize = (function createTesseractInstance(){
var Module = Tesseract304({
TOTAL_MEMORY: 90e6,
TesseractProgress: function(percent){
console.log('recognized',percent+'%')
postMessage({
'progress': {
'recognized': percent/100
}
})
}
})
@ -6505,12 +6509,22 @@ var recognize = (function createTesseractInstance(){ @@ -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.responseType = 'arraybuffer';
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(){
if (xhr.status == 200 || (xhr.status == 0 && xhr.response)) {
console.log('unzipping language model...')
var data = new Uint8Array(pako.deflate(new Uint8Array(xhr.response)))
console.log(lang +".traineddata", 'sucessfully unzipped')
postMessage({
'progress': 'unzipping_lang_model'
})
var data = pako.inflate(new Uint8Array(xhr.response))
postMessage({
'progress': 'unzipped_lang_model'
})
Module.FS_createDataFile('tessdata', lang +".traineddata", data, true, false);
loaded_langs.push(lang)
cb(null, lang)
@ -6698,7 +6712,7 @@ var recognize = (function createTesseractInstance(){ @@ -6698,7 +6712,7 @@ var recognize = (function createTesseractInstance(){
var ptr = Module.allocate(image, 'i8', Module.ALLOC_NORMAL);
loadLanguage(lang, function(err, result){
if(err){
console.error("error loading", lang);
cb(err, null)
@ -6707,7 +6721,14 @@ var recognize = (function createTesseractInstance(){ @@ -6707,7 +6721,14 @@ var recognize = (function createTesseractInstance(){
for (var option in options) {
if (options.hasOwnProperty(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(){ @@ -79,7 +79,11 @@ var recognize = (function createTesseractInstance(){
var Module = Tesseract304({
TOTAL_MEMORY: 90e6,
TesseractProgress: function(percent){
console.log('recognized',percent+'%')
postMessage({
'progress': {
'recognized': percent/100
}
})
}
})
@ -95,12 +99,22 @@ var recognize = (function createTesseractInstance(){ @@ -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.responseType = 'arraybuffer';
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(){
if (xhr.status == 200 || (xhr.status == 0 && xhr.response)) {
console.log('unzipping language model...')
var data = new Uint8Array(pako.deflate(new Uint8Array(xhr.response)))
console.log(lang +".traineddata", 'sucessfully unzipped')
postMessage({
'progress': 'unzipping_lang_model'
})
var data = pako.inflate(new Uint8Array(xhr.response))
postMessage({
'progress': 'unzipped_lang_model'
})
Module.FS_createDataFile('tessdata', lang +".traineddata", data, true, false);
loaded_langs.push(lang)
cb(null, lang)
@ -288,7 +302,7 @@ var recognize = (function createTesseractInstance(){ @@ -288,7 +302,7 @@ var recognize = (function createTesseractInstance(){
var ptr = Module.allocate(image, 'i8', Module.ALLOC_NORMAL);
loadLanguage(lang, function(err, result){
if(err){
console.error("error loading", lang);
cb(err, null)
@ -297,7 +311,14 @@ var recognize = (function createTesseractInstance(){ @@ -297,7 +311,14 @@ var recognize = (function createTesseractInstance(){
for (var option in options) {
if (options.hasOwnProperty(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(){ @@ -312,7 +333,6 @@ var recognize = (function createTesseractInstance(){
})
}
// base._simple = _simple
return recognize
})()

Loading…
Cancel
Save