在 OpenCV 的 namedWindow 之前或之后初始化 Tesseract

Initialise Tesseract before or after namedWindow from OpenCV

不知是bug还是我没看懂。
示例 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 );

结果:

Works fine.


示例 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);
}

结果:

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

区别:
创建 window 和初始化 tesseract 的顺序。
编辑:

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

此断言失败。这是否意味着 opencv 设置了区域设置而 tesseract 无法更改它?

这是 tesseract 的一个已知问题。查看 github issue Tesseract 团队目前正在努力解决它。 作为临时解决方案,您可以使用以下代码包装所有 tesseract 调用

// 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);