Jerome Wu
6 years ago
4 changed files with 96 additions and 119 deletions
@ -0,0 +1,89 @@
@@ -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; |
@ -1,30 +0,0 @@
@@ -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' };
|
||||
}; |
@ -1,91 +1,7 @@
@@ -1,91 +1,7 @@
|
||||
const check = require('check-types'); |
||||
const adapter = require('./node'); |
||||
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.`); |
||||
} |
||||
} |
||||
} |
||||
const utils = require('tesseract.js-utils'); |
||||
const TesseractWorker = require('./common/TesseractWorker'); |
||||
|
||||
module.exports = { |
||||
TesseractWorker, |
||||
utils, |
||||
}; |
||||
|
Loading…
Reference in new issue