Tesseract3.01 OCR在VS2008环境下的编译使用(1)

接触Tesseract没几天,只是想用它做一下简单的字符识别,虽然是HP 1995年的产品,后面google公开来使用,效果还是可以的,如果对字符分割好,仅仅只作为单个字符的识别,如果作为版面分析来使用的话,效果不算太好。这个库国内的使用资料还是蛮少,如果在linux下面使用的话,还简单点,但是在win下面的使用,资料少,也没找到自己使用的简介。废话不说,我把我这几天折腾的过程说一下。

 

一 。tesseract3.01在VS2008下面编译生成DLL,LIB的过程

(1)和VS2008的使用,官方安装过程可以参考:http://tesseract-ocr.googlecode.com/svn/trunk/vs2008/doc/setup.html

安装需要的软件:VS2008,Leptonica 1.68源码Tesseract-OCR Visual Studio 2008 source files ,Tesseract-OCR source ,在使用的时候先建立C:\BuildFolder文件夹,将Leptonica 1.68源码Tesseract-OCR Visual Studio 2008 source files ,Tesseract-OCR source解压在这个文件夹下:文件夹中出现下面的结构:

 

View Code
   C:\BuildFolder

     include\
        leptonica\
        stdint.h
     lib\
     tesseract-3.01\
        vs2008\
           cntraining\
           combine_tessdata\
           libtesseract\
              libtesseract.vcproj
           mftraining\
           port\
           tesseract\
              tesseract.vcproj
           unicharset_extractor\
           wordlist2dawg\
           tesseract.sln

 

按照官方安装文档(地址http://tesseract-ocr.googlecode.com/svn/trunk/vs2008/doc/building.html)说明,打开C:\BuildFolder\tesseract-3.0x\vs2008\tesseract.sln,选择编译属性,应该有DLL_Debug, DLL_Release, LIB_Debug, LIB_Release几项选择,但是实际上,使用官方的工程打开以后,选择编译属性里面只有:debug,release.static,release.Dynamic这几项,使用bulid编译以后也并没有dll文件,z只有静态链接库LIB文件,官方文档也3.01版本在VS2008下不能使用DLL“The dll isn't supported in Tesseract-OCR 3.00/3.01.”对于这个问题,可以参考一下链接(http://groups.google.com/group/tesseract-dev/browse_thread/thread/75be5c97eb4d1b3c/a59952174f78c000?#a59952174f78c000),在VS2008中使用的时候,官方文档也没有做太多的说明,弄了一个晚上也没弄明白。

 

后来在国外下载一个别人对源代码框架修改一下,使用起来更加方便一点,下载地址:https://github.com/tinku99/tesseract-ocr ,包含了解压好的Leptonica 1.68源码Tesseract-OCR Visual Studio 2008 source files ,Tesseract-OCR source,但是也对框架进行了修改,并且修改了代码里面关于DLL输出的问题,用python对文件做了一些归类,省了很多麻烦。整个过程的安装如下:

 

(1)这个工程要使用Python编译器,所有要首先安装  Python2.7(win平台)软件,安装在C盘,并且在VS2008上进行配置,将Python2.7安装在C盘,打开VS2008,工具-》选项-》项目和解决方案-》VC++目录,然后在“平台”选项下,选择“Win32”;“显示一下内容的目录”选项下,然后选 择“包含文件”,在下面的目录中,添加“C:/Python27/include”;选择“库文件”,添“C:/Python27 /libs”。另外需要注意的问题是,VS2008在DEBUG下面会有“找不到python25_d.lib”的问题,可以做一下修改:在python目录include文件夹下,打开pyconfig.h,
找到如下语句

# ifdef _DEBUG
# pragma comment(lib,"python25_d.lib")
# else
# pragma comment(lib,"python25.lib")
# endif /* _DEBUG */
将python25_d.lib改成python25.lib

b)找到
#ifdef _DEBUG
# define Py_DEBUG
#endif
将其用/**/注释掉就可以了。 

(2)代码解压后在C:\BuildFolder下:结构如下,

View Code
 C:\BuildFolder

     include\
        leptonica\
     lib\
     tesseract-3.01\
        api\
        ccmain\
        ccstruct\
        ccutil\
        classify\
        config\
        contrib\
        cube\
        cutil\
        dict\
        doc\
        image\
        java\
        image\
        neural_networks\
        tessdata\
        testing\
        textord\
        training\
        viewer\
        vs2008\
        wordrec\

