Tips for integrating Tesseract with Angular 2 and above

I'm currently exploring the use of Tesseract within one of my components for OCR processing 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>

While following this guide, I encountered the following error message:

"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"

What steps should I take to resolve this issue and successfully implement Tesseract functionality?

Answer №1

In the event that someone faces a similar issue, I have discovered a solution: the tesseract typescript wrapper.

For more information, please visit this link to GitHub

Answer №2

Avoid relying on an additional TypeScript wrapper dependency, as you can achieve the desired outcome without it.

  1. Begin by installing the necessary JavaScript module:

    npm install tesseract.js --save

  2. Next, add the @types declarations:

    npm install @types/tesseract.js --save

  3. To import, use the following code snippet:

    import * as Tesseract from 'tesseract.js'

  4. Utilize the module in this manner:

    let filename = 'assets/img/abcdefg.jpg'
     Tesseract.recognize(filename)
          .progress(function (p) { console.log('progress', p) })
          .catch(err => console.error(err))
          .then(function (result) {
            console.log("result ======<<<<>>>>>");
            console.log(result.text)
      })

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Troubleshooting Paths with Angular's NgFor Directive

Within my Angular project, I have implemented a basic ngFor loop to display logo images. Here is a snippet of the code: <div *ngFor="let item of list" class="logo-wrapper"> <div class="customer-logo"> & ...

Setting up a Node.js project in your local environment and making it

I need help installing my custom project globally so I can access it from anywhere in my computer using the command line. However, I've been struggling to make it work. I attempted the following command: npm install -g . and some others that I can&ap ...

Exploring how process.argv in NodeJS can be utilized within JavaScript code compiled by

I'm having trouble compiling a basic TypeScript file using webpack (with 'awesome-typescript-loader') that needs to access command line arguments. It seems like the compiled JavaScript is causing a problem by overriding the Node 'proce ...

What is the best way to execute my mocha fixtures with TypeScript?

I am seeking a cleaner way to close my server connection after each test using ExpressJS, TypeScript, and Mocha. While I know I can manually add the server closing code in each test file like this: this.afterAll(function () { server.close(); ...

Encountering difficulties testing MatTable row population in Karma testing

Can someone please assist me in identifying the issues with my coding method? I attempted to replicate the techniques demonstrated in this tutorial on Harnesses Here is an Angular component that consists of a simple data table (MatTable) connected to a re ...

the quickest method to apply font-weight:bold; to the text within the component

Is there a way to apply font-weight: bold; only to the inner content of a component in a scss file while avoiding affecting the component tag itself? :host { font-weight: bold; } Although this code works, it also affects the component tag itself. What ...

Issue with ESLint error in TypeScript PrimeReact async Button click handler

I am currently facing an issue with exporting data from a DataTable in PrimeReact. The onClick function for the Button does not allow async callbacks as flagged by eslint. Can someone guide me on how to properly call this function? const exportCSV = us ...

Tips for inserting a row component into a table using Angular 7

I am currently using the latest version of Angular (7.2.0). I have created a custom tr component as follows: import { Component, OnInit, Input } from '@angular/core'; @Component({ selector: 'app-table-row', templateUrl: './table- ...

I'm having trouble with Angular pipes in certain areas... but I must say, Stackblitz is truly incredible

Encountering this issue: ERROR in src\app\shopping-cart-summary\shopping-cart-summary.component.html(15,42): : Property '$' does not exist on type 'ShoppingCartSummaryComponent'. The error disappears when I remove the c ...

Swagger Issue Resolved: Restriction on Number of Params Set

After setting up this option for my route, I noticed that when accessing the first parameter (page), it correctly returns the value entered in Swagger UI. However, when trying to access the second parameter (genre), it seems to interpret it as a string &ap ...

What is the best way to pass a generic interface to the zustand create function in a TypeScript environment

Having trouble figuring out the right syntax to pass a generic interface when calling a function that accepts a generic type. My goal is to use: const data = itemStore<T>(state => state.data) import { create } from "zustand"; interface ...

The type 'Observable<void | AuthError>' cannot be assigned to 'Observable<Action>'

I am encountering an error that reads: error TS2322: Type 'Observable<void | AuthError>' is not assignable to type 'Observable<Action>'. Type 'void | AuthError' is not assignable to type 'Action'. Type &a ...

Angular. The MatSelectionListChange event is returning an undefined value

I am attempting to retrieve the value of a changed element in a list, but I always get an undefined value while the checked property seems to be correct. HTML <div> <mat-selection-list #costUnits [(ngModel)]="selectedCostUnits" ...

Tabulate the number of items in an array based on the month and

I have received JSON data with dates indicating the creation time of multiple parcels. I want to analyze this data and calculate the total number of parcels created in each month. I am new to this process and unsure about which thread on Stack Overflow can ...

Navigating product search in your Ionic Ecommerce App

Currently, I am working on developing an Ionic Ecommerce App with a large number of products that need to be displayed. However, I am facing a challenge when it comes to implementing the search functionality for these products within the Ionic App. After c ...

Looking for a way to toggle the visibility of a dropdown list when clicking on an input in Angular7?

My Angular7 application features a dropdown menu that automatically closes when an item is selected. Additionally, I have implemented functionality to toggle the dropdown open and closed by clicking on an input field. You can view a live example of this be ...

Next.js encountered an error when trying to locate the 'net' module while working with PostgreSQL

I'm facing a challenge in my Next.js project while attempting to retrieve all records from a table. The error message I'm encountering is "Module not found: Can't resolve 'net'" with an import trace pointing to multiple files withi ...

Can builtins like DOM globals be explicitly imported?

The present situation includes the utilization of rollup (as well as iife parameters), but I am hesitant about whether it is solely related to rollup or typescript. My objective is to achieve something similar to this: import { document } from "[wherever ...

Investigating the Angular signals that triggered the effect

Is there a way to determine which Angular signal triggered an effect? I have two signals and one effect in a component. One signal is used to start a timer function within a service, while the other signal reacts to changing conditions in the timer functio ...

Is there a way to specialize generic methods in Typescript and make them more specific?

I am working with a generic static method in an abstract class: abstract class Base { static find<T extends Base>(options?: Object): Promise<T[]> { return findResults(options); } } Now, I am trying to specify its type in a derived cla ...