tesseract是一个基于C++编写的开源OCR(光学字符识别)库,对于做机器学习中NLP领域有很重要的作用,某些时候,为了方便可以不需要从头搭建模型训练OCR,那么采用开源框架就是一个便捷的做法
本文简单介绍一下linux系统中安装和使用tesseract以及调用该库的C++ api进行开发
此处用的linux发行版是ubuntu 14.04,其他环境同理
理论上是可以对于所有依赖都下载源码编译安装,但是为了规避其中各种奇怪的坑,这里直接通过linux的包管理器下载安装全部依赖库
step1
sudo apt-get install libpng-dev
sudo apt-get install libjpeg8-dev
sudo apt-get install libtiff5-dev
如果还缺少依赖,可以加上
sudo apt-get install zlib1g-dev
step2
sudo apt-get install libleptonica-dev
step3
sudo apt install libtesseract-dev
此时,相关的开发包依赖已经安装完毕,默认安装目录如下:
头文件目录:
库目录:
其实,这几个都可以通过源码编译安装,这里就用ubuntu的包管理器安装更方便
step4
对于已训练数据集,默认安装好tesseract后会在 /usr/share/tesseract-ocr/tessdata这个目录里面,其实可以通过设置环境变量或者api里面设置目录的方式更改存放目录
类似于 eng.trainedddata,表示英文字符集
可以在github上下载对应的其他语言的已训练数据复制到这个目录,该目录会在程序运行时自动寻找到
tessdata下载地址:tessdata
使用命令行工具测试
准备好png,tif,jpg,bmp格式的文字测试图片,例如 phototest.png
输入命令行
tesseract phototest.png png_res
即可生成对应的文本文件,其中图片的后缀可以改成多种支持的格式,默认生成txt格式的输出文件
.
├── bmp_res.txt
├── jpg_res.txt
├── phototest.bmp
├── phototest.jpg
├── phototest.png
├── phototest.tif
├── png_res.txt
└── tif_res.txt
识别结果
The quick brown dog jumped over the
lazy fox. The quick brown dog jumped
over the lazy fox. The quick brown dog
jumped over the lazy fox. The quick
brown dog jumped over the lazy fox.
tesseract开放了C++ api供调用,方便集成到已有的项目中去,这里写了个简单的示例,参照官网
项目结构
.
├── build
│ ├── Makefile
│ ├── ocr_cpp_test
├── CMakeLists.txt
├── phototest.png
├── src
└── main.cpp
main.cpp
#include
#include
int main()
{
char *outText;
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
// Initialize tesseract-ocr with English, without specifying tessdata path
if (api->Init(NULL, "eng")) // you can set tessdata path here
{
fprintf(stderr, "Could not initialize tesseract.\n");
exit(1);
}
// Open input image with leptonica library
Pix *image = pixRead("../phototest.png");
api->SetImage(image);
// 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;
}
cmake配置文件
project(ocr_cpp_test)
cmake_minimum_required(VERSION 2.8)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -std=c++11 -W")
include_directories(
/usr/include
)
aux_source_directory(./src DIR_SRCS)
link_directories(/usr/lib)
add_executable(ocr_cpp_test ${DIR_SRCS})
target_link_libraries(ocr_cpp_test
tesseract
lept
)
编译运行结果:
OCR output:
This is a lot of 12 point text to test the
ocr code and see if it works on all types
of file format.
The quick brown dog jumped over the
lazy fox. The quick brown dog jumped
over the lazy fox. The quick brown dog
jumped over the lazy fox. The quick
brown dog jumped over the lazy fox.