Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Updated scheduler examples per #801 (#803)
  • Loading branch information
Balearica committed Jul 26, 2023
1 parent 67b982e commit 6f90bc9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
11 changes: 6 additions & 5 deletions examples/browser/basic-efficient.html
Expand Up @@ -4,7 +4,7 @@
<script src="/dist/tesseract.dev.js"></script>
</head>
<body>
<input type="file" id="uploader">
<input type="file" id="uploader" multiple>
<script type="module">

// This is a basic example more efficient than "basic.html".
Expand All @@ -22,10 +22,11 @@

const recognize = async function(evt){
const files = evt.target.files;

const ret = await worker.recognize(files[0]);
console.log(ret.data.text);


for (let i=0; i<files.length; i++) {
const ret = await worker.recognize(files[i]);
console.log(ret.data.text);
}
}
const elm = document.getElementById('uploader');
elm.addEventListener('change', recognize);
Expand Down
49 changes: 49 additions & 0 deletions examples/browser/basic-scheduler.html
@@ -0,0 +1,49 @@
<!DOCTYPE HTML>
<html>
<head>
<script src="/dist/tesseract.dev.js"></script>
</head>
<body>
<input type="file" id="uploader" multiple>
<script type="module">

// This example builds on "basic-efficient.html".
// Rather than using a single worker, a scheduler manages a pool of multiple workers.
// While performance is similar for a single file, this parallel processing results in significantly
// faster speeds when used with multiple files.

const scheduler = Tesseract.createScheduler();

// Creates worker and adds to scheduler
const workerGen = async () => {
const worker = await Tesseract.createWorker({
corePath: '../../node_modules/tesseract.js-core',
workerPath: "/dist/worker.dev.js",
logger: function(m){console.log(m);}
});
await worker.loadLanguage('eng');
await worker.initialize('eng');
scheduler.addWorker(worker);
}

const workerN = 4;
(async () => {
const resArr = Array(workerN);
for (let i=0; i<workerN; i++) {
resArr[i] = await workerGen();
}
})();

const recognize = async function(evt){
const files = evt.target.files;

for (let i=0; i<files.length; i++) {
scheduler.addJob('recognize', files[i]).then((x) => console.log(x.data.text))
}
}

const elm = document.getElementById('uploader');
elm.addEventListener('change', recognize);
</script>
</body>
</html>
5 changes: 2 additions & 3 deletions examples/node/scheduler.js
Expand Up @@ -4,8 +4,7 @@ const scheduler = createScheduler();

// Creates worker and adds to scheduler
const workerGen = async () => {
const worker = createWorker({cachePath: "."});
await worker.load();
const worker = await createWorker({cachePath: "."});
await worker.loadLanguage('eng');
await worker.initialize('eng');
scheduler.addWorker(worker);
Expand All @@ -15,7 +14,7 @@ const workerN = 4;
(async () => {
const resArr = Array(workerN);
for (let i=0; i<workerN; i++) {
resArr[i] = workerGen();
resArr[i] = await workerGen();
}
await Promise.all(resArr);
/** Add 4 recognition jobs */
Expand Down

0 comments on commit 6f90bc9

Please sign in to comment.