Browse Source

Minor refactor

pull/265/head
Jerome Wu 6 years ago
parent
commit
b843f08705
  1. 6
      src/common/TesseractJob.js
  2. 89
      src/common/TesseractWorker.js
  3. 30
      src/common/desaturate.js
  4. 90
      src/index.js

6
src/common/job.js → src/common/TesseractJob.js

@ -2,7 +2,7 @@ const adapter = require('../node/');
let jobCounter = 0; let jobCounter = 0;
module.exports = class TesseractJob { class TesseractJob {
constructor(instance) { constructor(instance) {
jobCounter += 1; jobCounter += 1;
this.id = `Job-${jobCounter}-${Math.random().toString(16).slice(3, 8)}`; this.id = `Job-${jobCounter}-${Math.random().toString(16).slice(3, 8)}`;
@ -83,4 +83,6 @@ module.exports = class TesseractJob {
this._finally.forEach(fn => fn(data)); this._finally.forEach(fn => fn(data));
} }
} }
}; }
module.exports = TesseractJob;

89
src/common/TesseractWorker.js

@ -0,0 +1,89 @@
const check = require('check-types');
const adapter = require('../node');
const circularize = require('./circularize');
const TesseractJob = require('./TesseractJob');
class TesseractWorker {
constructor(workerOptions = {}) {
this.worker = null;
this.workerOptions = Object.assign({}, adapter.defaultOptions, workerOptions);
this._currentJob = null;
this._queue = [];
}
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,
},
);
});
}
terminate() {
if (this.worker) {
adapter.terminateWorker(this);
}
this.worker = null;
this._currentJob = null;
this._queue = [];
}
_delay(fn) {
if (check.null(this.worker)) {
this.worker = adapter.spawnWorker(this, this.workerOptions);
}
const job = new TesseractJob(this);
this._queue.push(() => {
this._queue.shift();
this._currentJob = job;
fn(job);
});
if (check.null(this._currentJob)) {
this._dequeue();
}
return job;
}
_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;

30
src/common/desaturate.js

@ -1,30 +0,0 @@
/* eslint-disable no-bitwise */
/* eslint-disable max-len */
// This converts an image to grayscale
module.exports = (image) => {
if (image.data) {
const src = image.data;
const { width, height } = image;
const dst = new Uint8Array(width * height);
const srcLength = src.length | 0;
const srcLength16 = (srcLength - 16) | 0;
let i = 0;
let j = 0;
for (; i <= srcLength16; i += 16, j += 4) {
// convert to grayscale 4 pixels at a time; eveything with alpha gets put in front of 50% gray
dst[j] = (((src[i] * 77 + src[i + 1] * 151 + src[i + 2] * 28) * src[i + 3]) + ((255 - src[i + 3]) << 15) + 32768) >> 16;
dst[j + 1] = (((src[i + 4] * 77 + src[i + 5] * 151 + src[i + 6] * 28) * src[i + 7]) + ((255 - src[i + 7]) << 15) + 32768) >> 16;
dst[j + 2] = (((src[i + 8] * 77 + src[i + 9] * 151 + src[i + 10] * 28) * src[i + 11]) + ((255 - src[i + 11]) << 15) + 32768) >> 16;
dst[j + 3] = (((src[i + 12] * 77 + src[i + 13] * 151 + src[i + 14] * 28) * src[i + 15]) + ((255 - src[i + 15]) << 15) + 32768) >> 16;
}
// finish up
for (; i < srcLength; i += 4, j += 1) {
dst[j] = (((src[i] * 77 + src[i + 1] * 151 + src[i + 2] * 28) * src[i + 3]) + ((255 - src[i + 3]) << 15) + 32768) >> 16;
}
return dst;
}
return null;
// throw { err: 'Invalid ImageData' };
};

90
src/index.js

@ -1,91 +1,7 @@
const check = require('check-types'); const utils = require('tesseract.js-utils');
const adapter = require('./node'); const TesseractWorker = require('./common/TesseractWorker');
const circularize = require('./common/circularize');
const TesseractJob = require('./common/job');
class TesseractWorker {
constructor(workerOptions = {}) {
this.worker = null;
this.workerOptions = Object.assign({}, adapter.defaultOptions, workerOptions);
this._currentJob = null;
this._queue = [];
}
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,
},
);
});
}
terminate() {
if (this.worker) {
adapter.terminateWorker(this);
}
this.worker = null;
this._currentJob = null;
this._queue = [];
}
_delay(fn) {
if (check.null(this.worker)) {
this.worker = adapter.spawnWorker(this, this.workerOptions);
}
const job = new TesseractJob(this);
this._queue.push(() => {
this._queue.shift();
this._currentJob = job;
fn(job);
});
if (check.null(this._currentJob)) {
this._dequeue();
}
return job;
}
_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 = { module.exports = {
TesseractWorker, TesseractWorker,
utils,
}; };

Loading…
Cancel
Save