You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
1.7 KiB
73 lines
1.7 KiB
const adapter = require('./node/index.js') |
|
const circularize = require('./common/circularize.js') |
|
const TesseractJob = require('./common/job'); |
|
const version = require('../package.json').version; |
|
|
|
function create(workerOptions = {}){ |
|
var worker = new TesseractWorker(Object.assign({}, adapter.defaultOptions, workerOptions)); |
|
worker.create = create; |
|
worker.version = version; |
|
return worker; |
|
} |
|
|
|
class TesseractWorker { |
|
constructor(workerOptions){ |
|
this.worker = null; |
|
this.workerOptions = 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 }); |
|
}) |
|
} |
|
detect(image, options = {}){ |
|
return this._delay(job => { |
|
job._send('detect', { image, options, workerOptions: this.workerOptions }); |
|
}) |
|
} |
|
|
|
terminate(){ |
|
if(this.worker) adapter.terminateWorker(this); |
|
this.worker = null; |
|
} |
|
|
|
_delay(fn){ |
|
if(!this.worker) this.worker = adapter.spawnWorker(this, this.workerOptions); |
|
|
|
var job = new TesseractJob(this); |
|
this._queue.push(e => { |
|
this._queue.shift(); |
|
this._currentJob = job; |
|
fn(job); |
|
}); |
|
if(!this._currentJob) this._dequeue(); |
|
return job; |
|
} |
|
|
|
_dequeue(){ |
|
this._currentJob = null; |
|
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) |
|
} else { |
|
console.warn('Job ID ' + packet.jobId + ' not known.') |
|
} |
|
} |
|
} |
|
|
|
module.exports = create(); |