在Qt中 调用 Tesseract,配置 C++ API

来源:互联网 发布:中国网络日报有无权威 编辑:程序博客网 时间:2024/06/11 20:10

首先,我的环境:Ubuntu 16.04 + Tesseract-ocr 3.04 + QtCreator 5.6
有两种编译方式: cmake 和 qmake

  1. 若使用 cmake 编译, 则要修改 CMakeLists.txt 文件。具体修改方法可以自行搜索。
  2. 若使用 qmake 所以要修改 ~.pro 文件。

这里使用的是简单的 qmake: 需要修改 ~.pro 文件,主要是将 tesseract 在系统中的路径添加在 ~.pro 文件。

如: 我的 ~.pro 文件:
// 因为调用了 OPenCV, 所以把 OpenCV 也包含在内了。

TEMPLATE = appCONFIG += console c++11CONFIG -= app_bundleCONFIG -= qtSOURCES += main.cppINCLUDEPATH += /usr/local/include/tesseract \            /usr/include/leptonica \            /usr/local/include/leptonica \            /usr/local/include/opencv \            /usr/local/include/opencv2LIBS += -L/usr/local/lib/ -llept -ltesseract \        /usr/local/lib/libopencv_highgui.so \        /usr/local/lib/libopencv_core.so    \        /usr/local/lib/libopencv_imgproc.so \        -lopencv_core -lopencv_imgcodecs -lopencv_highgui

注意:-llept -ltesseract 一定要添加进去~否则编译会报错。

在main.cpp中,要包含头文件:

#include <tesseract/baseapi.h>      // tesseract提供的关于C++的接口#include <tesseract/strngs.h> 

如: 编译执行 GitHub 中 Tesseract WIki 中的 C++ API 例子:
其中, main.cpp 文件:

#include <iostream>// Tesseract-ocr #include <tesseract/baseapi.h>#include <leptonica/allheaders.h>//OpenCV#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <opencv2/imgproc/imgproc.hpp>#include <bits/ios_base.h>using namespace std;using namespace cv;int main(){      // Basic example        char *outText;        tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();        // Initialize tesseract-ocr with English, without specifying tessdata path        if (api->Init(NULL, "num1"))        {            fprintf(stderr, "Could not initialize tesseract.\n");            exit(1);        }        // Open input image with leptonica library        Pix *image = pixRead("/~/Mytest2/invoice2.jpg"); //absolute path of file        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;}

待识别图片:
这里写图片描述
识别结果:
这里写图片描述

阅读全文
0 0
原创粉丝点击