diff --git a/bify b/bify new file mode 100644 index 0000000..5079b03 --- /dev/null +++ b/bify @@ -0,0 +1 @@ +browserify src/worker.js -o inter/worker.js \ No newline at end of file diff --git a/compile b/compile deleted file mode 100644 index 36d2e5e..0000000 --- a/compile +++ /dev/null @@ -1,3 +0,0 @@ -browserify worker_src/worker.js -o worker/wumbo.js -cat worker_src/madeline.js worker/wumbo.js > worker/worker.js -rm worker/wumbo.js \ No newline at end of file diff --git a/lib/Tesseract..js b/lib/Tesseract..js new file mode 100644 index 0000000..95da8fa --- /dev/null +++ b/lib/Tesseract..js @@ -0,0 +1,130 @@ +var Tesseract = (function(){ + + var Tesseract = {} + + //https://cdn.rawgit.com/naptha/tesseract.js/master/worker/worker.js + //https://rawgit.com/naptha/tesseract.js/master/worker/worker.js for testing + //https://cdn.rawgit.com/naptha/tesseract.js/master/worker/worker.js + + var blob = new Blob(["importScripts('__tesseractjs__');"]); // changed on build + // console.log('localhost') + var worker = new Worker(window.URL.createObjectURL(blob)); + worker.postMessage({init: {mem: 16777216*6}}) + var bigworker = false + + var index = 0 + var handlers = [] + + worker.onmessage = function(e){ + 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) + } + } + + function convertToImageData(image){ + if(image.getContext){ + 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(typeof progress === "undefined"){ + progress = callback = new Function() + } + + if (typeof callback === "undefined"){ + callback = progress + progress = new Function() + } + + var i = index++ + + 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}) + }) + + } + + Tesseract.recognize = function(image, options, callback){ + var lang = options.lang + if (typeof lang === "undefined"){ + lang = 'eng' + } + + if (!bigworker && ['chi_sim', 'chi_tra', 'jpn'].indexOf(lang) != -1){ + worker.postMessage({init: {mem: 16777216*10}}) + bigworker = true + console.log('started big worker') + } + + if (typeof options === 'string') { + lang = options + options = {} + } + + if (typeof options === "function") { + callback = options + options = {} + } + + image = convertToImageData(image) + + var i = index++ + + handlers[i] = { + resolve: new Function(), + reject: new Function() + } + handlers[i].callback = callback || new Function() + handlers[i].progress = (function(){ + if(typeof options.progress === 'function'){ + var p = options.progress + delete options.progress + return p + } + 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 +})() diff --git a/lib/Tesseract.2015.07.14.js b/lib/Tesseract.2015.07.14.js new file mode 100644 index 0000000..95da8fa --- /dev/null +++ b/lib/Tesseract.2015.07.14.js @@ -0,0 +1,130 @@ +var Tesseract = (function(){ + + var Tesseract = {} + + //https://cdn.rawgit.com/naptha/tesseract.js/master/worker/worker.js + //https://rawgit.com/naptha/tesseract.js/master/worker/worker.js for testing + //https://cdn.rawgit.com/naptha/tesseract.js/master/worker/worker.js + + var blob = new Blob(["importScripts('__tesseractjs__');"]); // changed on build + // console.log('localhost') + var worker = new Worker(window.URL.createObjectURL(blob)); + worker.postMessage({init: {mem: 16777216*6}}) + var bigworker = false + + var index = 0 + var handlers = [] + + worker.onmessage = function(e){ + 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) + } + } + + function convertToImageData(image){ + if(image.getContext){ + 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(typeof progress === "undefined"){ + progress = callback = new Function() + } + + if (typeof callback === "undefined"){ + callback = progress + progress = new Function() + } + + var i = index++ + + 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}) + }) + + } + + Tesseract.recognize = function(image, options, callback){ + var lang = options.lang + if (typeof lang === "undefined"){ + lang = 'eng' + } + + if (!bigworker && ['chi_sim', 'chi_tra', 'jpn'].indexOf(lang) != -1){ + worker.postMessage({init: {mem: 16777216*10}}) + bigworker = true + console.log('started big worker') + } + + if (typeof options === 'string') { + lang = options + options = {} + } + + if (typeof options === "function") { + callback = options + options = {} + } + + image = convertToImageData(image) + + var i = index++ + + handlers[i] = { + resolve: new Function(), + reject: new Function() + } + handlers[i].callback = callback || new Function() + handlers[i].progress = (function(){ + if(typeof options.progress === 'function'){ + var p = options.progress + delete options.progress + return p + } + 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 +})() diff --git a/lib/Tesseract.2015.07.14.v..js b/lib/Tesseract.2015.07.14.v..js new file mode 100644 index 0000000..95da8fa --- /dev/null +++ b/lib/Tesseract.2015.07.14.v..js @@ -0,0 +1,130 @@ +var Tesseract = (function(){ + + var Tesseract = {} + + //https://cdn.rawgit.com/naptha/tesseract.js/master/worker/worker.js + //https://rawgit.com/naptha/tesseract.js/master/worker/worker.js for testing + //https://cdn.rawgit.com/naptha/tesseract.js/master/worker/worker.js + + var blob = new Blob(["importScripts('__tesseractjs__');"]); // changed on build + // console.log('localhost') + var worker = new Worker(window.URL.createObjectURL(blob)); + worker.postMessage({init: {mem: 16777216*6}}) + var bigworker = false + + var index = 0 + var handlers = [] + + worker.onmessage = function(e){ + 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) + } + } + + function convertToImageData(image){ + if(image.getContext){ + 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(typeof progress === "undefined"){ + progress = callback = new Function() + } + + if (typeof callback === "undefined"){ + callback = progress + progress = new Function() + } + + var i = index++ + + 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}) + }) + + } + + Tesseract.recognize = function(image, options, callback){ + var lang = options.lang + if (typeof lang === "undefined"){ + lang = 'eng' + } + + if (!bigworker && ['chi_sim', 'chi_tra', 'jpn'].indexOf(lang) != -1){ + worker.postMessage({init: {mem: 16777216*10}}) + bigworker = true + console.log('started big worker') + } + + if (typeof options === 'string') { + lang = options + options = {} + } + + if (typeof options === "function") { + callback = options + options = {} + } + + image = convertToImageData(image) + + var i = index++ + + handlers[i] = { + resolve: new Function(), + reject: new Function() + } + handlers[i].callback = callback || new Function() + handlers[i].progress = (function(){ + if(typeof options.progress === 'function'){ + var p = options.progress + delete options.progress + return p + } + 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 +})() diff --git a/lib/Tesseract.2015.07.14.v.0.js b/lib/Tesseract.2015.07.14.v.0.js new file mode 100644 index 0000000..95da8fa --- /dev/null +++ b/lib/Tesseract.2015.07.14.v.0.js @@ -0,0 +1,130 @@ +var Tesseract = (function(){ + + var Tesseract = {} + + //https://cdn.rawgit.com/naptha/tesseract.js/master/worker/worker.js + //https://rawgit.com/naptha/tesseract.js/master/worker/worker.js for testing + //https://cdn.rawgit.com/naptha/tesseract.js/master/worker/worker.js + + var blob = new Blob(["importScripts('__tesseractjs__');"]); // changed on build + // console.log('localhost') + var worker = new Worker(window.URL.createObjectURL(blob)); + worker.postMessage({init: {mem: 16777216*6}}) + var bigworker = false + + var index = 0 + var handlers = [] + + worker.onmessage = function(e){ + 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) + } + } + + function convertToImageData(image){ + if(image.getContext){ + 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(typeof progress === "undefined"){ + progress = callback = new Function() + } + + if (typeof callback === "undefined"){ + callback = progress + progress = new Function() + } + + var i = index++ + + 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}) + }) + + } + + Tesseract.recognize = function(image, options, callback){ + var lang = options.lang + if (typeof lang === "undefined"){ + lang = 'eng' + } + + if (!bigworker && ['chi_sim', 'chi_tra', 'jpn'].indexOf(lang) != -1){ + worker.postMessage({init: {mem: 16777216*10}}) + bigworker = true + console.log('started big worker') + } + + if (typeof options === 'string') { + lang = options + options = {} + } + + if (typeof options === "function") { + callback = options + options = {} + } + + image = convertToImageData(image) + + var i = index++ + + handlers[i] = { + resolve: new Function(), + reject: new Function() + } + handlers[i].callback = callback || new Function() + handlers[i].progress = (function(){ + if(typeof options.progress === 'function'){ + var p = options.progress + delete options.progress + return p + } + 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 +})() diff --git a/lib/Tesseract.2015.07.14.v.1.js b/lib/Tesseract.2015.07.14.v.1.js new file mode 100644 index 0000000..95da8fa --- /dev/null +++ b/lib/Tesseract.2015.07.14.v.1.js @@ -0,0 +1,130 @@ +var Tesseract = (function(){ + + var Tesseract = {} + + //https://cdn.rawgit.com/naptha/tesseract.js/master/worker/worker.js + //https://rawgit.com/naptha/tesseract.js/master/worker/worker.js for testing + //https://cdn.rawgit.com/naptha/tesseract.js/master/worker/worker.js + + var blob = new Blob(["importScripts('__tesseractjs__');"]); // changed on build + // console.log('localhost') + var worker = new Worker(window.URL.createObjectURL(blob)); + worker.postMessage({init: {mem: 16777216*6}}) + var bigworker = false + + var index = 0 + var handlers = [] + + worker.onmessage = function(e){ + 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) + } + } + + function convertToImageData(image){ + if(image.getContext){ + 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(typeof progress === "undefined"){ + progress = callback = new Function() + } + + if (typeof callback === "undefined"){ + callback = progress + progress = new Function() + } + + var i = index++ + + 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}) + }) + + } + + Tesseract.recognize = function(image, options, callback){ + var lang = options.lang + if (typeof lang === "undefined"){ + lang = 'eng' + } + + if (!bigworker && ['chi_sim', 'chi_tra', 'jpn'].indexOf(lang) != -1){ + worker.postMessage({init: {mem: 16777216*10}}) + bigworker = true + console.log('started big worker') + } + + if (typeof options === 'string') { + lang = options + options = {} + } + + if (typeof options === "function") { + callback = options + options = {} + } + + image = convertToImageData(image) + + var i = index++ + + handlers[i] = { + resolve: new Function(), + reject: new Function() + } + handlers[i].callback = callback || new Function() + handlers[i].progress = (function(){ + if(typeof options.progress === 'function'){ + var p = options.progress + delete options.progress + return p + } + 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 +})() diff --git a/lib/Tesseract.2015.07.14.v.asdf.js b/lib/Tesseract.2015.07.14.v.asdf.js new file mode 100644 index 0000000..95da8fa --- /dev/null +++ b/lib/Tesseract.2015.07.14.v.asdf.js @@ -0,0 +1,130 @@ +var Tesseract = (function(){ + + var Tesseract = {} + + //https://cdn.rawgit.com/naptha/tesseract.js/master/worker/worker.js + //https://rawgit.com/naptha/tesseract.js/master/worker/worker.js for testing + //https://cdn.rawgit.com/naptha/tesseract.js/master/worker/worker.js + + var blob = new Blob(["importScripts('__tesseractjs__');"]); // changed on build + // console.log('localhost') + var worker = new Worker(window.URL.createObjectURL(blob)); + worker.postMessage({init: {mem: 16777216*6}}) + var bigworker = false + + var index = 0 + var handlers = [] + + worker.onmessage = function(e){ + 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) + } + } + + function convertToImageData(image){ + if(image.getContext){ + 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(typeof progress === "undefined"){ + progress = callback = new Function() + } + + if (typeof callback === "undefined"){ + callback = progress + progress = new Function() + } + + var i = index++ + + 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}) + }) + + } + + Tesseract.recognize = function(image, options, callback){ + var lang = options.lang + if (typeof lang === "undefined"){ + lang = 'eng' + } + + if (!bigworker && ['chi_sim', 'chi_tra', 'jpn'].indexOf(lang) != -1){ + worker.postMessage({init: {mem: 16777216*10}}) + bigworker = true + console.log('started big worker') + } + + if (typeof options === 'string') { + lang = options + options = {} + } + + if (typeof options === "function") { + callback = options + options = {} + } + + image = convertToImageData(image) + + var i = index++ + + handlers[i] = { + resolve: new Function(), + reject: new Function() + } + handlers[i].callback = callback || new Function() + handlers[i].progress = (function(){ + if(typeof options.progress === 'function'){ + var p = options.progress + delete options.progress + return p + } + 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 +})() diff --git a/lib/Tesseract.2015.07.15.v.0.js b/lib/Tesseract.2015.07.15.v.0.js new file mode 100644 index 0000000..95da8fa --- /dev/null +++ b/lib/Tesseract.2015.07.15.v.0.js @@ -0,0 +1,130 @@ +var Tesseract = (function(){ + + var Tesseract = {} + + //https://cdn.rawgit.com/naptha/tesseract.js/master/worker/worker.js + //https://rawgit.com/naptha/tesseract.js/master/worker/worker.js for testing + //https://cdn.rawgit.com/naptha/tesseract.js/master/worker/worker.js + + var blob = new Blob(["importScripts('__tesseractjs__');"]); // changed on build + // console.log('localhost') + var worker = new Worker(window.URL.createObjectURL(blob)); + worker.postMessage({init: {mem: 16777216*6}}) + var bigworker = false + + var index = 0 + var handlers = [] + + worker.onmessage = function(e){ + 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) + } + } + + function convertToImageData(image){ + if(image.getContext){ + 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(typeof progress === "undefined"){ + progress = callback = new Function() + } + + if (typeof callback === "undefined"){ + callback = progress + progress = new Function() + } + + var i = index++ + + 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}) + }) + + } + + Tesseract.recognize = function(image, options, callback){ + var lang = options.lang + if (typeof lang === "undefined"){ + lang = 'eng' + } + + if (!bigworker && ['chi_sim', 'chi_tra', 'jpn'].indexOf(lang) != -1){ + worker.postMessage({init: {mem: 16777216*10}}) + bigworker = true + console.log('started big worker') + } + + if (typeof options === 'string') { + lang = options + options = {} + } + + if (typeof options === "function") { + callback = options + options = {} + } + + image = convertToImageData(image) + + var i = index++ + + handlers[i] = { + resolve: new Function(), + reject: new Function() + } + handlers[i].callback = callback || new Function() + handlers[i].progress = (function(){ + if(typeof options.progress === 'function'){ + var p = options.progress + delete options.progress + return p + } + 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 +})() diff --git a/lib/Tesseract.js b/lib/Tesseract.js index ddf43b9..95da8fa 100644 --- a/lib/Tesseract.js +++ b/lib/Tesseract.js @@ -4,8 +4,9 @@ var Tesseract = (function(){ //https://cdn.rawgit.com/naptha/tesseract.js/master/worker/worker.js //https://rawgit.com/naptha/tesseract.js/master/worker/worker.js for testing + //https://cdn.rawgit.com/naptha/tesseract.js/master/worker/worker.js - var blob = new Blob(["importScripts('https://cdn.rawgit.com/naptha/tesseract.js/master/worker/worker.js');"]); + var blob = new Blob(["importScripts('__tesseractjs__');"]); // changed on build // console.log('localhost') var worker = new Worker(window.URL.createObjectURL(blob)); worker.postMessage({init: {mem: 16777216*6}}) @@ -126,4 +127,4 @@ var Tesseract = (function(){ } return Tesseract -})() \ No newline at end of file +})() diff --git a/src/Tesseract.js b/src/Tesseract.js new file mode 100644 index 0000000..5320355 --- /dev/null +++ b/src/Tesseract.js @@ -0,0 +1,130 @@ +var Tesseract = (function(){ + + var Tesseract = {} + + //https://cdn.rawgit.com/naptha/tesseract.js/master/worker/worker.js + //https://rawgit.com/naptha/tesseract.js/master/worker/worker.js for testing + //https://cdn.rawgit.com/naptha/tesseract.js/master/worker/worker.js + + var blob = new Blob(["importScripts('__tesseractjs__');"]); // changed on build + // console.log('localhost') + var worker = new Worker(window.URL.createObjectURL(blob)); + worker.postMessage({init: {mem: 16777216*6}}) + var bigworker = false + + var index = 0 + var handlers = [] + + worker.onmessage = function(e){ + 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) + } + } + + function convertToImageData(image){ + if(image.getContext){ + 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(typeof progress === "undefined"){ + progress = callback = new Function() + } + + if (typeof callback === "undefined"){ + callback = progress + progress = new Function() + } + + var i = index++ + + 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}) + }) + + } + + Tesseract.recognize = function(image, options, callback){ + var lang = options.lang + if (typeof lang === "undefined"){ + lang = 'eng' + } + + if (!bigworker && ['chi_sim', 'chi_tra', 'jpn'].indexOf(lang) != -1){ + worker.postMessage({init: {mem: 16777216*10}}) + bigworker = true + console.log('started big worker') + } + + if (typeof options === 'string') { + lang = options + options = {} + } + + if (typeof options === "function") { + callback = options + options = {} + } + + image = convertToImageData(image) + + var i = index++ + + handlers[i] = { + resolve: new Function(), + reject: new Function() + } + handlers[i].callback = callback || new Function() + handlers[i].progress = (function(){ + if(typeof options.progress === 'function'){ + var p = options.progress + delete options.progress + return p + } + 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 +})() \ No newline at end of file diff --git a/worker_src/worker.js b/src/worker.js similarity index 100% rename from worker_src/worker.js rename to src/worker.js