打开C:\BuildFolder\tesseract-3.0x\vs2008\tesseract.sln,选择DLL_Debug, DLL_Release, LIB_Debug, LIB_Release编译选择。

点击libtesseract301工程右键,进行bulid,如下的DLL,LIB文件,

 

   static libraries:
      libtesseract301-static.lib
      libtesseract301-static-debug.lib
   DLLs:
      libtesseract301.lib  (import library)
      libtesseract301.dll
      libtesseract301d.lib (import library)
      libtesseract301d.dll

选择soluton主功能,选择编译选项,编译后得到:

  LIB_Release: tesseract.exe
  LIB_Debug:   tesseractd.exe
  DLL_Release: tesseract-dll.exe
  DLL_Debug:   tesseract-dlld.exe

三 测试编译结果

建立文件夹,C:\BuildFolder include\ lib\ tesseract-3.01\ testing,以DEBUG模式为例,将一下几个文件copy到testing文件夹下面

   liblept168d.dll
   libtesseract301d.dll
   tesseractd.exe
   eurotext.tif(图片)

需要注意的是:需要在testing文件夹中包含tesseract3的语言包,不然会出错,官方下载汉语文件包,在testing文件夹下建立文件夹tessdata,将语言包解压在这个文件夹下。

测试需要用命令行指令,在dos下,找到testing文件夹,输入:

 tesseractd.exe eurotext.tif eurotext

这个指令将输入的图片提取出来的字符输出到文本文件eurotext中,测试结果如下:

 

 

输出的文本:

The (quick) [brown] {fox} jumps!
Over the $43,456.78 <lazy> #90 dog
& duck/goose, as 12.5% of E-mail
from aspammer@website.com is spam.
Der ,,schnelle” braune Fuchs springt
fiber den faulen Hund. Le renard brun
<<rapide» saute par-dessus le chien
paresseux. La volpe marrone rapida
salta sopra il cane pigro. El zorro
marrén répido salta sobre el perro
perezoso. A raposa marrom rzipida
salta sobre 0 cfio preguieoso.     


四:在VS2008下的使用方法
(1)工程设置,tool-->工程与方案里面增加tesseract3.0使用的头文件以及库文件

在include files里面添加有文件

   C:\BuildFolder\include
   C:\BuildFolder\include\leptonica(and all its sub-directories)
   C:\BuildFolder\include\tesseract (and all its sub-directories)

 

 

在library files里面添加有文件: C:\BuildFolder\lib

 

 

(2)设置类属性

在VS2008工程-->属性-->C++-->preprocesser中添加宏,__MSW32__;USE_STD_NAMESPACE;TESSDLL_IMPORTS;CCUTIL_IMPORTS;LIBLEPT_IMPORTS

 

 

在VS2008工程-->属性-->linker-->additional depandence添加附加库:

ws2_32.lib
user32.lib
liblept$(LIBLEPT_VERSION)d.lib
libtesseract$(LIBTESS_VERSION)d.lib

 

 

(3)C:\BuildFolder\include中的tesseract_versionnumbers.vsprops可以用来设置工程的库引用,原文:Using tesseract_versionnumbers.vsprops (which automatically inherits leptonica_versionnumbers.vsprops) can make it easier to specify the libraries you need to import.在工程中,

Property Sheet-->Configuration Properties -->General -->Inherited Project Property Sheets item to-->添加tesseract_versionnumbers.vsprops的引用地址,

 

 

(4)添加=头文件

#include "allheaders.h"(必须)
#include "baseapi.h"
#include "resultiterator.h"(必须)
#include "strngs.h"
#include "blobs.h"

如果有文件中没有"strngs.h"中,可以再次下载另外一份文件,来替换C:\BuildFolder\include\tesseract 文件(包含一个测试文件)tesseract-3.01-win32-include-dir-sampleapp-2011-11-08.zip

 

主要的程序和引用主要在baseapi.h文件中

 

推荐链接:

http://code.google.com/p/tesseract-ocr/

http://tesseract-ocr.googlecode.com/svn/trunk/vs2008/doc/programming.html

https://github.com/tinku99/tesseract-ocr

 

posted @ 2012-06-03 07:08  RubbyZhang  阅读(7013)  评论(8编辑  收藏  举报