使用tesseract.js - npm (npmjs.com)
官方demo:GitHub - Balearica/tesseract.js-electron: An example to use tesseract.js in electron
目录结构:
async function ocr() {
const worker = await Tesseract.createWorker("chi_sim", 1, {
workerPath: "./ocr/worker.js", langPath: "./lang-data", corePath: "tesseract-core.wasm.js", logger: m => console.log(m), gzip: true,
workerBlobURL: false
});
const ret = await worker.recognize('./images/test.png');
console.log(ret.data.text);
alert(ret.data.text);
await worker.terminate();
}
需要注意的地方:
1. 三个文件都可以npm install tesseract.js后在node_modules中找到
2. 语言包下载地址:tessdata/4.0.0_best/chi_sim.traineddata.gz at gh-pages · naptha/tessdata · GitHub
3. langPath与corePath的文件位置都是基于workerPath的相对路径
4. workerBlobURL要写false
5. 语言包若未经过解压gzip写true