Jerome Wu
6 years ago
6 changed files with 1578 additions and 142 deletions
@ -0,0 +1,11 @@
@@ -0,0 +1,11 @@
|
||||
{ |
||||
"extends": "airbnb", |
||||
"env": { |
||||
"browser": true, |
||||
"node": true |
||||
}, |
||||
"rules": { |
||||
"no-underscore-dangle": 0, |
||||
"no-console": 0 |
||||
} |
||||
} |
@ -1,15 +1,18 @@
@@ -1,15 +1,18 @@
|
||||
// replace this with require('tesseract.js')
|
||||
var Tesseract = require('../../'), |
||||
image = require('path').resolve(__dirname, 'cosmic.png'); |
||||
const path = require('path'); |
||||
const { TesseractWorker } = require('../../'); |
||||
|
||||
Tesseract.recognize(image) |
||||
.then(data => { |
||||
console.log('then\n', data.text) |
||||
const image = path.resolve(__dirname, 'cosmic.png'); |
||||
const tessWorker = new TesseractWorker(); |
||||
|
||||
tessWorker.recognize(image) |
||||
.then((data) => { |
||||
console.log('then\n', data.text); |
||||
}) |
||||
.catch(err => { |
||||
.catch((err) => { |
||||
console.log('catch\n', err); |
||||
}) |
||||
.finally(e => { |
||||
.finally(() => { |
||||
console.log('finally\n'); |
||||
process.exit(); |
||||
}); |
@ -1,12 +1,15 @@
@@ -1,12 +1,15 @@
|
||||
// replace this with require('tesseract.js')
|
||||
var Tesseract = require('../../'), |
||||
image = require('path').resolve(__dirname, 'cosmic.png'); |
||||
const path = require('path'); |
||||
const { TesseractWorker } = require('../../'); |
||||
|
||||
Tesseract.detect(image) |
||||
.progress(function(info){ |
||||
const image = path.resolve(__dirname, 'cosmic.png'); |
||||
const tessWorker = new TesseractWorker(); |
||||
|
||||
tessWorker.detect(image) |
||||
.progress((info) => { |
||||
console.log(info); |
||||
}) |
||||
.then(function(data){ |
||||
.then((data) => { |
||||
console.log('done', data); |
||||
process.exit(); |
||||
}) |
||||
}); |
||||
|
@ -1,75 +1,91 @@
@@ -1,75 +1,91 @@
|
||||
const adapter = require('./node/index.js') |
||||
const circularize = require('./common/circularize.js') |
||||
const check = require('check-types'); |
||||
const adapter = require('./node/index.js'); |
||||
const circularize = require('./common/circularize.js'); |
||||
const TesseractJob = require('./common/job'); |
||||
const version = require('../package.json').version; |
||||
|
||||
const create = function(workerOptions = {}){ |
||||
var worker = new TesseractWorker(Object.assign({}, adapter.defaultOptions, workerOptions)); |
||||
worker.create = create; |
||||
worker.version = version; |
||||
return worker; |
||||
} |
||||
|
||||
class TesseractWorker { |
||||
constructor(workerOptions){ |
||||
constructor(workerOptions = {}) { |
||||
this.worker = null; |
||||
this.workerOptions = workerOptions; |
||||
this.workerOptions = Object.assign({}, adapter.defaultOptions, workerOptions); |
||||
this._currentJob = null; |
||||
this._queue = []; |
||||
} |
||||
|
||||
recognize(image, options = {}){ |
||||
return this._delay(job => { |
||||
if (typeof options === 'string') options = {lang: options} |
||||
options.lang = options.lang || 'eng'; |
||||
|
||||
job._send('recognize', { image, options, workerOptions: this.workerOptions }); |
||||
}) |
||||
recognize(image, options = { lang: 'eng' }) { |
||||
return this._delay((job) => { |
||||
job._send( |
||||
'recognize', |
||||
{ |
||||
image, |
||||
options: check.string(options) |
||||
? { lang: options || 'eng' } |
||||
: options, |
||||
workerOptions: this.workerOptions, |
||||
}, |
||||
); |
||||
}); |
||||
} |
||||
detect(image, options = {}){ |
||||
return this._delay(job => { |
||||
job._send('detect', { image, options, workerOptions: this.workerOptions }); |
||||
}) |
||||
|
||||
detect(image, options = {}) { |
||||
return this._delay((job) => { |
||||
job._send( |
||||
'detect', |
||||
{ |
||||
image, |
||||
options, |
||||
workerOptions: this.workerOptions, |
||||
}, |
||||
); |
||||
}); |
||||
} |
||||
|
||||
terminate(){ |
||||
if(this.worker) adapter.terminateWorker(this); |
||||
terminate() { |
||||
if (this.worker) { |
||||
adapter.terminateWorker(this); |
||||
} |
||||
this.worker = null; |
||||
this._currentJob = null; |
||||
this._queue = []; |
||||
} |
||||
|
||||
_delay(fn){ |
||||
if(!this.worker) this.worker = adapter.spawnWorker(this, this.workerOptions); |
||||
_delay(fn) { |
||||
if (check.null(this.worker)) { |
||||
this.worker = adapter.spawnWorker(this, this.workerOptions); |
||||
} |
||||
|
||||
var job = new TesseractJob(this); |
||||
this._queue.push(e => { |
||||
const job = new TesseractJob(this); |
||||
this._queue.push(() => { |
||||
this._queue.shift(); |
||||
this._currentJob = job; |
||||
fn(job); |
||||
}); |
||||
if(!this._currentJob) this._dequeue(); |
||||
if (check.null(this._currentJob)) { |
||||
this._dequeue(); |
||||
} |
||||
return job; |
||||
} |
||||
|
||||
_dequeue(){ |
||||
_dequeue() { |
||||
this._currentJob = null; |
||||
if(this._queue.length){ |
||||
if (this._queue.length) { |
||||
this._queue[0](); |
||||
} |
||||
} |
||||
|
||||
_recv(packet){ |
||||
if(packet.status === 'resolve' && packet.action === 'recognize'){ |
||||
packet.data = circularize(packet.data); |
||||
} |
||||
|
||||
if(this._currentJob.id === packet.jobId){ |
||||
this._currentJob._handle(packet) |
||||
_recv(packet) { |
||||
if (this._currentJob.id === packet.jobId) { |
||||
this._currentJob._handle({ |
||||
data: packet.status === 'resolve' && packet.action === 'recognize' |
||||
? circularize(packet.data) |
||||
: packet.data, |
||||
...packet, |
||||
}); |
||||
} else { |
||||
console.warn('Job ID ' + packet.jobId + ' not known.') |
||||
console.warn(`Job ID ${packet.jobId} not known.`); |
||||
} |
||||
} |
||||
} |
||||
|
||||
module.exports = create(); |
||||
module.exports = { |
||||
TesseractWorker, |
||||
}; |
||||
|
Loading…
Reference in new issue