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
How to implement Tesseract to run with project in Visual Studio 2010
提问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.
好的,我想通了,但它仅适用于Release和Win32配置(无调试或 x64)。Debug配置下有很多链接错误。
So,
所以,
1.First of all, download prepared library folder(Tesseract + Leptonica) here:
1.首先,在这里下载准备好的库文件夹(Tesseract + Leptonica):
2.Extract tesseract.zip
to C:\
2.提取tesseract.zip
到C:\
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;
自从上次回复以来已经很多了,但它可能对其他人有帮助;
- I think you must also add "liblept168.lib" and "liblept168d.lib" to Additional Dependencies
- Add "liblept168.dll" and "liblept168d.dll" to the destination of your exe.
- Add #include to your code.
- 我认为您还必须将“liblept168.lib”和“liblept168d.lib”添加到附加依赖项
- 将“liblept168.dll”和“liblept168d.dll”添加到您的exe的目标位置。
- 将#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:
最可能:
start by downlaoding the libs ( https://code.google.com/p/tesseract-ocr/downloads/detail?name=tesseract-3.02.02-win32-lib-include-dirs.zip&can=2&q=). They're compiled with Visual 2008 but it should be enough
Use the API directly (example, look at an open source project using it: https://code.google.com/p/qtesseract/source/browse/#svn%2Ftrunk%2Ftessdata) and read the links from this answer : How can i use tesseract ocr(or any other free ocr) in small c++ project?
首先下载库(https://code.google.com/p/tesseract-ocr/downloads/detail?name=tesseract-3.02.02-win32-lib-include-dirs.zip&can=2&q=)。它们是用 Visual 2008 编译的,但应该足够了
直接使用 API(例如,查看使用它的开源项目:https: //code.google.com/p/qtesseract/source/browse/#svn%2Ftrunk%2Ftessdata)并阅读此答案中的链接:How我可以在小型 C++ 项目中使用 tesseract ocr(或任何其他免费的 ocr)吗?