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

Initialise Tesseract before or after namedWindow from OpenCV

本文关键字:或之后 初始化 Tesseract OpenCV namedWindow      更新时间:2023-10-16

我想知道这是一个错误还是我不明白什么。
示例 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)

区别:
创建窗口和初始化的顺序。
编辑:

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

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

这是一个已知的问题。看看github问题,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);