Search code examples
c++opencvtesseract

Initialise Tesseract before or after namedWindow from OpenCV


I wonder whether it is a bug or I do not understand something.
Sample 1:

tesseract::TessBaseAPI *api;
api = new tesseract::TessBaseAPI();
if (api->Init(NULL, "eng")) {
    fprintf(stderr, "Could not initialize tesseract.\n");
    exit(1);
}
namedWindow( window_name, CV_WINDOW_NORMAL );

Result:

Works fine.


Sample 2:

namedWindow( window_name, CV_WINDOW_NORMAL );
tesseract::TessBaseAPI *api;
api = new tesseract::TessBaseAPI();
if (api->Init(NULL, "eng")) {
    fprintf(stderr, "Could not initialize tesseract.\n");
    exit(1);
}

Result:

!strcmp(locale, "C"):Error:Assert failed:in file baseapi.cpp, line 192
Segmentation fault (core dumped)

Difference:
The order of creating window and initialisation of tesseract.
Edit:

locale = std::setlocale(LC_CTYPE, nullptr);
ASSERT_HOST(!strcmp(locale, "C"));

This assert fails. Does it mean opencv sets locale and tesseract cannot change it?


Solution

  • This is a known issue of tesseract. Looking at the github issue Tesseract team is currently working on resolving it. As a temporary solution you can wrap all tesseract calls with the following code

    // set locale to "C" for tesseract
    char *old_ctype = strdup(setlocale(LC_ALL, NULL));
    setlocale(LC_ALL, "C");
    // some tesseract function, this is just an example.
    tesseract::TessBaseAPI api;
    api.InitForAnalysePage();
    
    // restore your previous locale
    setlocale(LC_ALL, old_ctype);
    free(old_ctype);