Jerome Wu
5 years ago
18 changed files with 341 additions and 274 deletions
@ -1,4 +1,9 @@
@@ -1,4 +1,9 @@
|
||||
const constants = require('../tests/constants'); |
||||
global.expect = require('expect.js'); |
||||
global.fs = require('fs'); |
||||
global.path = require('path'); |
||||
global.Tesseract = require('../src'); |
||||
|
||||
Object.keys(constants).forEach((key) => { |
||||
global[key] = constants[key]; |
||||
}); |
||||
|
@ -0,0 +1,3 @@
@@ -0,0 +1,3 @@
|
||||
module.exports = (prefix, cnt) => ( |
||||
`${prefix}-${cnt}-${Math.random().toString(16).slice(3, 8)}` |
||||
); |
@ -0,0 +1,18 @@
@@ -0,0 +1,18 @@
|
||||
const resolveURL = require('resolve-url'); |
||||
const { version, dependencies } = require('../../../package.json'); |
||||
const defaultOptions = require('../../constants/defaultOptions'); |
||||
|
||||
/* |
||||
* Default options for browser worker |
||||
*/ |
||||
module.exports = { |
||||
...defaultOptions, |
||||
workerPath: (typeof process !== 'undefined' && process.env.TESS_ENV === 'development') |
||||
? resolveURL(`/dist/worker.dev.js?nocache=${Math.random().toString(36).slice(3)}`) |
||||
: `https://unpkg.com/tesseract.js@v${version}/dist/worker.min.js`, |
||||
/* |
||||
* If browser doesn't support WebAssembly, |
||||
* load ASM version instead |
||||
*/ |
||||
corePath: `https://unpkg.com/tesseract.js-core@v${dependencies['tesseract.js-core'].substring(1)}/tesseract-core.${typeof WebAssembly === 'object' ? 'wasm' : 'asm'}.js`, |
||||
}; |
File diff suppressed because one or more lines are too long
@ -1,33 +1,21 @@
@@ -1,33 +1,21 @@
|
||||
const { TesseractWorker } = Tesseract; |
||||
const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined'; |
||||
const IMAGE_PATH = 'http://localhost:3000/tests/assets/images'; |
||||
const loadLangOptions = { |
||||
langPath: 'http://localhost:3000/tests/assets/traineddata', |
||||
cachePath: './tests/assets/traineddata', |
||||
}; |
||||
|
||||
const getWorker = options => ( |
||||
new TesseractWorker({ |
||||
cacheMethod: 'readOnly', |
||||
...(isBrowser ? { workerPath: 'http://localhost:3000/dist/worker.dev.js' } : {}), |
||||
...loadLangOptions, |
||||
...options, |
||||
}) |
||||
); |
||||
const { createScheduler, createWorker } = Tesseract; |
||||
const scheduler = createScheduler(); |
||||
const worker = createWorker(OPTIONS); |
||||
scheduler.addWorker(worker); |
||||
before(function cb() { |
||||
this.timeout(0); |
||||
return worker.load(); |
||||
}); |
||||
|
||||
describe('detect()', () => { |
||||
it('should detect OSD', (done) => { |
||||
describe('detect()', async () => { |
||||
it('should detect OSD', () => { |
||||
[ |
||||
{ name: 'cosmic.png', ans: { id: 12, degree: 0 } }, |
||||
].forEach(({ name, ans: { id, degree } }) => { |
||||
const worker = getWorker(); |
||||
worker |
||||
.detect(`${IMAGE_PATH}/${name}`) |
||||
.then(({ tesseract_script_id, orientation_degrees }) => { |
||||
expect(tesseract_script_id).to.be(id); |
||||
expect(orientation_degrees).to.be(degree); |
||||
done(); |
||||
}); |
||||
{ name: 'cosmic.png', ans: { script: 'Latin' } }, |
||||
].forEach(async ({ name, ans: { script } }) => { |
||||
await worker.loadLanguage('osd'); |
||||
await worker.initialize('osd'); |
||||
const { data: { script: s } } = await scheduler.addJob('detect', `${IMAGE_PATH}/${name}`); |
||||
expect(s).to.be(script); |
||||
}); |
||||
}).timeout(10000); |
||||
}).timeout(TIMEOUT); |
||||
}); |
||||
|
File diff suppressed because one or more lines are too long
@ -0,0 +1,18 @@
@@ -0,0 +1,18 @@
|
||||
<html> |
||||
<head> |
||||
<meta charset="utf-8"> |
||||
<link rel="stylesheet" href="../node_modules/mocha/mocha.css"> |
||||
</head> |
||||
<body> |
||||
<div id="mocha"></div> |
||||
<script src="../node_modules/mocha/mocha.js"></script> |
||||
<script src="../node_modules/expect.js/index.js"></script> |
||||
<script src="../dist/tesseract.dev.js"></script> |
||||
<script src="./constants.js"></script> |
||||
<script>mocha.setup('bdd');</script> |
||||
<script src="./scheduler.test.js"></script> |
||||
<script> |
||||
mocha.run(); |
||||
</script> |
||||
</body> |
||||
</html> |
@ -0,0 +1,35 @@
@@ -0,0 +1,35 @@
|
||||
const { createScheduler, createWorker } = Tesseract; |
||||
|
||||
let workers = []; |
||||
|
||||
before(async function cb() { |
||||
this.timeout(0); |
||||
const NUM_WORKERS = 10; |
||||
console.log(`Initializing ${NUM_WORKERS} workers`); |
||||
workers = await Promise.all(Array(NUM_WORKERS).fill(0).map(async () => { |
||||
const w = createWorker(OPTIONS); |
||||
await w.load(); |
||||
await w.loadLanguage('eng'); |
||||
await w.initialize('eng'); |
||||
return w; |
||||
})); |
||||
console.log(`Initialized ${NUM_WORKERS} workers`); |
||||
}); |
||||
|
||||
describe('scheduler', () => { |
||||
describe('should speed up with more workers (running 20 jobs)', () => { |
||||
Array(10).fill(0).forEach((_, num) => ( |
||||
it(`support using ${num + 1} workers`, async () => { |
||||
const NUM_JOBS = 30; |
||||
const scheduler = createScheduler(); |
||||
workers.slice(0, num + 1).forEach((w) => { |
||||
scheduler.addWorker(w); |
||||
}); |
||||
const rets = await Promise.all(Array(NUM_JOBS).fill(0).map((_, idx) => ( |
||||
scheduler.addJob('recognize', `${IMAGE_PATH}/${idx % 2 === 0 ? 'simple' : 'cosmic'}.png`) |
||||
))); |
||||
expect(rets.length).to.be(NUM_JOBS); |
||||
}).timeout(60000) |
||||
)); |
||||
}); |
||||
}); |
Loading…
Reference in new issue