4

My setup: Currently I'm working with c++ in visual studio 2017 on Windows 10.

The objective: To start using tesseract ocr in my basic c++ application. First, to make sure I'm able to #include the tesseract library and compile and execute a very simple program, I'm trying to run the simple test program below, provided on the official tesseract project's "APIExample" page.

What I've done so far: Following the advice of this stack overflow answer, I've ran the vcpkg install tesseract:x64-windows command in the command prompt along with the command .\vcpkg integrate install. When I run the command vcpkg list I see all of the packages that I installed(shown below in screenshot), but despite this intellisense in visual studio gives me errors saying it can't find the includes to run the aforementioned test project whose code I've posted below. What gives? I've provided a screenshot below of my visual studio setup with the errors and error codes produced for reference.

#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>

int main()
{
    char *outText;

    tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
    // Initialize tesseract-ocr with English, without specifying tessdata path
    if (api->Init(NULL, "eng")) {
        fprintf(stderr, "Could not initialize tesseract.\n");
        exit(1);
    }

    // Open input image with leptonica library
    Pix *image = pixRead("/usr/src/tesseract/testing/phototest.tif");
    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;
}

enter image description here

enter image description here

Mrcitrusboots
  • 317
  • 3
  • 14

2 Answers2

4

Looks like your tesseract package has been installed for x64 platform, but your project settings seems to be in x86. Correct that and ensure you choose "multi-threaded dynamically linked" in the library settings. If all goes well, MSCV IDE will automatically copy those dependency DLLs to your application directory at runtime.

seccpur
  • 4,996
  • 2
  • 13
  • 21
  • 1
    I made the change from x86 to x64 and now it compiles, which is great news. For the second step where you mention choosing "multi-threaded dynamically linked" in the library settings, where in visual studio are the library settings? I can't find them under any of the drop down menus. – Mrcitrusboots Jun 20 '18 at 17:48
  • Since in that simple test program I pasted into the original post, the if statement on line 9, `if (api->Init(NULL, "eng"))` is getting triggered which errors, printing out "could not initialize tesseract". Once I find out how to enable "multi-threaded dynamically linked" might that solve the problem? – Mrcitrusboots Jun 20 '18 at 19:57
  • 1
    In your project property page under c++/code Generation /Runtime library choose multi-threaded debug DLL . Hope you will be able to build the executable with the dependency lib files. – seccpur Jun 21 '18 at 06:19
  • 1
    I really appreciate the help. I checked and found that " multi-threaded debug DLL" was already selected. The problem seems to be that the call to `api->Init(NULL, "eng")` returns -1 which triggers the if statement, [as shown in this image.](https://i.imgur.com/OFGKzLI.jpg). I'm not sure if this is a problem with the English language data or something else. – Mrcitrusboots Jun 21 '18 at 13:11
  • Also, If I delete that if block and try to run the program I receive [this error](https://imgur.com/vlMNA2W.jpg) on the console window. – Mrcitrusboots Jun 21 '18 at 14:21
  • I just downloaded the tessdata file from github and stuck it in the directory that tesseract was looking for it in. Hopefully that's acceptable since tesseract is running fine now. However, I would like to upgrade to tesseract 4.0 for greater OCR accuracy. How could I easily do that,considering I have 3.05.01 currently? – Mrcitrusboots Jun 21 '18 at 20:53
0

could not initialize tesseract

Open properties -> Configuration Properties -> Linker -> General -> Additional Library Directories -> your tesseract header(path to this file #include <tesseract/baseapi.h>)

Settle your header first

Raphael Sauer
  • 630
  • 1
  • 6
  • 27