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