Search code examples
angulartypescriptimportocrtesseract

How to get Tesseract to work with Angular2+?


I am trying to use Tesseract in one my components to perform ocr on a file.

.ts:

import * as Tesseract from 'tesseract.js';

fileToUpload: File = null;
handleFileInput(files: FileList) {
    this.fileToUpload = files.item(0);
  }
imageOcr() {
    Tesseract.recognize(this.fileToUpload)
      .progress(message => console.log(message))
      .catch(err => console.error(err))
      .then(res => console.log(res))
      .finally(resultOrError => console.log(resultOrError));
 }

.html

<div>
  <h6>Local Image OCR</h6>
  <input type="file" accept=".jpg,.png,.jpeg,.webp"  (change)="handleFileInput($event.target.files)">
  <button (click)="imageOcr()">click</button>
</div>

I followed this , however this error shows

"blob:http://localhost:4200/65999042-8757-4264-b92d-ed5e0a0e4c27:1 Uncaught DOMException: Failed to execute 'importScripts' on 'WorkerGlobalScope': The script at 'http://localhost:4200/dist/worker.dev.js?nocache=qf0eq67rus' failed to load.
    at blob:http://localhost:4200/65999042-8757-4264-b92d-ed5e0a0e4c27:1:1"

How or what should I do to make this work?


Solution

  • If anyone else encounters this , here's the solution I found : tesseract typescript wrapper.

    Here's a link to github