問題描述
Tesseract OCR 在線程中使用時崩潰 (Tesseract OCR crash when used in thread)
我用 tesseract ocr 遇到了一個奇怪的問題。一切正常,就像 ocr 部分一樣。字符被正確識別。但它在完成所有計算後崩潰,這只發生在我在線程中運行代碼時。
void server(boost::asio::io_service & io_service, unsigned short port)
{
tcp::acceptor a(io_service, tcp::endpoint(tcp::v4(), port));
for (;;)
{
a.accept(sock);
//boost::thread t(session, boost::ref(sock));
//t.detach();
std::thread(session, std::move(sock)).detach();
}
}
void session(tcp::socket & sock)
{
tesseract::TessBaseAPI api;
if (api.Init("", "eng"))
{
fprintf(stderr, "Could not initialize tesseract.\n");
exit(1);
}
for (int i = 0; i < 81; i++)
{
if (!sudokuDatainside[i].empty())
{
api.SetImage((uchar*)sudokuDatainside[i].data, sudokuDatainside[i].size().width, sudokuDatainside[i].size().height, sudokuDatainside[i].channels(), sudokuDatainside[i].step1());
// Get OCR result
outText = api.GetUTF8Text();
std::cout << outText << std::endl;
}
else
{
std::cout << "Nothing ‑ " << i << std::endl;
}
}
api.End();
}
編輯:似乎它不是 api.End() 的問題。即使我不調用此方法,線程結束時程序也會崩潰... tesseract 是否支持線程?
參考解法
方法 1:
After some hours of trying to find a solution. I came across an error during building the debug libs. So rebuilding the tesseract debug libs solved the problem. Here are the instructions i used: https://github.com/charlesw/tesseract‑vs2012