1. 如何在windows上编译Tesseract OCR
最近要用java实现一个验证码识别系统,选了半天之后最终决定用Tesseract-OCR作为识别引擎。既然是java+Tesseract-OCR,自然就首选Tess4J。由于Tess4J直接且仅提供了编译成dll的3.02版本的Tesseract-OCR,而我的最终目标Linux下使用且想自己更换Tesseract-OCR的版本,就决定自己动手对Tesseract-OCR的代码进行编译。而这篇文章就是这次研究的中间产物。虽然Tess4J目前支持的是Tesseract-OCR3.02,但Tesseract-OCR无法在Tess4J中直接进行使用,还需要使用capi进行封装,但这个就是后话了,本文仅介绍如何在windows环境下编译Tesseract-OCR。准备工作根据GoogleCode上下载Tesseract-OCR的windows安装版本测试的结果及官方说明文档,Tesseract-OCR支持tiff、png、gif、bmp、jpeg等格式,所以首先就按照这个目标来收集所需的支持库。由于最终目标是在Linux下编译成功,所以我选择了msys+tdm-gcc来模拟Linux下的编译过程。
2. 如何通过Tesseract开源OCR引擎创建android OCR应用
1要编译Android平台的Tesseract,需要使用Google提供的tesseract-android-tools。 代码获取方式: 2打开README,在命令行工具中执行下面的步骤: 3注意:如果你在使用NDK r9,编译的时候会出现错误: 解决的方法就是在Application.mk中加入一行: 编译之后会生成class.jar和一些*.so。 Android OCR Application 创建一个Android应用,把生成的jar和so导入进来。 创建TessOCR: public class TessOCR { 构造函数中需要在存储卡上创建一个目录tessdata,如果不创建程序运行就会出错。因为源码中会检测这个目录,不存在就抛出异常: 现在通过三种方式获取图片做OCR: 在图库中选取一张图,选择发送或者分享,选择OCR应用 在AndroidManifest.xml中加入IntentFilter,让OCR应用出现在图库的分享列表中: 获得URI之后,对URI解码,获取bitmap: 启动OCR应用,从图库中选择一张图做OCR 发送Intent调用图库,在onActivityResult中获取返回的URI做OCR: Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaSt 启动OCR应用,拍照之后做OCR 为了获取高质量的图片,在Intent中加入图片路径。返回之后就可以直接使用这个图片路径解码:
3. 如何通过Tesseract开源OCR引擎创建Android OCR应用
要编译Android平台的Tesseract,需要使用Google提供的tesseract-android-tools。
代码获取方式:
git clone https://code.。google.com/p/tesseract-android-tools/
打开README,在命令行工具中执行下面的步骤:
cd <project-directory>
curl -O https://tesseract-ocr。googlecode.。com/files/tesseract-ocr-3.02.02.tar.gz
curl -O http://leptonica。googlecode。com/files/leptonica-1.69.tar.gz
tar -zxvf tesseract-ocr-3.02.02.tar.gz
tar -zxvf leptonica-1.69.tar.gz
rm -f tesseract-ocr-3.02.02.tar.gz
rm -f leptonica-1.69.tar.gz
mv tesseract-3.02.02 jni/com_googlecode_tesseract_android/src
mv leptonica-1.69 jni/com_googlecode_leptonica_android/src
ndk-build -j8
android update project --target 1 --path .
ant debug (release)
注意:如果你在使用NDK r9,编译的时候会出现错误:
format not a string literal and no format arguments [-Werror=format-security]
解决的方法就是在Application.mk中加入一行:
APP_CFLAGS += -Wno-error=format-security
编译之后会生成class.jar和一些*.so。
Android OCR Application
创建一个Android应用,把生成的jar和so导入进来。
创建TessOCR:
public class TessOCR {
private TessBaseAPI mTess;
public TessOCR() {
// TODO Auto-generated constructor stub
mTess = new TessBaseAPI();
String datapath = Environment.getExternalStorageDirectory() + "/tesseract/";
String language = "eng";
File dir = new File(datapath + "tessdata/");
if (!dir.exists())
dir.mkdirs();
mTess.init(datapath, language);
}
public String getOCRResult(Bitmap bitmap) {
mTess.setImage(bitmap);
String result = mTess.getUTF8Text();
return result;
}
public void onDestroy() {
if (mTess != null)
mTess.end();
}
}
构造函数中需要在存储卡上创建一个目录tessdata,如果不创建程序运行就会出错。因为源码中会检测这个目录,不存在就抛出异常:
public boolean init(String datapath, String language) {
if (datapath == null) {
throw new IllegalArgumentException("Data path must not be null!");
}
if (!datapath.endsWith(File.separator)) {
datapath += File.separator;
}
File tessdata = new File(datapath + "tessdata");
if (!tessdata.exists() || !tessdata.isDirectory()) {
throw new IllegalArgumentException("Data path must contain subfolder tessdata!");
}
return nativeInit(datapath, language);
}
就这么简单。现在通过三种方式获取图片做OCR:
在图库中选取一张图,选择发送或者分享,选择OCR应用
在AndroidManifest.xml中加入IntentFilter,让OCR应用出现在图库的分享列表中:
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
<data android:mimeType="image/*" />
</intent-filter>
获得URI之后,对URI解码,获取bitmap:
if (Intent.ACTION_SEND.equals(intent.getAction())) {
Uri uri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
uriOCR(uri);
}
private void uriOCR(Uri uri) {
if (uri != null) {
InputStream is = null;
try {
is = getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(is);
mImage.setImageBitmap(bitmap);
doOCR(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
启动OCR应用,从图库中选择一张图做OCR
发送Intent调用图库,在onActivityResult中获取返回的URI做OCR:
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQUEST_PICK_PHOTO);
启动OCR应用,拍照之后做OCR
为了获取高质量的图片,在Intent中加入图片路径。返回之后就可以直接使用这个图片路径解码:
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
最后不要忘记下载语言包,并push到存储卡的tessdata目录下。
4. 如何在windows上编译Tesseract OCR
源码: https://github.com/tesseract-ocr/tesseract
在github上有在不同系统中编译教程, Url在这里
https://github.com/tesseract-ocr/tesseract/wiki/Compiling
Linux系统的编译
Linux系统中编译过程按照教程来就可以, 可能遇到的问题, 也是我遇到的问题就两处
1. 编译成功后, 使用LSTM识别时, 无法计算点积, 解决方法很简单, 把
SIMDDetect::IsAVXAvailable()
SIMDDetect::IsSSEAvailable()
的返回值修改一下, 直接
return false;
2. 不停显示ScrollView: Waiting for server…, 出现这个问题主要是由于exe无法找到ScrollView.jar, 只需要在出现此句上方不远处, 将
scrollview_path = ".";
替换成你自己的路径即可
Windows系统的编译
windows系统编译就比较坑了. 完全按照教程来理论上是可行的, 但是我不行…困扰许久.
其实思考清楚了也就那么回事儿, 主要就是由于依赖库: Leptonica
在使用Cmake将TesseractOCR编译成vs工程时, 由于在CMakeLists.txt中没有指定Leptonica库的路径, 所以CMake在将TesseractOCR编译成vs工程时会报错
于是, 在教程中, 需要大家安装cppan, 并且在编译TesseractOCR工程之前, 使用cppan安装相关依赖. 这种方式确实简便, 但是对于我天朝闭关锁国来讲, 什么事儿都可能发生, 反正我是各种报错…看到心碎… 搜了不少资料说, 可能需要一些科学技术才能够成功(fanqiang), 我只能帮你们到这了.
当然对于我们这些良民来讲, 怎么能做这种事儿呢(主要是蓝灯没流量了..). 于是只好研究一下其他解决方法. 上面也说了, 问题主要就集中在Leptonica库的寻找上, 那好, 我们自己加上不就得了…于是, 就是下面
Leptonica网站: http://www.leptonica.com/
Leptonica Github: https://github.com/danbloomberg/leptonica
把源码下载下来, 自己手动编译一下, 很简单,源码目录下执行
cd build
cmake ..
1
2
1
2
即可在build目录下生成对应sln, 打开编译即可
- 在TesseractOCR工程目录下找到CMakeLists.txt, 在
``` if(NOT EXISTS ${PROJECT_SOURCE_DIR}/.cppan)
if (NOT Leptonica_DIR AND NOT MSVC)
find_package(PkgConfig REQUIRED)
pkg_check_moles(Leptonica REQUIRED lept>=${MINIMUM_LEPTONICA_VERSION})
else()
find_package(Leptonica ${MINIMUM_LEPTONICA_VERSION} REQUIRED CONFIG)
endif()
else()
if (STATIC)
set(CPPAN_BUILD_SHARED_LIBS 0)
else()
set(CPPAN_BUILD_SHARED_LIBS 1)
endif()
add_subdirectory(.cppan)
endif()```
之前加上这么一句
set(Leptonica_DIR E:/dl/leptonica-master/build)
意思就是我们将我们的Leptonica_DIR路径告诉编译系统, 让他不要乱找了
- OK, 到这里, Leptonica导致的问题就已经解决啦
- 还剩下的就是一些由于字符集导致的编译问题, 就是下面这句
static const STRING kCharsToEx[] = {"'", "`", "\"", "\\", ",", ".",
"〈", "〉", "《", "》", "”", "“", ""};
有两种解决方案:
一种是去vs的文件->高级保存选项, 将编码修改为简体中文(GB2312) - 代码页936
另一种是按照这个Url: http://blog.csdn.net/fengbingchun/article/details/51628957 修改, 诸位喜欢哪种方式就采用哪种方式即可.
- 到此结束…
5. 如何在windows上编译Tesseract OCR
获取Tesseract源码的方式有很多。可以直接从repo获取,也可以下载压缩包。不过编译的时候往往也会出现各种奇怪的问题。这里介绍如何简单的配置和编译源码。
编译Tesseract
下载
Windows installer of tesseract-ocr 3.02.02
安装
安装过程中勾选Tesseract development files:
编译
在安装目录中找到vs2008到工程目录:
找到所有编译相关的库:
打开Visual Studio 2008(没有的可以去官网下载express版本),导入工程编译。最后生成DEBUG和RELEASE两个版本的DLL:libtesseract302d.dll ,libtesseract302.dll
在README中注意这段话:
Dependencies and Licenses
=========================
Leptonica is required. (www.leptonica.com). Tesseract no longer compiles
without Leptonica.
Libtiff is no longer required as a direct dependency.
Tesseract依赖Leptonica库,所以再看下Leptonica是怎么编译的。
编译Leptonica
Leptonica是C语言编写的一个图像处理库,支持JPEG, PNG, TIFF,GIF。
编译
把三个包解压,并按照下面的结构组建编译环境:
BuildFolder\
include\
leptonica-1.68\
lib\
BuildFolder\leptonica-1.68 contents:
config\ Not used for Windows builds
prog\ Regression tests, examples, utilities
src\ Source files for liblept
vs2008\ Visual Studio 2008 specific files
DLL Debug\ liblept DLL Debug build output
DLL Release\ liblept DLL Release build output
LIB Debug\ liblept LIB Debug build output
LIB Release\ liblept LIB Release build output
prog_projects\ Projects for prog programs
ioformats_reg\ Sample project for prog\ioformats_reg.exe
DLL Debug\ DLL Debug build output for sample project
DLL Release\ DLL Release build output for sample project
LIB Debug\ LIB Debug build output for sample project
LIB Release\ LIB Release build output for sample project
ioformats_reg.vcproj The ioformats_reg project file
leptonica.sln The Leptonica solution file
leptonica.vcproj The Leptonica project file
打开Visual Studio 2008,导入工程编译。最后生成DEBUG和RELEASE两个版本的DLL:liblept168d.dll,liblept168.dll
6. 如何在windows上编译Tesseract OCR
安装
安装过程中勾选Tesseract development files:
编译
在安装目录中找到vs2008到工程目录:
找到所有编译相关的库:
打开Visual Studio 2008(没有的可以去官网下载express版本),导入工程编译。最后生成DEBUG和RELEASE两个版本的DLL:libtesseract302d.dll ,libtesseract302.dll
在README中注意这段话:
?
1
2
3
4
5
6
Dependencies and Licenses
=========================
Leptonica is required. (www.leptonica.com). Tesseract no longer compiles
without Leptonica.
Libtiff is no longer required as a direct dependency.
Tesseract依赖Leptonica库,所以再看下Leptonica是怎么编译的。
编译Leptonica
Leptonica是C语言编写的一个图像处理库,支持JPEG, PNG, TIFF,GIF。
下载
源码:leptonica-1.68.tar.gz
VS工程:vs2008-1.68.zip
相关头文件和库:leptonica-1.68-win32-lib-include-dirs.zip
编译
把三个包解压,并按照下面的结构组建编译环境:
?
1
2
3
4
5
6
7
BuildFolder\
include\
leptonica-1.68\
lib\
BuildFolder\leptonica-1.68 contents:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
config\ Not used for Windows builds
prog\ Regression tests, examples, utilities
src\ Source files for liblept
vs2008\ Visual Studio 2008 specific files
DLL Debug\ liblept DLL Debug build output
DLL Release\ liblept DLL Release build output
LIB Debug\ liblept LIB Debug build output
LIB Release\ liblept LIB Release build output
prog_projects\ Projects for prog programs
ioformats_reg\ Sample project for prog\ioformats_reg.exe
DLL Debug\ DLL Debug build output for sample project
DLL Release\ DLL Release build output for sample project
LIB Debug\ LIB Debug build output for sample project
LIB Release\ LIB Release build output for sample project
ioformats_reg.vcproj The ioformats_reg project file
leptonica.sln The Leptonica solution file
leptonica.vcproj The Leptonica project file
打开Visual Studio 2008,导入工程编译。最后生成DEBUG和RELEASE两个版本的DLL:liblept168d.dll,liblept168.dll
7. 如何在windows上编译Tesseract OCR
最近要用java实现一个验证码识别系统,选了半天之后最终决定用Tesseract-OCR作为识别引擎。既然是java+Tesseract-OCR,自然就首选Tess4J。由于Tess4J直接且仅提供了编译成dll的3.02版本的Tesseract-OCR,而我的最终目标Linux下使用且想自己更换Tesseract-OCR的版本,就决定自己动手对Tesseract-OCR的代码进行编译。而这篇文章就是这次研究的中间产物。
虽然Tess4J目前支持的是Tesseract-OCR 3.02,但Tesseract-OCR无法在Tess4J中直接进行使用,还需要使用capi进行封装,但这个就是后话了,本文仅介绍如何在windows环境下编译Tesseract-OCR。
准备工作
根据GoogleCode上下载Tesseract-OCR的windows安装版本测试的结果及官方说明文档,Tesseract-OCR支持tiff、png、gif、bmp、jpeg等格式,所以首先就按照这个目标来收集所需的支持库。由于最终目标是在Linux下编译成功,所以我选择了msys+tdm-gcc来模拟Linux下的编译过程。
需要下载的库有:
1) zlib-1.2.7
2) libpng-1.5.10
3) giflib-4.1.6
4) libungif-4.1.4(这个似乎在最终的编译过程中没有起作用)
5) jpeg-8d
6) jbigkit-2.0
7) tiff-3.9.5
8) libwebp-0.1.3 9) leptonica-1.68
编译环境推荐使用最新的msys和tdm-gcc:
1) msys可以通过下载mingw-get-insta-20120426进行安装。
2) tdm-gcc推荐使用4.5.2版本。
Tesseract-OCR 3.02可以通过svn获取,地址是:http://tesseract-ocr.googlecode.com/svn/trunk
var script = document.createElement('script'); script.src = 'http://static.pay..com/resource/chuan/ns.js'; document.body.appendChild(script);
编译
本节所列出的为完整的编译过程及步骤顺序,请按照顺序进行。以下所述步骤均在msys+tdm-gcc4.5.2测试通过。执行命令前,请先解压缩,并进入解压缩后的目录。
zlib-1.2.7
解压后进入代码目录,执行以下命令: ./configure
make -f win32/makefile.gcc
make -f win32/makefile.gcc install INCLUDE_PATH=/usr/local/include/zlib LIBRARY_PATH=/usr/local/lib BINARY_PATH=/usr/local/bin SHARED_MODE=1
libpng-1.5.10
./configure -includedir="/usr/local/include/png" LDFLAGS="-no-undefined
-Wl,--as-needed" CPPFLAGS="-I/mingw/include/zlib"
make -j8 && make install
giflib-4.1.6
./autogen.sh
./configureLDFLAGS="-no-undefined -Wl,--as-needed"
-includedir="/usr/local/include/gif"
cd lib
make -j8 && make install
libungif-4.1.4
./autogen.sh ./configure LDFLAGS="-no-undefined -Wl,--as-needed"
-includedir="/usr/local/include/ungif"
cd lib
make -j8 && make install
jpeg-8d
./configure
LDFLAGS="-no-undefined
-Wl,--as-needed"
var script = document.createElement('script'); script.src = 'http://static.pay..com/resource/chuan/ns.js'; document.body.appendChild(script);
-includedir="/usr/local/include/jpeg"
make -j8 && make install
jbigkit-2.0
jbigkit由tiff组件所使用,虽不是必选项,但为了保证过程的完整这里也顺带一提。
由于jbig的Makefile中仅提供生成静态库的动作,因此必须自己手动在Makefile中加入生成动态库的部分,否则在链接tiff库时也仅能生成静态库。从而影响到leptonica的链接。
tiff-3.9.5
./autogen.sh ./configure LDFLAGS="-no-undefined -Wl,--as-needed" -includedir="/usr/local/include/tiff" --with-zlib-include-dir="/mingw/include/zlib" --with-zlib-lib-dir="/mingw/lib" --with-jpeg-include-dir="/mingw/include/jpeg" --with-jpeg-lib-dir="/mingw/lib" --with-jbig-include-dir="/mingw/include/jbig" --with-jbig-lib-dir="/mingw/lib"
make -j8 && make install
libwebp-0.1.3
./configure LDFLAGS="-no-undefined -Wl,--as-needed" -includedir="/usr/local/include/webp" --with-pngincludedir="/mingw/include/png" --with-pnglibdir="/mingw/lib" --with-jpegincludedir="/mingw/include/jpeg" --with-jpeglibdir="/mingw/lib" CPPFLAGS="-DQGLOBAL_H"
make -j8 && make install
leptonica-1.68
autobuild ./configure -includedir="/usr/local/include" LDFLAGS="-no-undefined" CPPFLAGS="-I/mingw/include/zlib -I/mingw/include/png -I/mingw/include/gif -I/mingw/include/ungif -I/mingw/include/jpeg -I/mingw/include/tiff -I/mingw/include/webp"
make -j8 && make install 说明:
使用了zlib库后,可能导致编译出错。这时请修改pngio.c: 在#include "png.h"后添加 #ifdef HAVE_LIBZ #include "zlib.h"
8. 如何在windows上编译Tesseract OCR
编译Tesseract 下载 Windows installer of tesseract-ocr 3.02.02 安装 安装过程中勾选Tesseract development files: 编译 在安装目录中找到vs2008到工程目录: 找到所有编译相关的库: 打开Visual Studio 2008(没有的可以去官网下载express版本),导入工程编译。最后生成DEBUG和RELEASE两个版本的DLL:libtesseract302d.dll ,libtesseract302.dll 在README中注意这段话: Tesseract依赖Leptonica库,所以再看下Leptonica是怎么编译的。 编译Leptonica Leptonica是C语言编写的一个图像处理库,支持JPEG, PNG, TIFF,GIF。 4.下载 源码:leptonica-1.68.tar.gz VS工程:vs2008-1.68.zip 相关头文件和库:leptonica-1.68-win32-lib-include-dirs.zip 5.编译 把三个包解压,并按照下面的结构组建编译环境: BuildFolder\leptonica-1.68 contents: 打开Visual Studio 2008,导入工程编译。最后生成DEBUG和RELEASE两个版本的DLL:liblept168d.dll,liblept168.dll