繁体   English   中英

如何让Tesseract与Angular2 +一起使用?

[英]How to get Tesseract to work with Angular2+?

我试图在我的一个组件中使用Tesseract在文件上执行ocr。

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

我跟着这个 ,但是这个错误显示了

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

我应该怎样或怎样做才能使这项工作成功?

如果有其他人遇到这个,这里是我找到的解决方案:tesseract typescript wrapper。

这是github的链接

不需要使用另一个打字稿包装器依赖项,当你可以不使用它时也可以这样做。

  1. 你需要安装实际的javascript模块:

    npm install tesseract.js --save

  2. 还要安装@types声明:

    npm install @types/tesseract.js --save

  3. 最后进行以下导入:

    import * as Tesseract from 'tesseract.js'

  4. 像这样使用它

    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) })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM