Browse Source

Fix PSM

pull/333/head
Jerome Wu 5 years ago
parent
commit
ce4d4d51be
  1. 6
      package-lock.json
  2. 2
      package.json
  3. 5
      src/common/dump.js
  4. 2
      src/common/types.js
  5. 43
      src/common/workerUtils.js
  6. 20
      tests/recognize.test.js

6
package-lock.json generated

@ -8553,9 +8553,9 @@ @@ -8553,9 +8553,9 @@
}
},
"tesseract.js-core": {
"version": "2.0.0-beta.10",
"resolved": "https://registry.npmjs.org/tesseract.js-core/-/tesseract.js-core-2.0.0-beta.10.tgz",
"integrity": "sha512-QmNgMA9m5ES5uMTqpOAPysrUA80vUx/6WKQlfkK3zhOeAgqv8DjwwcDv9tQv2TgRzOQ+LFKrJn94Y2rw5b2IGw=="
"version": "2.0.0-beta.11",
"resolved": "https://registry.npmjs.org/tesseract.js-core/-/tesseract.js-core-2.0.0-beta.11.tgz",
"integrity": "sha512-07haKH2JYYo0OfIJoioMS9dDiI5Hrl7+r1MqjeNAAT5WpKO0ATe4cpncC8s1kz0e3s1kaC5WOwL3YJcjbJE+hg=="
},
"tesseract.js-utils": {
"version": "1.0.0-beta.8",

2
package.json

@ -57,7 +57,7 @@ @@ -57,7 +57,7 @@
"node-fetch": "^2.3.0",
"opencollective-postinstall": "^2.0.2",
"resolve-url": "^0.2.1",
"tesseract.js-core": "^2.0.0-beta.10",
"tesseract.js-core": "^2.0.0-beta.11",
"tesseract.js-utils": "^1.0.0-beta.8"
},
"repository": {

5
src/common/dump.js

@ -78,7 +78,10 @@ module.exports = (TessModule, api, { @@ -78,7 +78,10 @@ module.exports = (TessModule, api, {
for (let i = 0; i < n; i += 1) {
polygon.push([px.getValue(i), py.getValue(i)]);
}
TessModule._ptaDestroy(TessModule.getPointer(poly));
/*
* TODO: find out why _ptaDestroy doesn't work
*/
// TessModule._ptaDestroy(TessModule.getPointer(poly));
}
block = {

2
src/common/types.js

@ -12,7 +12,6 @@ module.exports = { @@ -12,7 +12,6 @@ module.exports = {
LSTM_ONLY: 1,
TESSERACT_LSTM_COMBINED: 2,
DEFAULT: 3,
COUNT: 4,
},
/*
* PSM = Page Segmentation Mode
@ -31,6 +30,5 @@ module.exports = { @@ -31,6 +30,5 @@ module.exports = {
SPARSE_TEXT: '10',
SPARSE_TEXT_OSD: '11',
RAW_LINE: '12',
COUNT: '13',
},
};

43
src/common/workerUtils.js

@ -11,6 +11,7 @@ const { readImage, loadLang } = require('tesseract.js-utils'); @@ -11,6 +11,7 @@ const { readImage, loadLang } = require('tesseract.js-utils');
const pdfTTF = require('./pdf-ttf');
const dump = require('./dump');
const { defaultParams } = require('./options');
const { OEM, PSM } = require('./types');
/*
* Tesseract Module returned by TesseractCore.
@ -201,11 +202,33 @@ const loadLanguage = ({ langs, options }, res) => { @@ -201,11 +202,33 @@ const loadLanguage = ({ langs, options }, res) => {
* @param {object} res - job instance
*/
const handleRecognize = ({
image, langs, options, params: customParams,
}, res) => (
handleInit(options, res)
image, langs: iLangs, options, params: customParams,
}, res) => {
const params = {
...defaultParams,
...customParams,
};
const { tessedit_pageseg_mode } = params;
let langs = iLangs;
/*
* When PSM === OSD_ONLY or AUTO_OSD or RAW_LINE
* osd.traineddata must be included and
* OEM must be TESSERACT_ONLY (LSTM doesn't support OSD)
*/
if ([
PSM.OSD_ONLY,
PSM.AUTO_OSD,
PSM.RAW_LINE,
].includes(tessedit_pageseg_mode)) {
langs = (typeof iLangs === 'string')
? `${iLangs}+osd`
: [...iLangs, 'osd'];
params.tessedit_ocr_engine_mode = OEM.TESSERACT_ONLY;
}
return handleInit(options, res)
.then(() => (
loadLanguage({ langs, options }, res)
loadLanguage({ langs, params, options }, res)
.catch((e) => {
if (e instanceof DOMException) {
/*
@ -222,10 +245,6 @@ const handleRecognize = ({ @@ -222,10 +245,6 @@ const handleRecognize = ({
const progressUpdate = (progress) => {
res.progress({ status: 'initializing api', progress });
};
const params = {
...defaultParams,
...customParams,
};
progressUpdate(0);
handleParams(langs, params);
progressUpdate(0.5);
@ -241,8 +260,8 @@ const handleRecognize = ({ @@ -241,8 +260,8 @@ const handleRecognize = ({
res.reject({ err });
}
})
))
);
));
};
/**
* handleDetect
@ -263,8 +282,8 @@ const handleDetect = ({ @@ -263,8 +282,8 @@ const handleDetect = ({
.then(() => (
loadLanguage({ langs, options }, res)
.then(() => {
api.Init(null, getLangsStr(langs));
api.SetPageSegMode(TessModule.PSM_OSD_ONLY);
api.Init(null, getLangsStr(langs), OEM.TESSERACT_ONLY);
api.SetPageSegMode(PSM.OSD_ONLY);
const params = {
...defaultParams,
...customParams,

20
tests/recognize.test.js

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
const { TesseractWorker } = Tesseract;
const { TesseractWorker, PSM } = Tesseract;
const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
const SIMPLE_PNG = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUAAAAC0CAIAAABqhmJGAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAASuSURBVHhe7dftVdswAIbRzsVAzMM0XabDUCOUxLYsWW4Jp+/pvf9w9GH76CHw4x2IJWAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAI9p8G/PbyY8rL2686g8t+vnqHTyfgIYfvz/26veTXn/UKX8+f0EU9bHrtu/6KfAN/AwEXAj7lFf2TBFw4nae8on+SgIvJ01n/KLzpDK+L3bT/Ap4O+HC+V12mTH+M3gzcLbIY/EO6HfxYp13k09nb6r3UqcdnjoCL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IJmAIJmCI9f7+G6yFxVg/GyYwAAAAAElFTkSuQmCC';
@ -145,6 +145,24 @@ describe('recognize()', () => { @@ -145,6 +145,24 @@ describe('recognize()', () => {
));
});
describe('should support all page seg modes', () => {
Object
.keys(PSM)
.map(name => ({ name, mode: PSM[name] }))
.forEach(({ name, mode }) => (
it(`support PSM.${name} mode`, (done) => {
const worker = getWorker();
worker
.recognize(`${IMAGE_PATH}/testocr.png`, 'eng', { tessedit_pageseg_mode: mode })
.then((ret) => {
expect(Object.keys(ret).length).not.to.be(0);
worker.terminate();
done();
});
}).timeout(30000)
));
});
describe('should recognize base64 image', () => {
[
{ format: 'png', image: SIMPLE_PNG, ans: SIMPLE_TEXT },

Loading…
Cancel
Save