首页 > 其他分享 >tesseract-wasm 基于webassembly 的tesseract npm 包

tesseract-wasm 基于webassembly 的tesseract npm 包

时间:2023-11-01 21:58:30浏览次数:28  
标签:npm webassembly const image await wasm import tesseract

tesseract 是一个开源的ocr 工具,社区提供可一个基于webassembly 的tesseract-wasm ,可以方便直接基于浏览器的ocr 识别
以下是一个简单的试用

项目代码

  • package.json
 
{
  "name": "tesseract",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "tesseract-wasm": "^0.10.0"
  },
  "devDependencies": {
    "vite": "^4.5.0"
  },
  "scripts": {
    "dev":"vite --force",
    "build":"vite build"
  }
}
  • vite.config.js
import { defineConfig } from "vite";
 
export default defineConfig({
  optimizeDeps: {
    exclude: ['tesseract-wasm']
  },
  build: {
    rollupOptions:{
      output:{
        entryFileNames: `[name].js`,
        chunkFileNames: `[name].js`,
        assetFileNames: `[name].[ext]`
      }
    }
  },
  assetsInclude: ['**/*.wasm',"**/*.traineddata"],
});
  • 测试代码

需要识别的图片

app.js


 
import { OCRClient } from 'tesseract-wasm';
 
// 识别一个中文的图片
import imgUrl from './6.png'
 
// 使用中文模型
import traineddataModel from './chi_sim.traineddata'
 
async function runOCR() {
 
  // Fetch document image and decode it into an ImageBitmap.
  const imageResponse = await fetch(imgUrl);
  const imageBlob = await imageResponse.blob();
  const image = await createImageBitmap(imageBlob);
 
  // Initialize the OCR engine. This will start a Web Worker to do the
  // work in the background.
  const ocr = new OCRClient();
 
  try {
    // Load the appropriate OCR training data for the image(s) we want to
    // process.
    await ocr.loadModel(traineddataModel);
 
    await ocr.loadImage(image);
 
    // Perform text recognition and return text in reading order.
    const text = await ocr.getText();
   // 渲染到页面
    document.body.textContent = `tesseract-wasm result: ${text}`;
 
  } finally {
    // Once all OCR-ing has been done, shut down the Web Worker and free up
    // resources.
    ocr.destroy();
  }
}
 
runOCR();
  • 效果

 

说明

目前来说tesseract-wasm 并不是很大(2m) 左右,但是中文的模型比较大(50M),对于一些简单场景,而且可以容忍加载时间的可以使用
实际上做好优化也还可以,因为使用了web worker 对于一些加载会有一些问题,我使用了原始模式的vite 构建,完整代码在github 中,同时tesseract-wasm 也提供了nodejs 支持,官方示例提供了,nodejs 示例

 
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { Command } from "commander";
 
import { createOCREngine } from "tesseract-wasm";
import { loadWasmBinary } from "tesseract-wasm/node";
import sharp from "sharp";
 
async function loadImage(path) {
  const image = await sharp(path).ensureAlpha();
  const { width, height } = await image.metadata();
  return {
    data: await image.raw().toBuffer(),
    width,
    height,
  };
}
 
/** Resolve a URL relative to the current module. */
function resolve(path) {
  return fileURLToPath(new URL(path, import.meta.url).href);
}
 
const program = new Command();
program.description("Extract text from an image");
program.argument("file");
program.parse();
 
// Initialize the OCR engine. In this demo we use the synchronous OCREngine
// API directly. In a server you would want to use the async OCRClient API
// instead.
const wasmBinary = await loadWasmBinary();
const engine = await createOCREngine({ wasmBinary });
 
const model = readFileSync("chi_sim.traineddata");
engine.loadModel(model);
 
// Load the image and perform OCR synchronously.
const image = await loadImage(program.args[0]);
engine.loadImage(image);
 
const text = engine.getText((progress) => {
  process.stderr.write(`\rRecognizing text (${progress}% done)...`);
});
process.stderr.write("\n\n");
process.stdout.write(text);

参考资料

