zoukankan      html  css  js  c++  java
  • linux安装Tesseract-OCR

    安装Tesseract-OCR
    1. leptonica 需要源码编译安装http://www.leptonica.org/
    leptonica 包: leptonica-1.73.tar.gz
     解压后切换到leptonica-1.68 根目录  
    ./configure
    make
    make install
    2.tesseract安装:
     依赖安装完毕后开始安装tesseract
     下载tesseract-3.01 安装包: http://tesseract-ocr.googlecode.com/files/tesseract-3.01.tar.gz
     解压后切换到tesseract-3.01 根目录
     (如果在make时遇到类似strngs.h:1: error: stray '357' in program 的错误,请将tesseract-3.01/ccutil/strngs.h 文件转为ANSI 编码保存,再重新编译) 
    ./autogen.sh
    ./configure
    make
    make install
    ldconfig
    tesseract英文语言包安装:  
    下载tesseract-3.01 英文语言包: http://tesseract-ocr.googlecode.com/files/tesseract-ocr-3.01.eng.tar.gz
    sudo cp tesseract-ocr/tessdata/* /usr/local/share/tessdata/
    测试一下:
     切换到解压后的tesseract-3.01 根目录(这个目录下有一个自带的phototest.tif 可以做测试用)
    tesseract phototest.tif phototest -l eng

     tesseract 1.jpg result -l chi_sim -psm 7 nobatch
     输出:1
      这时应该在当前目录生成一个phototest.txt 文本文件,内容就是phototest.tif 显示的文字.

    Tesseract-OCR的QA合集

    A.ImageMagick是什么?

    ImageMagick是一个用于查看、编辑位图文件以及进行图像格式转换的开放源代码软件套装

    我在这里之所以提到ImageMagick是因为某些图片格式需要用这个工具来转换。

    B.Leptonica 是什么?

    Leptonica 是一图像处理与图像分析工具,tesseract依赖于它。而且不是所有的格式(如jpg)都能处理,所以我们需要借助imagemagick做格式转换。

    Here's a summary of compression support and limitations:
        - All formats except JPEG support 1 bpp binary.
        - All formats support 8 bpp grayscale (GIF must have a colormap).
        - All formats except GIF support 24 bpp rgb color.
        - All formats except PNM support 8 bpp colormap.
        - PNG and PNM support 2 and 4 bpp images.
        - PNG supports 2 and 4 bpp colormap, and 16 bpp without colormap.
        - PNG, JPEG, TIFF and GIF support image compression; PNM and BMP do not.
        - WEBP supports 24 bpp rgb color.

    C.提高图片质量?

    识别成功率跟图片质量关系密切,一般拿到后的验证码都得经过灰度化,二值化,去噪,利用imgick就可以很方便的做到.

    convert -monochrome foo.png bar.png #将图片二值化

    D.我只想识别字符和数字?

    结尾仅需要加digits

    命令实例:tesseract imagename outputbase digits

    E.训练你的tesseract

    不得不说,tesseract英文识别率已经很不错了(现有的tesseract-data-eng),但是验证码识别还是太鸡肋了。但是请别忘记,tesseract的智能识别是需要训练的.

    F.命令执行出现empty page!!错误

    严格来说,这不是一个bug(tesseract 3.0),出现这个错误是因为tesseract搞不清图像的字符布局

    -psm N
        Set Tesseract to only run a subset of layout analysis and assume a certain form of image. The options for N are:


        0 = Orientation and script detection (OSD) only.
        1 = Automatic page segmentation with OSD.
        2 = Automatic page segmentation, but no OSD, or OCR.
        3 = Fully automatic page segmentation, but no OSD. (Default)
        4 = Assume a single column of text of variable sizes.
        5 = Assume a single uniform block of vertically aligned text.
        6 = Assume a single uniform block of text.
        7 = Treat the image as a single text line.
        8 = Treat the image as a single word.
        9 = Treat the image as a single word in a circle.
        10 = Treat the image as a single character.

    对于我们的验证码a.tif排列来说,采用-psm 7(single text line)比较合适。

    5.Tesseract-OCR的训练方法

    A.使用jTessBoxEditor工具

    1.下载地址:http://download.csdn.net/detail/whatday/7740739

    这个工具是用来训练样本用的,由于该工具是用JAVA开发的,需要安装JAVA虚拟机1.6才能运行。

    2. 获取样本图像。用画图工具绘制了5张0-9的文样本图像(当然样本越多越好),如下图所示:

      

      

    3.合并样本图像。运行jTessBoxEditor工具,在点击菜单栏中Tools--->Merge TIFF。在弹出的对话框中选择样本图像(按Shift选择多张),合并成num.font.exp0.tif文件。

    4.生成Box File文件。打开命令行,执行命令:

    [plain] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. tesseract.exe num.font.exp0.tif num.font.exp0 batch.nochop makebox  

      生成的BOX文件为num.font.exp0.box,BOX文件为Tessercat识别出的文字和其坐标。

    注:Make Box File的命令格式为:

    [plain] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. tesseract [lang].[fontname].exp[num].tif [lang].[fontname].exp[num] batch.nochop makebox  

    其中lang为语言名称,fontname为字体名称,num为序号,可以随便定义。

    5.文字校正。运行jTessBoxEditor工具,打开num.font.exp0.tif文件(必须将上一步生成的.box和.tif样本文件放在同一目录),如下图所示。可以看出有些字符识别的不正确,可以通过该工具手动对每张图片中识别错误的字符进行校正。校正完成后保存即可。

    6.定义字体特征文件。Tesseract-OCR3.01以上的版本在训练之前需要创建一个名称为font_properties的字体特征文件。

    font_properties不含有BOM头,文件内容格式如下:

    [plain] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <fontname> <italic> <bold> <fixed> <serif> <fraktur>  

    其中fontname为字体名称,必须与[lang].[fontname].exp[num].box中的名称保持一致。<italic> 、<bold> 、<fixed> 、<serif>、 <fraktur>的取值为1或0,表示字体是否具有这些属性。

    这里在样本图片所在目录下创建一个名称为font_properties的文件,用记事本打开,输入以下下内容:

    [plain] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. font 0 0 0 0 0  

    这里全取值为0,表示字体不是粗体、斜体等等。

    7.生成语言文件。在样本图片所在目录下创建一个批处理文件,输入如下内容。

    [plain] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. rem 执行改批处理前先要目录下创建font_properties文件  
    2.   
    3. echo Run Tesseract for Training..  
    4. tesseract.exe num.font.exp0.tif num.font.exp0 nobatch box.train  
    5.   
    6. echo Compute the Character Set..  
    7. unicharset_extractor.exe num.font.exp0.box  
    8. mftraining -F font_properties -U unicharset -O num.unicharset num.font.exp0.tr  
    9.   
    10. echo Clustering..  
    11. cntraining.exe num.font.exp0.tr  
    12.   
    13. echo Rename Files..  
    14. rename normproto num.normproto  
    15. rename inttemp num.inttemp  
    16. rename pffmtable num.pffmtable  
    17. rename shapetable num.shapetable   
    18.   
    19. echo Create Tessdata..  
    20. combine_tessdata.exe num.  

    将批处理通过命令行执行。执行后的结果如下:

    需确认打印结果中的Offset 1、3、4、5、13这些项不是-1。这样,一个新的语言文件就生成了。

    num.traineddata便是最终生成的语言文件,将生成的num.traineddata拷贝到Tesseract-OCR-->tessdata目录下。可以用它来进行字符识别了。

    训练前:

    1.准备一副待识别的图像,这里用画图工具随便写了一串数字,保存为number.jpg,如下图所示:
            

    2.  打开命令行,定位到Tesseract-OCR目录,输入命令:

    [plain] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. tesseract.exe number.jpg result -l eng  

         其中result表示输出结果文件txt名称,eng表示用以识别的语言文件为英文。

    3.  打开Tesseract-OCR目录下的result.txt文件,看到识别的结果为7542315857,有3个字符识别错误,识别率还不是很高,那有没有什么方法来提供识别率呢?Tesseract提供了一套训练样本的方法,用以生成自己所需的识别语言库。下面介绍一下具体训练样本的方法。

         

    训练后:

    用训练后的语言库识别number.jpg文件, 打开命令行,定位到Tesseract-OCR目录,输入命令:

    [plain] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. tesseract.exe number.jpg result -l eng  

    识别结果如如图所示,可以看到识别率提高了不少。通过自定义训练样本,可以进行图形验证码、车牌号码识别等。感兴趣的朋友可以研究研究。

    B.使用 CowBoxer工具

    下载地址为:http://download.csdn.net/detail/whatday/7740815

    第一步生成第一个 box 文件
    演示中将 Tesseract 解压到了 E: esseract-ocr 目录。然后在该目录中建立了一个 build 目录用于存放原始数据和训练过程中生成的文件。原始图片数据一个有 3 个 (test.001.tif - test.003.tif):





    首先生成第一个图片 test.001.tif 的 box 文件,这里使用官方的 eng 语言数据进行文字识别:

    E: esseract-ocruild>.. esseract test.001.tif test.001 -l eng batch.nochop makebox
    Tesseract Open Source OCR Engine with Leptonica
    Number of found pages: 1.


    执行完这个命令之后,build 目录下就生成了一个 test.001.box。使用 CowBoxer 打开这个 box 文件,CowBoxer 会自动找到同名的 tif 文件显示出来。



    CowBoxer 的使用方法可以看 Help -> About 中的说明。修改完成之后 File -> Save box file 保存文件。

    生成初始的 traineddata

    接下来使用这一个 box 文件先生成一个 traineddata,在接下来生成其他图片的 box 文件时,使用这个 traineddata 有利于提高识别的正确率,减少修改次数。

    .. esseract test.001.tif test.001 nobatch box.train
    .. rainingunicharset_extractor test.001.box
    .. rainingmftraining -U unicharset -O test.unicharset test.001.tr
    .. rainingcntraining test.001.tr
    rename normproto test.normproto
    rename Microfeat test.Microfeat
    rename inttemp test.inttemp
    rename pffmtable test.pffmtable
    .. rainingcombine_tessdata test.


    在 build 目录下执行完这一系列命令之后,就生成了可用的 test.traineddata。

    生成其余 box 文件

    将上一步生成的 test.traineddata 移动到 tesseract-ocr essdata 目录中,接下来生成其他 box 文件时就可以通过 -l test 参数使用它了。

    .. esseract test.002.tif test.002 -l test batch.nochop makebox
    .. esseract test.003.tif test.003 -l test batch.nochop makebox


    这里仅仅是使用 3 个原始文件作为例子。实际制作训练文件时,什么时候生成一个 traineddata 根据情况而定。中途生成 traineddata 的目的只是为了提高文字识别的准确率,使后面生成的 box 文件能少做修改。

    生成最终的 traineddata

    在所有的 box 都制作完成后,就可以生成最终的 traineddata 了。

    .. esseract test.001.tif test.001 nobatch box.train
    .. esseract test.002.tif test.002 nobatch box.train
    .. esseract test.003.tif test.003 nobatch box.train
    .. rainingunicharset_extractor test.001.box test.002.box test.003.box
    .. rainingmftraining -U unicharset -O test.unicharset test.001.tr test.002.tr test.003.tr
    .. rainingcntraining test.001.tr test.002.tr test.003.tr
    rename normproto test.normproto
    rename Microfeat test.Microfeat
    rename inttemp test.inttemp
    rename pffmtable test.pffmtable
    .. rainingcombine_tessdata test.


    在文件较多时可以用程序生成这种脚本执行。

    图片大小调整:

    convert pic_name  -scale 200% pic_name;

  • 相关阅读:
    java类的继承
    Java数组运算
    Eclipse问题提示
    oracle的insert的时候&符号如何插入(转义)
    修改数据库用户名--CMD环境执行有效
    java环境变量设置
    当前时间时间戳以及10分钟后的时间戳
    复选框事件
    分享到
    多物体运动
  • 原文地址:https://www.cnblogs.com/timssd/p/5330602.html
Copyright © 2011-2022 走看看