You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

node-tesseract-ocr报Cannot open input file: stdin错误及替代库咨询

问题:node-tesseract-ocr 调用Tesseract v3.02时出错,求排查及替代库?

环境与代码

Windows 11系统,使用Node.js v18.12.1、Tesseract v3.02,通过node-tesseract-ocr提取图片文字,执行代码如下:

const tesseract = require("node-tesseract-ocr");

const config = {
    lang: "eng",
    oem: 1,
    psm: 3,
};

tesseract
.recognize("https://tesseract.projectnaptha.com/img/eng_bw.png", config)
.then((text) => {
    console.log("Result:", text);
})
.catch((error) => {
    console.log(error.message);
});

报错信息

Command failed: tesseract stdin stdout -l eng --oem 1 --psm 3
read_params_file: Can't open 1
read_params_file: Can't open -psm
read_params_file: Can't open 3
Tesseract Open Source OCR Engine v3.02 with Leptonica
Cannot open input file: stdin

请问哪里操作有误?还有哪些可靠的图片文字提取库可以使用?


错误原因与解决方法

1. 参数版本不兼容

Tesseract v3.02不支持--oem参数(该参数是v4及以上版本引入的OCR引擎模式配置),同时v3仅支持短格式参数-psm,而node-tesseract-ocr默认按高版本Tesseract的长参数格式生成命令,导致v3把这些参数当成配置文件读取,出现read_params_file类错误。

2. 远程图片处理问题

node-tesseract-ocr处理远程URL时会通过stdin传递图片内容,但Tesseract v3.02对stdin输入的支持存在缺陷,引发Cannot open input file: stdin错误。

修复方案

  • 推荐:升级Tesseract到v4+版本:v4及以上版本支持--oem、--psm长参数,且对stdin输入的处理更完善,升级后原代码可直接正常运行。
  • 保留v3.02的适配方案:删除oem参数,改用v3支持的短参数格式,同时将远程图片下载到本地后再识别:
const tesseract = require("node-tesseract-ocr");
const fs = require("fs");
const https = require("https");

const config = {
    lang: "eng",
    args: ["-psm", "3"] // 用v3支持的短参数格式
};

// 下载远程图片到本地
const downloadImage = (url, dest) => {
    return new Promise((resolve, reject) => {
        const file = fs.createWriteStream(dest);
        https.get(url, (res) => {
            res.pipe(file);
            file.on("finish", () => file.close(resolve));
        }).on("error", (err) => {
            fs.unlink(dest, () => reject(err));
        });
    });
};

downloadImage("https://tesseract.projectnaptha.com/img/eng_bw.png", "temp_img.png")
.then(() => tesseract.recognize("temp_img.png", config))
.then((text) => {
    console.log("Result:", text);
    fs.unlinkSync("temp_img.png"); // 清理临时文件
})
.catch((err) => console.log(err.message));

可靠的图片文字提取替代库

  • Tesseract.js:纯JS实现的Tesseract OCR,无需本地安装Tesseract引擎,支持Node.js和浏览器环境,多语言识别,API简洁友好,适合快速开发。
  • easyocr:基于PyTorch的OCR库,识别精度和速度表现优异,支持数十种语言,可通过子进程在Node.js中调用,也提供云端API服务。
  • ocrad.js:轻量级JS OCR库,体积小巧,适合印刷体文字的简单识别场景,性能稳定。
  • Google Cloud Vision API:云端OCR服务,识别精度极高,支持手写体、文档结构化等高级功能,适合企业级应用,需付费使用。

内容的提问来源于stack exchange,提问作者M Maaz Azhar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 03:55:15