basic-scheduler.html 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <script src="/dist/tesseract.min.js"></script>
  5. </head>
  6. <body>
  7. <input type="file" id="uploader" multiple>
  8. <script type="module">
  9. // This example builds on "basic-efficient.html".
  10. // Rather than using a single worker, a scheduler manages a pool of multiple workers.
  11. // While performance is similar for a single file, this parallel processing results in significantly
  12. // faster speeds when used with multiple files.
  13. const scheduler = Tesseract.createScheduler();
  14. // Creates worker and adds to scheduler
  15. const workerGen = async () => {
  16. const worker = await Tesseract.createWorker("eng", 1, {
  17. corePath: '../../node_modules/tesseract.js-core',
  18. workerPath: "/dist/worker.min.js",
  19. logger: function(m){console.log(m);}
  20. });
  21. scheduler.addWorker(worker);
  22. }
  23. const workerN = 4;
  24. (async () => {
  25. const resArr = Array(workerN);
  26. for (let i=0; i<workerN; i++) {
  27. resArr[i] = await workerGen();
  28. }
  29. })();
  30. const recognize = async function(evt){
  31. const files = evt.target.files;
  32. for (let i=0; i<files.length; i++) {
  33. scheduler.addJob('recognize', files[i]).then((x) => console.log(x.data.text))
  34. }
  35. }
  36. const elm = document.getElementById('uploader');
  37. elm.addEventListener('change', recognize);
  38. </script>
  39. </body>
  40. </html>