You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
272 lines
7.2 KiB
272 lines
7.2 KiB
6 years ago
|
/**
|
||
|
*
|
||
6 years ago
|
* Worker script for browser and node
|
||
6 years ago
|
*
|
||
6 years ago
|
* @fileoverview Worker script for browser and node
|
||
6 years ago
|
* @author Kevin Kwok <antimatter15@gmail.com>
|
||
|
* @author Guillermo Webster <gui@mit.edu>
|
||
|
* @author Jerome Wu <jeromewus@gmail.com>
|
||
|
*/
|
||
6 years ago
|
const { loadLang } = require('tesseract.js-utils');
|
||
6 years ago
|
const dump = require('./utils/dump');
|
||
|
const isBrowser = require('../utils/getEnvironment')('type') === 'browser';
|
||
|
const setImage = require('./utils/setImage');
|
||
|
const getFiles = require('./utils/getFiles');
|
||
|
const defaultParams = require('./constants/defaultParams');
|
||
6 years ago
|
|
||
6 years ago
|
/*
|
||
|
* Tesseract Module returned by TesseractCore.
|
||
|
*/
|
||
7 years ago
|
let TessModule;
|
||
6 years ago
|
/*
|
||
|
* TessearctBaseAPI instance
|
||
|
*/
|
||
7 years ago
|
let api;
|
||
7 years ago
|
let latestJob;
|
||
|
let adapter = {};
|
||
6 years ago
|
let params = defaultParams;
|
||
9 years ago
|
|
||
6 years ago
|
|
||
6 years ago
|
/**
|
||
|
* handleInit
|
||
|
*
|
||
|
* @name handleInit
|
||
|
* @function handle initialization of TessModule
|
||
|
* @access public
|
||
|
* @param {object} req - job payload
|
||
|
* @param {string} req.corePath - path to the tesseract-core.js
|
||
|
* @param {object} res - job instance
|
||
|
* @returns {Promise} A Promise for callback
|
||
|
*/
|
||
6 years ago
|
const load = ({ workerId, jobId, payload: { options: { corePath } } }, res) => {
|
||
7 years ago
|
if (!TessModule) {
|
||
|
const Core = adapter.getCore(corePath, res);
|
||
8 years ago
|
|
||
6 years ago
|
res.progress({ workerId, status: 'initializing tesseract', progress: 0 });
|
||
9 years ago
|
|
||
6 years ago
|
Core({
|
||
7 years ago
|
TesseractProgress(percent) {
|
||
6 years ago
|
latestJob.progress({
|
||
|
workerId,
|
||
|
jobId,
|
||
|
status: 'recognizing text',
|
||
|
progress: Math.max(0, (percent - 30) / 70),
|
||
|
});
|
||
7 years ago
|
},
|
||
|
})
|
||
7 years ago
|
.then((tessModule) => {
|
||
|
TessModule = tessModule;
|
||
6 years ago
|
res.progress({ workerId, status: 'initialized tesseract', progress: 1 });
|
||
|
res.resolve({ loaded: true });
|
||
7 years ago
|
});
|
||
6 years ago
|
} else {
|
||
|
res.resolve({ loaded: true });
|
||
7 years ago
|
}
|
||
|
};
|
||
9 years ago
|
|
||
6 years ago
|
/**
|
||
|
* loadLanguage
|
||
|
*
|
||
|
* @name loadLanguage
|
||
|
* @function load language from remote or local cache
|
||
|
* @access public
|
||
|
* @param {object} req - job payload
|
||
6 years ago
|
* @param {string} req.langs - languages to load, ex: eng, eng+chi_tra
|
||
6 years ago
|
* @param {object} req.options - other options for loadLang function
|
||
|
* @param {object} res - job instance
|
||
|
* @returns {Promise} A Promise for callback
|
||
|
*/
|
||
6 years ago
|
const loadLanguage = ({ workerId, payload: { langs, options } }, res) => {
|
||
|
res.progress({ workerId, status: 'loading language traineddata', progress: 0 });
|
||
|
loadLang({ langs, TessModule, ...options }).then(() => {
|
||
|
res.progress({ workerId, status: 'loaded language traineddata', progress: 1 });
|
||
|
res.resolve(langs);
|
||
6 years ago
|
}).catch((err) => {
|
||
|
if (isBrowser && err instanceof DOMException) {
|
||
6 years ago
|
/*
|
||
|
* For some reason google chrome throw DOMException in loadLang,
|
||
|
* while other browser is OK, for now we ignore this exception
|
||
|
* and hopefully to find the root cause one day.
|
||
|
*/
|
||
|
} else {
|
||
6 years ago
|
res.reject(err.toString());
|
||
6 years ago
|
}
|
||
7 years ago
|
});
|
||
|
};
|
||
9 years ago
|
|
||
6 years ago
|
const setParameters = ({ payload: { params: _params } }, res) => {
|
||
|
Object.keys(_params)
|
||
|
.filter(k => !k.startsWith('tessjs_'))
|
||
|
.forEach((key) => {
|
||
|
api.SetVariable(key, _params[key]);
|
||
|
});
|
||
|
params = { ...params, ..._params };
|
||
|
|
||
|
if (typeof res !== 'undefined') {
|
||
|
res.resolve(params);
|
||
|
}
|
||
|
};
|
||
|
|
||
6 years ago
|
const initialize = ({
|
||
|
workerId,
|
||
|
jobId,
|
||
6 years ago
|
payload: { langs: _langs, oem },
|
||
6 years ago
|
}, res) => {
|
||
6 years ago
|
const langs = (typeof _langs === 'string')
|
||
|
? _langs
|
||
|
: _langs.map(l => ((typeof l === 'string') ? l : l.data)).join('+');
|
||
6 years ago
|
|
||
6 years ago
|
try {
|
||
|
res.progress({
|
||
|
workerId, jobId, status: 'initializing api', progress: 0,
|
||
|
});
|
||
|
api = new TessModule.TessBaseAPI();
|
||
|
api.Init(null, langs, oem);
|
||
|
setParameters({ payload: { params } });
|
||
|
res.progress({
|
||
|
workerId, jobId, status: 'initialized api', progress: 1,
|
||
|
});
|
||
|
res.resolve();
|
||
|
} catch (err) {
|
||
|
res.reject(err.toString());
|
||
6 years ago
|
}
|
||
|
};
|
||
|
|
||
6 years ago
|
/**
|
||
|
* handleRecognize
|
||
|
*
|
||
|
* @name handleRecognize
|
||
|
* @function handle recognition job
|
||
|
* @access public
|
||
|
* @param {object} req - job payload
|
||
|
* @param {array} req.image - binary image in array format
|
||
6 years ago
|
* @param {string} req.langs - languages to load, ex: eng, eng+chi_tra
|
||
6 years ago
|
* @param {object} req.options - other options for loadLang function
|
||
|
* @param {object} req.params - parameters for tesseract
|
||
|
* @param {object} res - job instance
|
||
|
*/
|
||
6 years ago
|
const recognize = ({ payload: { image } }, res) => {
|
||
|
try {
|
||
6 years ago
|
const ptr = setImage(TessModule, api, image, params);
|
||
6 years ago
|
api.Recognize(null);
|
||
|
res.resolve({
|
||
6 years ago
|
files: getFiles(TessModule, api, adapter, params),
|
||
|
...dump(TessModule, api, params),
|
||
6 years ago
|
});
|
||
|
TessModule._free(ptr);
|
||
|
} catch (err) {
|
||
|
res.reject(err.toString());
|
||
6 years ago
|
}
|
||
|
};
|
||
7 years ago
|
|
||
6 years ago
|
/**
|
||
|
* handleDetect
|
||
|
*
|
||
|
* @name handleDetect
|
||
|
* @function handle detect (Orientation and Script Detection / OSD) job
|
||
|
* @access public
|
||
|
* @param {object} req - job payload
|
||
|
* @param {array} req.image - binary image in array format
|
||
6 years ago
|
* @param {string} req.langs - languages to load, ex: eng, eng+chi_tra
|
||
6 years ago
|
* @param {object} req.options - other options for loadLang function
|
||
|
* @param {object} res - job instance
|
||
|
*/
|
||
6 years ago
|
const detect = ({ payload: { image } }, res) => {
|
||
|
try {
|
||
6 years ago
|
const ptr = setImage(TessModule, api, image, params);
|
||
6 years ago
|
const results = new TessModule.OSResults();
|
||
7 years ago
|
|
||
6 years ago
|
if (!api.DetectOS(results)) {
|
||
|
api.End();
|
||
|
TessModule._free(ptr);
|
||
|
res.reject('Failed to detect OS');
|
||
|
} else {
|
||
|
const best = results.best_result;
|
||
|
const oid = best.orientation_id;
|
||
|
const sid = best.script_id;
|
||
7 years ago
|
|
||
6 years ago
|
TessModule._free(ptr);
|
||
7 years ago
|
|
||
6 years ago
|
res.resolve({
|
||
|
tesseract_script_id: sid,
|
||
|
script: results.unicharset.get_script_from_script_id(sid),
|
||
|
script_confidence: best.sconfidence,
|
||
|
orientation_degrees: [0, 270, 180, 90][oid],
|
||
|
orientation_confidence: best.oconfidence,
|
||
|
});
|
||
|
}
|
||
|
} catch (err) {
|
||
|
res.reject(err.toString());
|
||
|
}
|
||
|
};
|
||
7 years ago
|
|
||
6 years ago
|
const terminate = (_, res) => {
|
||
|
try {
|
||
|
api.End();
|
||
|
res.resolve({ terminated: true });
|
||
|
} catch (err) {
|
||
|
res.reject(err.toString());
|
||
|
}
|
||
|
};
|
||
|
|
||
6 years ago
|
/**
|
||
|
* dispatchHandlers
|
||
|
*
|
||
|
* @name dispatchHandlers
|
||
|
* @function worker data handler
|
||
|
* @access public
|
||
|
* @param {object} data
|
||
|
* @param {string} data.jobId - unique job id
|
||
|
* @param {string} data.action - action of the job, only recognize and detect for now
|
||
|
* @param {object} data.payload - data for the job
|
||
|
* @param {function} send - trigger job to work
|
||
|
*/
|
||
6 years ago
|
exports.dispatchHandlers = (packet, send) => {
|
||
6 years ago
|
const res = (status, data) => {
|
||
7 years ago
|
send({
|
||
6 years ago
|
...packet,
|
||
7 years ago
|
status,
|
||
|
data,
|
||
|
});
|
||
|
};
|
||
6 years ago
|
res.resolve = res.bind(this, 'resolve');
|
||
|
res.reject = res.bind(this, 'reject');
|
||
|
res.progress = res.bind(this, 'progress');
|
||
7 years ago
|
|
||
6 years ago
|
latestJob = res;
|
||
7 years ago
|
|
||
|
try {
|
||
6 years ago
|
const { action } = packet;
|
||
|
if (action === 'load') {
|
||
|
load(packet, res);
|
||
|
} else if (action === 'load-language') {
|
||
|
loadLanguage(packet, res);
|
||
|
} else if (action === 'initialize') {
|
||
|
initialize(packet, res);
|
||
6 years ago
|
} else if (action === 'set-parameters') {
|
||
|
setParameters(packet, res);
|
||
6 years ago
|
} else if (action === 'recognize') {
|
||
|
recognize(packet, res);
|
||
7 years ago
|
} else if (action === 'detect') {
|
||
6 years ago
|
detect(packet, res);
|
||
6 years ago
|
} else if (action === 'terminate') {
|
||
|
terminate(packet, res);
|
||
7 years ago
|
}
|
||
|
} catch (err) {
|
||
7 years ago
|
/** Prepare exception to travel through postMessage */
|
||
6 years ago
|
res.reject(err.toString());
|
||
7 years ago
|
}
|
||
|
};
|
||
|
|
||
6 years ago
|
/**
|
||
|
* setAdapter
|
||
|
*
|
||
|
* @name setAdapter
|
||
|
* @function
|
||
|
* @access public
|
||
6 years ago
|
* @param {object} adapter - implementation of the worker, different in browser and node environment
|
||
6 years ago
|
*/
|
||
6 years ago
|
exports.setAdapter = (_adapter) => {
|
||
|
adapter = _adapter;
|
||
7 years ago
|
};
|