node-tesseract 无法加载语言

node-tesseract failed to load languages

我从 Whosebug 复制了一个代码来从图像的 base64 中读取文本:

let base64 = req.body.toString("base64");
let imageBuffer = Buffer.from(base64, "base64");

const worker = createWorker();
(async () => {
    await worker.load();
    await worker.loadLanguage('eng');
    await worker.initialize('eng');
    console.log("Recognizing...");
    const { data: { text } } = await worker.recognize(imageBuffer);
    console.log("Recognized text:", text);
    await worker.terminate();
})();

但是我得到了这个错误:

Error opening data file ./eng.traineddata
Please make sure the TESSDATA_PREFIX environment variable is set to your "tessdata" directory.
Failed loading language 'eng'
Tesseract couldn't load any languages!
Recognizing...
AdaptedTemplates != nullptr:Error:Assert failed:in file /workspace/tesseract/src/classify/adaptmatch.cpp, line 196
undefined
undefined
C:\Users\jaysm\OneDrive\Desktop\QBase\node_modules\tesseract.js\src\createWorker.js:173
        throw Error(data);
        ^

Error: RuntimeError: abort(undefined). Build with -s ASSERTIONS=1 for more info.
    at ChildProcess.<anonymous> (C:\Users\jaysm\OneDrive\Desktop\QBase\node_modules\tesseract.js\src\createWorker.js:173:15)
    at ChildProcess.emit (events.js:315:20)
    at emit (internal/child_process.js:903:12)
    at processTicksAndRejections (internal/process/task_queues.js:81:21)
[nodemon] app crashed - waiting for file changes before starting...

我的代码有什么问题?

我找到了这个错误的一些解决方案,但它们是使用 python 或其他一些语言,但 none 使用 NodeJS。

实际上我的问题不是将 TESSDATA_PREFIX 设置为环境变量,而是我没有将 eng.traineddata 文件放在基本目录中。

我放置了 eng.traineddata 文件,问题已解决!