C++ 如何在 Visual Studio 2010 中实现 Tesseract 与项目一起运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18180824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:46:42  来源:igfitidea点击:

How to implement Tesseract to run with project in Visual Studio 2010

c++opencvocrtesseract

提问by OpenMinded

I have a C++ project in Visual Studio 2010 and wish to use OCR. I came across many "tutorials" for Tesseract but sadly, all I got was a headache and wasted time.

我在 Visual Studio 2010 中有一个 C++ 项目并希望使用 OCR。我遇到了许多 Tesseract 的“教程”,但遗憾的是,我得到的只是头痛和浪费时间。

In my project I have an image stored as a Mat. One solution to my problem is to save this Mat as an image (image.jpg for example) and then call Tesseract executable file like this:

在我的项目中,我将图像存储为Mat。我的问题的一种解决方案是将此 Mat 保存为图像(例如 image.jpg),然后像这样调用 Tesseract 可执行文件:

system("tesseract.exe image.jpg out");

Which gets me an output out.txtand then I call

这让我得到一个输出out.txt然后我打电话

infile.open ("out.txt");

to read the output from Tesseract.

读取 Tesseract 的输出。

It is all good and works like a chair but it is not an optimal solution. In my project I am processing a video so save/call .exe/write/readat 10+ FPS is not what I am really looking for. I want to implement Tesseract to existing code so to be able to pass a Mat as an argument and immediately get a result as a String.

这一切都很好,像椅子一样工作,但它不是最佳解决方案。在我的项目中,我正在处理视频,因此以 10+ FPS保存/调用 .exe/写入/读取并不是我真正想要的。我想对现有代码实现 Tesseract,以便能够将 Mat 作为参数传递并立即获得字符串形式的结果。

Do you know any good tutorial(pref. step-by-step) to implement Tesseract OCR with Visual Studio 2010? Or your own solution?

你知道用 Visual Studio 2010 实现 Tesseract OCR 的任何好的教程(pref. step-by-step)吗?还是你自己的解决方案?

回答by OpenMinded

OK, I figured it out but it works for Releaseand Win32configuration only (No debug or x64). There are many linking errors under Debug configuration.

好的,我想通了,但它仅适用于ReleaseWin32配置(无调试或 x64)。Debug配置下有很多链接错误。

So,

所以,

1.First of all, download prepared library folder(Tesseract + Leptonica) here:

1.首先,在这里下载准备好的库文件夹(Tesseract + Leptonica):

Mirror 1(Google Drive)

镜像 1(Google 云端硬盘)

Mirror 2(MediaFire)

镜子 2(MediaFire)



2.Extract tesseract.zipto C:\

2.提取tesseract.zipC:\



3.In Visual Studio, go under C/C++ > General > Additional Include Directories

3.在 Visual Studio 中,进入C/C++ > General > Additional Include Directories

Insert C:\tesseract\include

插入 C:\tesseract\include



4.Under Linker > General > Additional Library Directories

4.Linker > General > Additional Library Directories

Insert C:\tesseract\lib

插入 C:\tesseract\lib



5.Under Linker > Input > Additional Dependencies

5.Linker > Input > Additional Dependencies

Add:

添加:

liblept168.lib
libtesseract302.lib


Sample code should look like this:

示例代码应如下所示:

#include <tesseract\baseapi.h>
#include <leptonica\allheaders.h>
#include <iostream>

using namespace std;

int main(void){

    tesseract::TessBaseAPI api;
    api.Init("", "eng", tesseract::OEM_DEFAULT);
    api.SetPageSegMode(static_cast<tesseract::PageSegMode>(7));
    api.SetOutputName("out");

    cout<<"File name:";
    char image[256];
    cin>>image;
    PIX   *pixs = pixRead(image);

    STRING text_out;
    api.ProcessPages(image, NULL, 0, &text_out);

    cout<<text_out.string();

    system("pause");
}

For interaction with OpenCV and Mattype images look HERE

对于与 OpenCV 和Mat类型图像的交互,请看这里

回答by farukterzioglu

It has been a lot since the last reply but it may be help to others;

自从上次回复以来已经很多了,但它可能对其他人有帮助;

  1. I think you must also add "liblept168.lib" and "liblept168d.lib" to Additional Dependencies
  2. Add "liblept168.dll" and "liblept168d.dll" to the destination of your exe.
  3. Add #include to your code.
  1. 我认为您还必须将“liblept168.lib”和“liblept168d.lib”添加到附加依赖项
  2. 将“liblept168.dll”和“liblept168d.dll”添加到您的exe的目标位置。
  3. 将#include 添加到您的代码中。

(This answer must be a comment to Bruce's answer. Sorry for confusion. )

(此答案必须是对布鲁斯答案的评论。抱歉造成混淆。)

回答by Bruce

You need to use the library through the API.

您需要通过 API 使用该库。

Most probably:

最可能: