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

After the rendering process, the React Component member goes back to a state of

One issue I encountered is related to a component that utilizes a separate client for making HTTP requests. Specifically, when trying to use the client within a click event handler, the call to this.client.getChannel() fails due to this.client being undefi ...

What causes the Babel JSON configuration error to appear in my project?

I'm currently working on a React website, utilizing TSX instead of JSX. In my setup, I am using webpack and Babel. However, I have encountered an error while running the webpack-dev-server. ERROR in ./src/index.tsx Module build failed (from ./node_mo ...

Using template expressions to access properties that contain spaces

Here is the code structure that I am working with: "name": { "age": [ { "data": { "how old": "23" } }, One of my JSON keys has a space in it. How can I access this pr ...

An error occurred while trying to load the configuration "next/core-web-vitals" for extension

If you're embarking on a new project using NextJs and TypeScript, chances are you may encounter the following error: Failed to load config "next/core-web-vitals" to extend from. Wondering how to resolve this issue? ...

Transform data into JSON format using the stringify method

I am facing an issue with my TypeScript code where I need to retrieve specific information from a response. Specifically, I want to output the values of internalCompanyCode and timestamp. The Problem: An error is occurring due to an implicit 'any&apo ...

Tips for defining the type of any children property in Typescript

Currently, I am delving into Typescript in conjunction with React where I have a Team Page and a slider component. The slider component is functioning smoothly; however, my goal is to specify the type of children for it. The TeamPage import react, { Fragm ...

Tips for troubleshooting TypeScript integration tests within Pycharm

Currently, I have been utilizing PyCharm to code in typescript. To run tests in the integration directory, I execute npm run test:integration. However, I am now looking to debug the tests. The directory structure looks like this: my_project /src /tests ...

Setting up ng-bootstrap for your project

I'm currently in the process of trying to incorporate the ng-bootstrap package into my application. To ensure I have all the necessary packages, I've executed npm i and updated both the @angular/core and @angular/cli modules to their latest vers ...

update the element that acts as the divider in a web address (Angular)

Is it possible to modify the separator character used when obtaining the URL parameters with route.queryParams.subscribe? Currently, Object.keys(params) separates the parameters using "&" as the separator. Is there a way to change this to use a differe ...

Exploring the possibility of integrating direct search functionality into the URL bar within an Angular application

One interesting feature I observed on GitHub is that after typing "github.com" in the URL bar, you can directly search by pressing the spacebar, which activates the "search mode." Here's how it looks like on Chrome: https://i.sstatic.net/XIgJu.png I ...

Enhance the variety of types for an external module in TypeScript

I am in the process of migrating an existing codebase from a JavaScript/React/JSX setup to TypeScript. My plan is to tackle this task file by file, but I have a question regarding the best approach to make the TypeScript compiler work seamlessly with the e ...

Utilize useState and useEffect to efficiently sort through an item list based on its current state

I am currently working on an application where I have a list of items and I need to create a details page for each item when clicked. I am facing some challenges in implementing this functionality using useState, useEffect, and typescript. I have previousl ...

injecting a variable from the configuration service into a TypeScript decorator

I am interested in setting up a scheduled task for my NestJs application to run at regular intervals. I found information on how to use intervals in the NestJs documentation. Since my application uses configuration files, I want to keep the interval value ...

How to deliver various static files in NestJS using different paths and prefixes?

I've set up static file serving for developer documentation using the following code: app.useStaticAssets(docsLocation, { prefix: "/docs/" }) Now I have another directory with more static content that I want to serve. Is it possible to serve from ...

The login option failed to redirect users to their dashboard

I am encountering an issue with the login services. The password check works fine, but there seems to be a problem with the navigation router not functioning correctly. I feel like I might be overlooking something specific, can you please help me pinpoint ...

Typescript indicates that an object may be potentially null

I've hit a roadblock where I keep getting warnings that the objects might be null. After searching online and on StackOverflow, I've tried numerous solutions with no luck. My goal is to insert the text "test" into the HTML elements using their ID ...

Create a .d.ts file for a custom JavaScript file

I am working on an application written in JavaScript and considering incorporating TypeScript for a new feature. Currently, I have a base class defined in JavaScript as shown below: // base.js module.exports = function BaseClass () { // ... ... }; M ...

Disabling an attribute/element from change detection in Angular

Let's say I have the following code in my template: <img src="..." alt="myService.getImgAlt()" /> This works fine, but calling this function at every change detection can impact performance negatively. Is there a way to excl ...

Having trouble with nativescript-pager after updating nativescript-cli to version 6.0?

Error Found:- ERROR TypeError: Could not load view for: Pager.TypeError: Unable to access property 'PagerAdapter' of null ERROR TypeError: TNSViewPager is not a constructor Sample Code Snippet:- <Pager row="0" [items]="itemList" [selecte ...

Switching from HttpModule to HttpClientModule

Angular's transition from HttpModule to HttpClientModule is causing some confusion, as discussed in detail here. The official Angular tutorial at https://angular.io/tutorial/toh-pt6 still uses HttpModule, while the Fundamentals documentation at https ...