Browse Source

Add eslint & rewrite src/index.js

pull/265/head
Jerome Wu 6 years ago
parent
commit
1e80e42a6f
  1. 11
      .eslintrc
  2. 29
      examples/node/basic.js
  3. 23
      examples/node/detect.js
  4. 1507
      package-lock.json
  5. 8
      package.json
  6. 142
      src/index.js

11
.eslintrc

@ -0,0 +1,11 @@ @@ -0,0 +1,11 @@
{
"extends": "airbnb",
"env": {
"browser": true,
"node": true
},
"rules": {
"no-underscore-dangle": 0,
"no-console": 0
}
}

29
examples/node/basic.js

@ -1,15 +1,18 @@ @@ -1,15 +1,18 @@
// replace this with require('tesseract.js')
var Tesseract = require('../../'),
image = require('path').resolve(__dirname, 'cosmic.png');
const path = require('path');
const { TesseractWorker } = require('../../');
Tesseract.recognize(image)
.then(data => {
console.log('then\n', data.text)
})
.catch(err => {
console.log('catch\n', err);
})
.finally(e => {
console.log('finally\n');
process.exit();
});
const image = path.resolve(__dirname, 'cosmic.png');
const tessWorker = new TesseractWorker();
tessWorker.recognize(image)
.then((data) => {
console.log('then\n', data.text);
})
.catch((err) => {
console.log('catch\n', err);
})
.finally(() => {
console.log('finally\n');
process.exit();
});

23
examples/node/detect.js

@ -1,12 +1,15 @@ @@ -1,12 +1,15 @@
// replace this with require('tesseract.js')
var Tesseract = require('../../'),
image = require('path').resolve(__dirname, 'cosmic.png');
const path = require('path');
const { TesseractWorker } = require('../../');
Tesseract.detect(image)
.progress(function(info){
console.log(info);
})
.then(function(data){
console.log('done', data);
process.exit();
})
const image = path.resolve(__dirname, 'cosmic.png');
const tessWorker = new TesseractWorker();
tessWorker.detect(image)
.progress((info) => {
console.log(info);
})
.then((data) => {
console.log('done', data);
process.exit();
});

1507
package-lock.json generated

File diff suppressed because it is too large Load Diff

8
package.json

@ -22,15 +22,21 @@ @@ -22,15 +22,21 @@
"browserify": "^13.1.0",
"concurrently": "^3.1.0",
"envify": "^3.4.1",
"eslint": "^5.9.0",
"eslint-config-airbnb": "^17.1.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-jsx-a11y": "^6.1.2",
"eslint-plugin-react": "^7.11.1",
"http-server": "^0.9.0",
"pako": "^1.0.3",
"uglify-js": "^3.4.9",
"watchify": "^3.7.0"
},
"dependencies": {
"check-types": "^7.4.0",
"file-type": "^3.8.0",
"isomorphic-fetch": "^2.2.1",
"is-url": "1.2.2",
"isomorphic-fetch": "^2.2.1",
"jpeg-js": "^0.2.0",
"level-js": "^2.2.4",
"node-fetch": "^1.6.3",

142
src/index.js

@ -1,75 +1,91 @@ @@ -1,75 +1,91 @@
const adapter = require('./node/index.js')
const circularize = require('./common/circularize.js')
const check = require('check-types');
const adapter = require('./node/index.js');
const circularize = require('./common/circularize.js');
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 {
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';
constructor(workerOptions = {}) {
this.worker = null;
this.workerOptions = Object.assign({}, adapter.defaultOptions, workerOptions);
this._currentJob = null;
this._queue = [];
}
job._send('recognize', { image, options, workerOptions: this.workerOptions });
})
}
detect(image, options = {}){
return this._delay(job => {
job._send('detect', { image, options, workerOptions: this.workerOptions });
})
}
recognize(image, options = { lang: 'eng' }) {
return this._delay((job) => {
job._send(
'recognize',
{
image,
options: check.string(options)
? { lang: options || 'eng' }
: options,
workerOptions: this.workerOptions,
},
);
});
}
terminate(){
if(this.worker) adapter.terminateWorker(this);
this.worker = null;
this._currentJob = null;
this._queue = [];
}
detect(image, options = {}) {
return this._delay((job) => {
job._send(
'detect',
{
image,
options,
workerOptions: this.workerOptions,
},
);
});
}
_delay(fn){
if(!this.worker) this.worker = adapter.spawnWorker(this, this.workerOptions);
terminate() {
if (this.worker) {
adapter.terminateWorker(this);
}
this.worker = null;
this._currentJob = null;
this._queue = [];
}
var job = new TesseractJob(this);
this._queue.push(e => {
this._queue.shift();
this._currentJob = job;
fn(job);
});
if(!this._currentJob) this._dequeue();
return job;
}
_delay(fn) {
if (check.null(this.worker)) {
this.worker = adapter.spawnWorker(this, this.workerOptions);
}
_dequeue(){
this._currentJob = null;
if(this._queue.length){
this._queue[0]();
}
}
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;
}
_recv(packet){
if(packet.status === 'resolve' && packet.action === 'recognize'){
packet.data = circularize(packet.data);
}
_dequeue() {
this._currentJob = null;
if (this._queue.length) {
this._queue[0]();
}
}
if(this._currentJob.id === packet.jobId){
this._currentJob._handle(packet)
} else {
console.warn('Job ID ' + packet.jobId + ' not known.')
}
}
_recv(packet) {
if (this._currentJob.id === packet.jobId) {
this._currentJob._handle({
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…
Cancel
Save