一、在Mac OS下安装Tesseract
命令行输入: brew install tesseract
大概不到一分钟就安装完毕,速度超级快,意料之外
安装完成之后,可以在命令行使用tesseract --version
查看是否成功及版本信息。
在各平台编译安装Tesseract难度从低到高排列依次是:Mac < Windows <Linux
总结一下:Mac超简单,Linux超级麻烦
二.安装后的Tesseract文件位置
默认安装到“/usr/local/Cellar”目录下,如图:
重点关注"tesseract"和“leptopnica”两个文件夹。
1.需要的头文件文件夹目录
/usr/local/Cellar/leptonica/1.77.0_1/include
/usr/local/Cellar/tesseract/4.0.0/include/tesseract
2.需要的动态库(或静态库)目录
/usr/local/Cellar/leptonica/1.77.0_1/lib 我使用的动态库ibtlept.5.dylib
/usr/local/Cellar/tesseract/4.0.0/lib 我使用的动态库libtessract.4.dylib
3.字库目录(默认只有英文字库 eng.traindata,其它语言另外下载)
/usr/local/Cellar/tesseract/4.0.0/share/tessdata
所有东西都准备好了,配置就简单了吧。不会就百度吧
三、代码示例
包含头文件:
#include "allheaders.h"
#include "capi.h"
识别:
//读取图片,原图像的路径
PIX *img;
if ((img = pixRead(argv[1])) == NULL)
{
cout<<"Load image error..."<<endl;
return -1;
}
//创建Tesseract实例
TessBaseAPI *handle;
handle = TessBaseAPICreate();
//初始化Tesseract(设置字库路径,识别语言)
string tessdataPath="/Users/lanzy/Cellar/tessdata";
if (TessBaseAPIInit3(handle, tessdataPath.c_str(), "eng") != 0)
{
cout<<"Error initialising tesseract\n"<<endl;;
return -1;
}
//设置图片
TessBaseAPISetImage2(handle, img);
//识别
if (TessBaseAPIRecognize(handle, NULL) != 0)
cout<<"Error in Tesseract recognition\n"<<endl;
//获取识别结果
char *text = NULL;
if ((text = TessBaseAPIGetUTF8Text(handle)) == NULL)
cout<<"Error getting text\n"<<endl;
cout<<text<<endl;
//释放
TessDeleteText(text);
TessBaseAPIEnd(handle);
TessBaseAPIDelete(handle);
pixDestroy(&img);
so easy