Guillermo
10 years ago
3 changed files with 11886 additions and 79 deletions
@ -1,79 +1,119 @@ |
|||||||
var Tesseract = {} |
var Tesseract = (function(){ |
||||||
|
|
||||||
Tesseract.recognize = function(image, options, callback){ |
var Tesseract = {} |
||||||
var lang = options.lang |
|
||||||
if(typeof lang === "undefined"){ |
var blob = new Blob(["importScripts('http://localhost:1234/master/worker/worker.js');"]); |
||||||
lang = 'eng' |
console.log('localhost') |
||||||
} |
var worker = new Worker(window.URL.createObjectURL(blob)); |
||||||
|
|
||||||
|
var index = 0 |
||||||
|
var handlers = [] |
||||||
|
|
||||||
if (typeof options === 'string') { |
worker.onmessage = function(e){ |
||||||
lang = options |
// console.log(handlers, e)
|
||||||
options = {} |
var handler = handlers[e.data.index] |
||||||
|
if(e.data.progress){ |
||||||
|
handler.progress(e.data.progress) |
||||||
|
} |
||||||
|
else if(e.data.err){ |
||||||
|
handler.reject(e.data.err) |
||||||
|
handler.callback(e.data.err) |
||||||
|
} |
||||||
|
else { |
||||||
|
handler.resolve(e.data.result) |
||||||
|
handler.callback(null,e.data.result) |
||||||
|
} |
||||||
} |
} |
||||||
|
|
||||||
if (typeof options === "function") { |
function convertToImageData(image){ |
||||||
callback = options |
if(image.getContext){ |
||||||
options = {} |
image = image.getContext('2d'); |
||||||
|
}else if(image.tagName == "IMG" || image.tagName == "VIDEO"){ |
||||||
|
var c = document.createElement('canvas'); |
||||||
|
if(image.tagName == "IMG"){ |
||||||
|
c.width = image.naturalWidth; |
||||||
|
c.height = image.naturalHeight; |
||||||
|
}else if(image.tagName == "VIDEO"){ |
||||||
|
c.width = image.videoWidth; |
||||||
|
c.height = image.videoHeight; |
||||||
|
} |
||||||
|
var ctx = c.getContext('2d'); |
||||||
|
ctx.drawImage(image, 0, 0); |
||||||
|
image = ctx; |
||||||
|
} |
||||||
|
if(image.getImageData) image = image.getImageData(0, 0, image.canvas.width, image.canvas.height); |
||||||
|
return image |
||||||
} |
} |
||||||
|
|
||||||
|
Tesseract.detect = function(image, progress, callback){ |
||||||
|
image = convertToImageData(image) |
||||||
|
|
||||||
if(image.getContext){ |
if(typeof progress === "undefined"){ |
||||||
image = image.getContext('2d'); |
progress = callback = new Function() |
||||||
}else if(image.tagName == "IMG" || image.tagName == "VIDEO"){ |
|
||||||
var c = document.createElement('canvas'); |
|
||||||
if(image.tagName == "IMG"){ |
|
||||||
c.width = image.naturalWidth; |
|
||||||
c.height = image.naturalHeight; |
|
||||||
}else if(image.tagName == "VIDEO"){ |
|
||||||
c.width = image.videoWidth; |
|
||||||
c.height = image.videoHeight; |
|
||||||
} |
} |
||||||
var ctx = c.getContext('2d'); |
|
||||||
ctx.drawImage(image, 0, 0); |
|
||||||
image = ctx; |
|
||||||
} |
|
||||||
if(image.getImageData) image = image.getImageData(0, 0, image.canvas.width, image.canvas.height); |
|
||||||
|
|
||||||
|
if (typeof callback === "undefined"){ |
||||||
|
callback = progress |
||||||
|
progress = new Function() |
||||||
|
} |
||||||
|
|
||||||
var blob = new Blob(["importScripts('https://cdn.rawgit.com/naptha/tesseract.js/master/worker/worker.js');"]); |
var i = index++ |
||||||
|
|
||||||
var worker = new Worker(window.URL.createObjectURL(blob)); |
handlers[i] = { |
||||||
|
resolve: new Function(), |
||||||
|
reject: new Function() |
||||||
|
} |
||||||
|
handlers[i].callback = callback |
||||||
|
handlers[i].progress = progress |
||||||
|
|
||||||
|
return new Promise(function(resolve, reject){ |
||||||
|
handlers[i].resolve = resolve |
||||||
|
handlers[i].reject = reject |
||||||
|
worker.postMessage({index: i, fun: 'detect', image: image}) |
||||||
|
}) |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
var progress = (function(){ |
Tesseract.recognize = function(image, options, callback){ |
||||||
if(typeof options.progress === 'function'){ |
var lang = options.lang |
||||||
var p = options.progress |
if(typeof lang === "undefined"){ |
||||||
delete options.progress |
lang = 'eng' |
||||||
return p |
|
||||||
} |
} |
||||||
return function(){} |
|
||||||
})() |
|
||||||
|
|
||||||
|
if (typeof options === 'string') { |
||||||
|
lang = options |
||||||
|
options = {} |
||||||
|
} |
||||||
|
|
||||||
if(typeof callback === "function"){ |
if (typeof options === "function") { |
||||||
worker.onmessage = function(e){ |
callback = options |
||||||
if(e.data.progress){ |
options = {} |
||||||
progress(e.data.progress) |
|
||||||
} |
|
||||||
else{ |
|
||||||
callback(e.data.err, e.data.result) |
|
||||||
} |
|
||||||
} |
} |
||||||
worker.postMessage({image: image, lang: lang}) |
|
||||||
} |
image = convertToImageData(image) |
||||||
else { |
|
||||||
return new Promise(function(resolve, reject){ |
var i = index++ |
||||||
worker.onmessage = function(e){ |
|
||||||
if(e.data.progress){ |
handlers[i] = { |
||||||
progress(e.data.progress) |
resolve: new Function(), |
||||||
} |
reject: new Function() |
||||||
else if(e.data.err){ |
} |
||||||
reject(e.data.err) |
handlers[i].callback = callback || new Function() |
||||||
} |
handlers[i].progress = (function(){ |
||||||
else { |
if(typeof options.progress === 'function'){ |
||||||
resolve(e.data.result) |
var p = options.progress |
||||||
} |
delete options.progress |
||||||
|
return p |
||||||
} |
} |
||||||
worker.postMessage({image: image, lang: lang, options: options}) |
return function(){} |
||||||
|
})() |
||||||
|
|
||||||
|
return new Promise(function(resolve, reject){ |
||||||
|
handlers[i].resolve = resolve |
||||||
|
handlers[i].reject = reject |
||||||
|
worker.postMessage({index: i, fun: 'recognize', image: image, lang: lang, options: options}) |
||||||
}) |
}) |
||||||
|
|
||||||
} |
} |
||||||
} |
return Tesseract |
||||||
|
})() |
Loading…
Reference in new issue