https://github.com/tesseract-ocr/tesseract
https://github.com/robertknight/tesseract-wasm
https://github.com/robertknight/tesseract-wasm/tree/main/examples
https://www.fabiofranchino.com/log/how-to-remove-hashing-in-vite-built-file-names/
https://github.com/vitejs/vite/issues/378
https://github.com/rongfengliang/tesseract-wasm-learning

标签:npm,webassembly,const,image,await,wasm,import,tesseract
From: https://www.cnblogs.com/rongfengliang/p/17804205.html

相关文章

  • tinygo webassembly 试用
    主要是简单测试下tinygo的使用,同时基于vite进行web的集成构建wasm生成注意只测试标注类型支持比较多,其他的就没添加,其他类型的需要自己处理,这点上wasm-pack处理的比较好main.gopackagemain //go:wasm-module//exportaddfuncadd(x,yuint32)uint......
  • java webassembly 集成试用
    wasmerio这个组织实现了不少webassembly周边的工具,以下是一个简单的java集成试用安装命令因为java包默认没有发布到中央仓库,需要自己本地安装localmavenmvninstall:install-file-Dfile=./wasmer-jni-amd64-darwin-0.3.0.jar-DgroupId=org.wasmer-Dartifac......
  • 在VScode中执行npm、yarn命令报错解
    在VScode中执行npm、yarn命令报错解我使用的是vnm安装好npm,在Windows+R界面是可以运行查看出版本的;但是在VScode中报错。查了很多资料,我这种情况的原因是在VScode中默认使用的终端是Powershell,然后我切换到系统的cmd则可以运行成功了,具体原因:在VSCode中,它使用的是独立的PowerS......
  • emscripten基于llvm的webassembly 编译器
    emscripten是一个基于llvm的webassembly编译器包含的特性可移植支持编译现有的c,c++以及其他语言项目,使用lvvm编译为支持nodejs,浏览器,以及wasm运行时运行的服务提供apis支持将openGL转换为WebGL,pthreads转换为webapi。。。快速集成了不少工具(llvm,emscripten,binar......
  • npm下载源如何修改为其他源地址
    npm下载源如何修改为其他源地址目录npm下载源如何修改为其他源地址1、国内源地址清单2、修改npm源2.1、临时修改2.2、永久修改方法一:使用命令行修改方法二:直接修改配置文件3、npm仓库管理工具1、国内源地址清单官方的源地址:https://registry.npmjs.org/由于服务器在海外,国内......
  • 在NodeJS中使用npm包实现JS代码混淆加密
    使用npm包,在NodeJS中实现JS代码混淆加密在前后端JS开发过程中,JS代码保护(JS代码混淆加密)是非常重要的一环。JShaman是一个云端的代码保护Saas平台,可以对JS代码进行混淆、加密、压缩等操作,从而增强JS代码的安全性。同时,JShaman还有更方便易用的npm包,方便开发人员通过调用接口的方式,快......
  • pnpm : 无法加载文件
    报错pnpm:无法加载文件C:\Users\16978\AppData\Roaming\npm\pnpm.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅https:/go.microsoft.com/fwlink/?LinkID=135170中的about_Execution_Policies。 解决办法windows11powershell的安全策略,将nrm命令视为了不安全......
  • nginx unit WebAssembly 试用
    nginxunit已经支持WebAssembly,刚好体验下环境准备基于docker运行unit,对于wasm的开发基于rust,实际上测试直接试用了官方的示例代码docker-composeversion:"3"services:app:image:unit:1.31.1-wasmports:-8080:8080......
  • npm 安装依赖报错整理
    1. [email protected]:`nodeinstall.js`npmERR!codeELIFECYCLEnpmERR!errno1npmERR!chromedriver@2.27.2install:`nodeinstall.js`npmERR!Exitstatus1npmERR!npmERR!Failedatthechromedriver@2.27.2installscript.npmER......
  • npm发包教程
    1-npm注册账号访问npm官网注册账号,邮件验证激活账号npm官网2-项目npm配置在项目下打开终端,初始化npmnpminit-y此时项目下会生成package.json配置文件,可以在里面修改配置。3-登录npm账号注意:这里开始不要使用npm镜像,否则发包会失败。如果使用了镜像,可以输入下面指令......