Node-tesseract OCR Windows使用小结

时间:2021-01-13 08:57:04

1.首先安装 Tesseract-OCR (windows )

github
wiki

2.设置环境变量

1)PATH 增加  tesseract 安装目录

2)新增系统变量 TESSDATA_PREFIX='安装目录文件夹下\tessdata'

3.nodejs 调用

安装wraper

npm install node-tesseract

示例:

var tesseract = require('node-tesseract');

 // Recognize text of any language in any format

tesseract.process(__dirname+'/test.png',function(err, text) {
    if(err) {
            console.error(err);
   } else {
             console.log(text);
              }
       });

4. 多语言

通过-l 选项,依次增加语言
语法:
tesseract imagename outputbase [-l lang] [-psm pagesegmode] [configfile…]

var options = {
        l: 'chi_sim+eng',
        psm: 6,
};

tesseract.process(__dirname + '/test.jpg', options, function(err, text) {
    if(err) {
            console.error(err);
    } else {
           console.log(text);
 }

});

-psm:

Member name Value Description
PSM_OSD_ONLY 0 Orientation and script detection only.
PSM_AUTO_OSD 1 Automatic page segmentation with orientation and script detection. (OSD)
PSM_AUTO_ONLY 2 Automatic page segmentation, but no OSD, or OCR.
PSM_AUTO 3 Fully automatic page segmentation, but no OSD.
PSM_SINGLE_COLUMN 4 Assume a single column of text of variable sizes.
PSM_SINGLE_BLOCK_VERT_TEXT 5 Assume a single uniform block of vertically aligned text.
PSM_SINGLE_BLOCK 6 Assume a single uniform block of text. (Default.)
PSM_SINGLE_LINE 7 Treat the image as a single text line.
PSM_SINGLE_WORD 8 Treat the image as a single word.
PSM_CIRCLE_WORD 9 Treat the image as a single word in a circle.
PSM_SINGLE_CHAR 10 Treat the image as a single character.
PSM_SPARSE_TEXT 11 Find as much text as possible in no particular order.
PSM_SPARSE_TEXT_OSD 12 Sparse text with orientation and script det.
PSM_RAW_LINE 13 Treat the image as a single text line, bypassing hacks that are Tesseract-specific.

5.配合 GraphicsMagick 识别验证码的例子

参考链接 GraphicsMagick for node

6.Localized OCR(识别特定区域)

有时候需要识别特定区域,可以通过UZN 文件配合 -psm 4参数实现

Tesseract can read in uzn files, and use them instead of doing its own segmentation, on two conditions:

  • The segmentation mode PSM_SINGLE_COLUMN must be used (Check manpage for details)
  • The uzn file must be named imageName.uzn, so for scan01.png the uzn file must be named scan01.uzn

即两个前提:

  • 使用-psm 4参数

  • uzn 文件与图片文件名称相同

例如,考虑有内容如下的图片test.png:

This is a text

       This is another test

 This is a last test

test.uzn内容:

50 65 100 15 Text

命令行输入:

tesseract.exe test.png test -psm 4

输出结果:

This is another test

7. node-tesseract 增加hocr输出

两种方法:

1) 首先修改安装目录下 \lib\tesseract.js ,第22行处options增加hocr属性

options: {
           'l': 'eng',
          'psm': 3,
          'config': null,
          'binary': 'tesseract',
          'hocr':null
},

70行增加:

if (options.hocr !== null) {
command.push('hocr');
}

如果想要输出hocr格式,参考:

var options = {
          l: 'chi_sim+eng',
          psm: 4,
          hocr:'hocr'
};

tesseract.process( '/test.png', options, function(err, text) {
          if(err) {
                    console.error(err);
        } else {
                console.log(text);
          }
        });

2)参考 链接
好像主文件没更新,需要修改到自己的 tesseract.js 文件中