Browse Source

Add jsdoc to TesseractWorker

pull/265/head
Jerome Wu 7 years ago
parent
commit
a4fdded574
  1. 2
      src/browser/index.js
  2. 13
      src/common/TesseractJob.js
  3. 127
      src/common/TesseractWorker.js
  4. 2
      src/node/index.js

2
src/browser/index.js

@ -38,7 +38,7 @@ exports.spawnWorker = (instance, workerOptions) => { @@ -38,7 +38,7 @@ exports.spawnWorker = (instance, workerOptions) => {
}
worker.onmessage = ({ data }) => {
instance._recv(data);
instance.recv(data);
};
return worker;

13
src/common/TesseractJob.js

@ -1,3 +1,12 @@ @@ -1,3 +1,12 @@
/**
*
* The job exectued by worker, each job is basically a recognition of an image.
*
* @fileoverview Job excuted by Worker
* @author Kevin Kwok <antimatter15@gmail.com>
* @author Guillermo Webster <gui@mit.edu>
* @author Jerome Wu <jeromewus@gmail.com>
*/
const adapter = require('../node/');
let jobCounter = 0;
@ -44,7 +53,7 @@ class TesseractJob { @@ -44,7 +53,7 @@ class TesseractJob {
return this;
}
_send(action, payload) {
send(action, payload) {
adapter.sendPacket(this._instance, {
jobId: this.id,
action,
@ -52,7 +61,7 @@ class TesseractJob { @@ -52,7 +61,7 @@ class TesseractJob {
});
}
_handle(packet) {
handle(packet) {
const { data } = packet;
let runFinallyCbs = false;

127
src/common/TesseractWorker.js

@ -54,34 +54,64 @@ class TesseractWorker { @@ -54,34 +54,64 @@ class TesseractWorker {
this._queue = [];
}
/**
* recognize
*
* @name recognize
* @function recognize text in given image
* @access public
* @param {Buffer, string} image - image to be recognized
* @param {string} [lang=eng] - language to recognize
* @param {object} params - tesseract parameters
*
*/
recognize(image, lang = 'eng', params = {}) {
return this._delay((job) => {
job._send(
'recognize',
{
image,
lang,
params,
options: this.options,
},
);
});
return this._sendJob('recognize', image, lang, params);
}
/**
* detect
*
* @name detect
* @function detect language of the text in the image
* @access public
* @param {Buffer, string} image - image to be recognized
* @param {object} params - tesseract parameters
*
*/
detect(image, params = {}) {
return this._delay((job) => {
job._send(
'detect',
{
image,
lang: 'osd',
params,
options: this.options,
},
);
});
return this._sendJob('detect', image, 'osd', params);
}
/**
* recv
*
* @name recv
* @function handle completed job
* @access public
* @param {object} packet job data
*/
recv(packet) {
if (this._currentJob.id === packet.jobId) {
this._currentJob.handle({
...packet,
data: packet.status === 'resolve' && packet.action === 'recognize'
? circularize(packet.data)
: packet.data,
});
} else {
console.warn(`Job ID ${packet.jobId} not known.`);
}
}
/**
* terminate
*
* @name terminate
* @function terminate the worker
* @access public
*
*/
terminate() {
if (this.worker) {
adapter.terminateWorker(this);
@ -91,6 +121,39 @@ class TesseractWorker { @@ -91,6 +121,39 @@ class TesseractWorker {
this._queue = [];
}
/**
* _sendJob
*
* @name _sendJob
* @function append a new job to the job queue
* @access private
* @param {string} type job type, should be recognize or detect
* @param {Buffer, string} image image to recognize
* @param {string} lang language to recognize
* @param {object} params tesseract parameters
*/
_sendJob(type, image, lang, params) {
return this._delay((job) => {
job.send(
type,
{
image,
lang,
params,
options: this.options,
},
);
});
}
/**
* _delay
*
* @name _delay
* @function delays the fn to execute until it is on the rear of the queue
* @access private
* @param {function} fn A handler function for the job
*/
_delay(fn) {
if (check.null(this.worker)) {
this.worker = adapter.spawnWorker(this, this.options);
@ -108,25 +171,19 @@ class TesseractWorker { @@ -108,25 +171,19 @@ class TesseractWorker {
return job;
}
/**
* _dequeue
*
* @name _dequeue
* @function dequeue and execute the rear job
* @access private
*/
_dequeue() {
this._currentJob = null;
if (this._queue.length) {
this._queue[0]();
}
}
_recv(packet) {
if (this._currentJob.id === packet.jobId) {
this._currentJob._handle({
...packet,
data: packet.status === 'resolve' && packet.action === 'recognize'
? circularize(packet.data)
: packet.data,
});
} else {
console.warn(`Job ID ${packet.jobId} not known.`);
}
}
}
module.exports = TesseractWorker;

2
src/node/index.js

@ -24,7 +24,7 @@ exports.defaultOptions = { @@ -24,7 +24,7 @@ exports.defaultOptions = {
exports.spawnWorker = (instance, { workerPath }) => {
const cp = fork(workerPath);
cp.on('message', (packet) => {
instance._recv(packet);
instance.recv(packet);
});
return cp;
};

Loading…
Cancel
Save