一、源碼編譯安裝:#
安裝前建議你備份一下 /usr/include/ 目錄下 opencv 和 opencv2 兩個目錄。
反正我每次安裝完 tesseract 後,我的 opencv 頭文件不是沒了就是出問題。
github 地址: https://github.com/tesseract-ocr/tesseract/releases
建議下載安裝 4.0 版本,有 LSTM,準確率和速度都不錯。
其次建議下載安裝 3.05 版本,也是此時的最新版本,沒有 LSTM。
安裝介紹地址: [ https://github.com/tesseract-
ocr/tesseract/blob/master/INSTALL.GIT.md ](https://github.com/tesseract-
ocr/tesseract/blob/master/INSTALL.GIT.md)
懶得打開上面安裝鏈接的看下面就好。
-
先安裝各種依賴
sudo apt-get install g++ # or clang++ (presumably)
sudo apt-get install autoconf automake libtool
sudo apt-get install autoconf-archive
sudo apt-get install pkg-config
sudo apt-get install libpng12-dev
sudo apt-get install libjpeg8-dev
sudo apt-get install libtiff5-dev
sudo apt-get install zlib1g-dev
如果你要安裝訓練工具的話,還要安裝下面三個依賴
sudo apt-get install libicu-dev
sudo apt-get install libpango1.0-dev
sudo apt-get install libcairo2-dev
2. 然後安裝 Leptonica
這個軟件有很多版本,先給你下載地址,然後給你版本對照表。
官網地址: http://www.leptonica.org/
必須源碼編譯安裝,默認安裝目錄在 /usr/local/ 下
-
安裝 tesseract
./autogen.sh
./configure
make
sudo make install
sudo ldconfig
make training
sudo make training-install
二、部分 API 介紹#
預訓練模型地址: https://github.com/tesseract-ocr/tessdata
預訓練模型可以放到任意位置,也可以放到默認位置:/usr/local/share/tessdata/
這個方法基本原理我當時也沒深研究。
我理解大概就是先把圖片轉化為灰度圖。
然後以當前點為中心,判斷周圍一圈的點是否不為白色。
如果這個點不是白色,那麼繼續以這個點為中心點向外擴張。
通過上面簡單原理,我們可以知道。
輸入圖片越小運行速度越快
輸入圖片越乾淨輸出越準確
字符不能有過大旋轉角度(作者肯定不會 360 度旋轉訓練模型,他也確實沒這麼做)
下面是 API 例子:
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
int main()
{
char *outText;
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
//設置識別引擎,如果要用cube引擎,需要去上面下載cube對應的預訓練模型。
tesseract::OcrEngineMode enginemode = static_cast
(0);
/*
"OCR Engine modes:"
" 0 Original Tesseract only."
" 1 Cube only."
" 2 Tesseract + cube."
" 3 Default, based on what is available."
4 Neural nets (LSTM) only.(版本4.0才有)
*/
// 初始化,NULL可以替換成你存放預訓練模型的路徑。下面"eng"可以是"eng++chi_sim"形式。
if (api->Init(NULL, "eng", enginemode))
{
fprintf(stderr, "Could not initialize tesseract.\n");
exit(1);
}
//下面兩句話都是設置只識別數字,第一種方法是打開識別開關,第二種方法是只識別白名單內字符。當然還有黑名單。具體看下面我給的API地址。
//api->SetVariable("classify_bln_numeric_mode", "1");
//api->SetVariable("tessedit_char_whitelist", "0123456789");
//設置識別頁面類型
tesseract::PageSegMode pagesegmode = static_cast
(7);
api->SetPageSegMode(pagesegmode);
/*
"Page segmentation modes:"
" 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."
" 11 Sparse text. Find as much text as possible in no particular order."
" 12 Sparse text with OSD."
" 13 Raw line. Treat the image as a single text line,"
*/
// Open input image with leptonica library
Pix *image = pixRead("/usr/src/tesseract/testing/phototest.tif");
api->SetImage(image);
//下面是通過opencv讀取圖片
//cv::Mat gray_img, thres_img;
//cv::Mat img = cv::imread("");
//cv::cvtColor(img, gray_img, cv::COLOR_BGR2GRAY);
//cv::threshold(gray_img, thres_img, 127., 255., cv::THRESH_BINARY);
//api->SetImage((uchar*)thres_img.data, thres_img.cols, thres_img.rows, 1, thres_img.cols);
// Get OCR result
outText = api->GetUTF8Text();
printf("OCR output:\n%s", outText);
// Destroy used object and release memory
api->End();
delete [] outText;
pixDestroy(&image);
return 0;
}
API 頭文件地址: https://github.com/tesseract-ocr/tesseract/blob/master/api/baseapi.h
先這樣吧,這個工具功能蠻強大的。還需要時間慢慢研究。
順便提一嘴我做卡片識別的思路。
關鍵技術是 opencv 的 findContours () 配合 finetune 後的 mnist,數字識別效果拔群。
以上部分內容參考自: https://github.com/tesseract-ocr/tesseract