導航:首頁 > 源碼編譯 > tesseract源碼編譯

tesseract源碼編譯

發布時間:2022-12-09 01:44:48

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

閱讀全文

與tesseract源碼編譯相關的資料

熱點內容
程序員理發店生意怎麼樣 瀏覽:601
程序員羅技 瀏覽:180
軟考初級程序員課程2021下載 瀏覽:487
杭州程序員奶奶 瀏覽:878
不聽命令造成錯誤 瀏覽:979
kool系統源碼 瀏覽:608
流氓app在哪裡看 瀏覽:98
域名購買了怎麼指向伺服器 瀏覽:121
安卓手機如何讓照片顏色反轉 瀏覽:859
怎麼下載卓睿安手機版 瀏覽:514
h3crange命令 瀏覽:468
php前景和python 瀏覽:338
php壓縮圖片內存大小 瀏覽:495
在哪裡可以查看雲伺服器的信息 瀏覽:70
python讀取非txt文件 瀏覽:799
艾莫迅用什麼編程軟體好 瀏覽:227
android文件存儲讀取 瀏覽:214
php基礎教程第5版 瀏覽:543
伺服器裡面怎麼刷東西 瀏覽:194
榮耀手機如何快速把app切換頁面 瀏覽:798